Detailed introduction to Java connection Mysql _mysql

Source: Internet
Author: User

1.

Now the project (not SRC) right key--build path--add External Archives, select the driver of the jar package, this is release version, the bin directory is the debug version.

Example in the docs under the connector-j.html, there are examples (of which test is the database name, in exchange for their own).

Copy Code code as follows:

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 under the MySQL console, or you can do it by executing "\. Absolute path name ".

"--" is an annotation character.

Copy Code code as follows:

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
*/
The public static void main (string[] args) {//Multiple try merges together, and then uses the source---format
TODO auto-generated Method Stub
If you use the Finally, you need to put the statement outside the try
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {
Class.forName ("Com.mysql.jdbc.Driver"); If you add ". Newinstance" to the back, you'll need to add a few exceptions.
conn = Drivermanager.getconnection ("Jdbc:mysql://localhost/mydata?")
+ "User=root&password=root");
/*
* Java.sql.Statement; Not com.mysql this bag;
*/
stmt = Conn.createstatement ();
rs = Stmt.executequery ("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 ();
}
}

}

}

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.