Connection to MySql using Java _ MySQL

Source: Internet
Author: User
Java connection to MySql details bitsCN.com

1.

Right-click the project (not Src) and choose -- Build Path -- Add External Archives. select the jar package under the driver. this is the release version and the debug version under the bin directory.

The example is in connector-j.html under docs. there is an example in it (the test is the database name, which is changed to your own ).

Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. SQLException;
Connection conn = null;
...
Try {
Conn =
DriverManager. getConnection ("jdbc: mysql: // localhost/test? "+
"User = monty & password = greatsqldb ");
// Do something with the Connection
...
} Catch (SQLException ex ){
// Handle any errors
System. out. println ("SQLException:" + ex. getMessage ());
System. out. println ("SQLState:" + ex. getSQLState ());
System. out. println ("VendorError:" + ex. getErrorCode ());
}

2. you can create a database directly on the MySql console or Run "/. absolute path name ".

"--" Is a annotator.

View Code
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;

Public class mysql {

/**
* @ Param args
*/
Public static void main (String [] args) {// merge multiple try values into one, and then use source --- format
// TODO Auto-generated method stub
// If finally is used, the statement must be placed outside try.
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

Try {
Class. forName ("com. mysql. jdbc. Driver"); // if ". newInstance" is added later, several throws must be added.
Conn = DriverManager. getConnection ("jdbc: mysql: // localhost/mydata? "
+ "User = root & password = root ");
/*
* Java. SQL. Statement; not the com. mysql package; both cannot exist simultaneously
*/
Stmt = conn. createStatement ();
Rs = stmt.exe cuteQuery ("select * from info ");

While (rs. next ()){
System. out. println (rs. getString ("name "));

}

// Do something with the Connection
} Catch (ClassNotFoundException ex ){
// Handle any errors
Ex. printStackTrace ();

} Catch (SQLException ex ){
// TODO Auto-generated catch block
System. out. println ("SQLException:" + ex. getMessage ());
System. out. println ("SQLState:" + ex. getSQLState ());
System. out. println ("VendorError:" + ex. getErrorCode ());
} Finally {
Try {
If (null! = Rs ){
Rs. close ();
Rs = null;
}

If (null! = Stmt ){
Stmt. close ();
Stmt = null;
}

If (null! = Conn ){
Conn. close ();
Conn = null;
}

} Catch (SQLException e ){
E. printStackTrace ();
}
}

}

}

BitsCN.com

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.