Friday, April 13, 2018

Display All Record in Java JDBC and MySQL

Here is a code that I wrote using Java and MySQL to display all the records from the database.

I am currently accepting programming work, it project, school programming projects , thesis and capstone projects, IT consulting work and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.


Program Listing

DisplayRecords.java

package jdbcdemo;

import java.sql.*;

/**
 * NetBeans IDE 8.2
 * @author Mr. Jake R. Pomperada
 * March 27, 2018  Tuesday
 * Bacolod City, Negros Occidental
 */

public class DisplayRecords {

    static Connection conn;

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://localhost:3306/persons";
            conn = DriverManager.getConnection(url, "root", "");
        } catch (Exception e) {
            e.printStackTrace();;
        }
    }
    public static void main(String[] args)throws Exception  {
      
     
      try {

          // calling the method selectRecords() to display  
          // all the records from MySQL in our screen.
          selectRecords();           
        
           closeConnection();
        }  catch (SQLException ex) {
            ex.printStackTrace();
        }
     
   
    }
    
 static void closeConnection() {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

  private static void selectRecords() 
          throws Exception {
       String selectrecordSQL = "SELECT * FROM users"; 
         
     PreparedStatement statement = conn.prepareStatement(selectrecordSQL);
     
     ResultSet result = statement.executeQuery(selectrecordSQL);
        
         if (result.next()) {
                ResultSetMetaData metaData = result.getMetaData();
                int numberOfColumns = metaData.getColumnCount();
                 System.out.println();
                 System.out.println("\t\t\t ===== LIST OF USER RECORDS =====");
                 System.out.println();
                 System.out.println("\t\t    Created By: Mr. Jake R. Pomperada, MAED-IT");
                 System.out.println();

                for (int i = 1; i <= numberOfColumns; i++) {
                    System.out.printf("%-15s", metaData.getColumnName(i));
                }
                System.out.println();

                do {
                    for (int i = 1; i <= numberOfColumns; i++) {
                        System.out.printf("%-15s", result.getObject(i));
                    }
                    System.out.println();
                } while (result.next());
                
                System.out.println();

            } else {
                System.out.println("No database records found.");
            }

         
    }

}

No comments:

Post a Comment