Monday 3 September 2018

How to recover data from mysql to java

I am making a java program about library computer seat monitoring. If the library custodian enters the student's student number, he/she will be logged in and his/her firstname and lastname will be displayed on a JLabel in java GUI. The student number, firstname and lastname are all in the database. How will I retrieve those data from tha database and store it to the JLabel?


What something simple? Use JDBC
Here's an example from that wikipedia page:
Connection conn = DriverManager.getConnection(
     "jdbc:mysql:myhostname1",
     "myLogin",
     "myPassword" );
Statement stmt = conn.createStatement();
try {
    ResultSet rs = stmt.executeQuery( "SELECT * FROM MyTable" );
    try {
        while ( rs.next() ) {
            int numColumns = rs.getMetaData().getColumnCount();
            for ( int i = 1 ; i <= numColumns ; i++ ) {
               // Column numbers start at 1.
               // Also there are many methods on the result set to return
               //  the column as a particular type. Refer to the Sun documentation
               //  for the list of valid conversions.
               System.out.println( "COLUMN " + i + " = " + rs.getObject(i) );
            }
        }
    } finally {
        try { rs.close(); } catch (Throwable ignore) { /* Propagate the original exception
instead of this one that you may want just logged */ }
    }
} finally {
    try { stmt.close(); } catch (Throwable ignore) { /* Propagate the original exception
instead of this one that you may want just logged */ }
}

0 comments:

Post a Comment