Import java. SQL .*;
Public class TestJDBC {
Public static void main (String [] arges) throws SQLException, ClassNotFoundException {
String user = "root ";
String pwd = "mysql ";
// Set the URL
String myjdbc = "jdbc: mysql: // localhost: 3306/test ";
// Load the MYSQL driver
Class. forName ("com. mysql. jdbc. Driver ");
// Create a connection session
Connection myConnection = DriverManager. getConnection (myjdbc, user, pwd );
// Create a statement block object
Statement Myoperation = myConnection. createStatement ();
// Obtain the result set object
ResultSet record=Myoperation.exe cuteQuery ("select * from test ");
While (record. next ()){
// Output result set object
System. out. println (record. getInt ("id") + "," + record. getString ("name "));
}
// Close the result set
Try {
If (record! = Null)
// Handle exceptions when disabling the result set
Record. close ();
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
If (myConnection! = Null)
MyConnection. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
}
This article is from the "java technology community" blog