package librarysystem; import java.sql.DriverManager; import java.sql.SQLException; class Database{ private void ViewBooks() { try{ //to load the JDBC drivers Class.forName("com.mysql.jdbc.Driver").newInstance(); //Connection object which connect to the database //DriverManager.getConnection uses url, username, and password as input parameters //this code is for database on local machine java.sql.Connection connection = DriverManager.getConnection("jdbc:mysql:///demo","root",""); //ResultSet object is used to store the results returned by Query java.sql.ResultSet rs = null; //Statement object is used to execute SQL query on the database java.sql.Statement stmt = null; stmt = Main.connection.createStatement(); rs = stmt.executeQuery("SELECT ISBN,TITLE,AUTHOR,NUM_COPIES FROM BOOK;"); int i=0; //rs.next() loops through the resultset while(rs.next()) { //table display logic goes here } //close the database connection after you are done connection.close(); // don't forget to handle SQLException }catch(SQLException e) { e.printStackTrace(); } //handle other exceptions as well catch(Exception ex) { ex.printStackTrace(); } } }