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 ();
}
}
}
}