The following article describes the actual operation process of connecting JDBC to the MySQL database and the actual application code in the middle of the actual operation. The following describes the specific solution for connecting JDBC to the MySQL database, I hope this will help you in your future studies. PublicclassMySQLJdbc {publicStringdriverNamecom. MySQL. jdbc. Driv
The following article describes the actual operation process of connecting JDBC to the MySQL database and the actual application code in the middle of the actual operation. The following describes the specific solution for connecting JDBC to the MySQL database, I hope this will help you in your future studies. PublicclassMySQLJdbc {publicStringdriverName = com. MySQL. jdbc. Driv
The following article describes the actual operation process of connecting JDBC to the MySQL database and the actual application code in the middle of the actual operation. The following describes the specific solution for connecting JDBC to the MySQL database, I hope this will help you in your future studies.
Public class MySQLJdbc {
Public String driverName = "com. MySQL. jdbc. Driver ";
Public String url = "jdbc: MySQL: // localhost: 3306/oa ";
Public String userName = "root ";
Public String userPwd = "me ";
/**
* Apr 19,200 9 12:49:55
*
* @ Param args
*/
Public static void main (String [] args ){
MySQLJdbc my = newMySQLJdbc ();
My. read ();
}
Public void read (){
String queryString = "select userName, realName from user ";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Try {
Load driver
Class. forName (driverName );
Create a connection to the MySQL database
Conn = DriverManager. getConnection (url, userName, userPwd );
Create Statement
St = conn. createStatement ();
Run the SQL statement to obtain the query result.
Rs = st.exe cuteQuery (queryString );
Output query results
While (rs. next ()){
System. out. println ("userName:" + rs. getString ("userName ")
+ "RealName:" + rs. getString ("realName "));
}
Close Resources
Rs. close ();
St. close ();
Conn. close ();
} Catch (Exception ex ){
Output Error Message
Ex. printStackTrace ();
} Finally {
The finally Clause always executes (even if an error occurs), so that the resource is absolutely disabled.
Try {
If (rs! = Null)
Rs. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Try {
If (st! = Null)
St. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Try {
If (conn! = Null)
Conn. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
}
}
The above content is an introduction to connecting to the MySQL database through JDBC. I hope you will get some benefits.