Test the connection of jdk to mysql

Source: Internet
Author: User

  

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

 

Related Article

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.