JDBC connection to MySQL database

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.