There are three ways to drive MySQL,
1, the first is to put the jar package in the project directory, by adding a jar package, is the use of relative address, so that the project can be copied to other computers may also be used
2, the second method is to import the external jar package, is the absolute address, if the project to copy to another computer and re-import
3, how to configure the use of environment variables?
---------------------
Reference article: http://www.cnblogs.com/taoweiji/archive/2012/12/11/2812295.html
---------------------------------------------------------------------------------------------
Package JDBC2;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Connection;
Import java.sql.Statement;
public class Mysqldemo {
public static void Main (string[] args) throws Exception {
Connection conn = null;
String SQL;
MySQL jdbc url writing method: jdbc:mysql://host Name: The name of the connection port/database? parameter = value
Avoid Chinese garbled to specify Useunicode and characterencoding
To create a database on a database management system before performing a database operation, the name will be
The following statement is preceded by the creation of the Javademo database
String url = "Jdbc:mysql://localhost:3306/bizhi?"
+ "User=root&password=admin&useunicode=true&characterencoding=utf8";
try {
The reason to use the following statement is because you want to use the MySQL driver, so we have to drive it up,
It can be loaded through class.forname, or it can be driven by initialization, and the following three forms can be
Class.forName ("Com.mysql.jdbc.Driver");//dynamic load MySQL Driver
Or
Com.mysql.jdbc.Driver Driver = new Com.mysql.jdbc.Driver ();
Or
New Com.mysql.jdbc.Driver ();
SYSTEM.OUT.PRINTLN ("Load MySQL driver successfully");
A connection represents a database connection
conn = drivermanager.getconnection (URL);
Statement contains many methods, such as executeupdate can be inserted, update and delete, etc.
Statement stmt = Conn.createstatement ();
sql = "CREATE TABLE student (no char", name varchar (), primary key (No)) ";
int result = stmt.executeupdate (SQL);//The Executeupdate statement returns an affected number of rows if 1 is not successful
if (Result! =-1) {
SYSTEM.OUT.PRINTLN ("Create data Table Success");
sql = "INSERT into student (No,name) VALUES (' 2012001 ', ' Tao Weiki ')";
result = Stmt.executeupdate (SQL);
sql = "INSERT into student (No,name) VALUES (' 2012002 ', ' Zhou Xiaojun ')";
result = Stmt.executeupdate (SQL);
sql = "SELECT * from student";
ResultSet rs = stmt.executequery (sql);//ExecuteQuery Returns a collection of results, otherwise a null value is returned
System.out.println ("School number \ t name");
while (Rs.next ()) {
System.out
. println (rs.getstring (1) + "\ T" + rs.getstring (2));//Enter if the type of int is returned can be used with getint ()
}
}
} catch (SQLException e) {
System.out.println ("MySQL operation Error");
E.printstacktrace ();
} catch (Exception e) {
E.printstacktrace ();
} finally {
Conn.close ();
}
}
}