1. MySQL JDBC driver package: http://dev.mysql.com/downloads/connector/j/
Version: mysql-connector-java-5.1.17.zip
2. Download any directory decompress to/mysql-connector-java-5.1.17/mysql-connector-java-5.1.17-bin.jar;
/Mysql-connector-java-5.1.17/directory also has docs and src folder, src folder has lib directory;
3. Prerequisites: the mysql and eclipse environments have been installed. refer to the previous blog article on installing the j2ee environment and mysql In Ubuntu;
4. Open eclispe, go to the menu windows-> prefrences, go to Java-> Build Path-> user libraries, select new, name mysql, Path:/mysql-connector-java-5.1.17/mysql-connector-java-5.1.17-bin.jar;
5. Create a java project named mysqlcon, right-click it, select Build Path-> Add libraries, select user library from the list, and select the mysql installation package deployed above in the next interface;
6. for development, create a new class under the mysqlcon project, name it my, and set the package to cn, that is, the java file of cn. my:
Package cn;
import java.sql.*;
Public ClassMy {Public Static VoidMain (String args []) {Try{Class. forName ("Com. mysql. jdbc. Driver");// Load the mysql jdbc driverSystem. out. println ("Success loading Mysql Driver!");}Catch(Exception e) {System. out. print ("Error loading Mysql Driver!"); E. printStackTrace ();}Try{Connection connect=DriverManager. getConnection ("Jdbc: mysql: // localhost: 3306/dbname","Root","Password");System. out. println ("Success connect Mysql server!");
// Select code Statement stmt=Connect. createStatement (); ResultSet rs=Stmt.exe cuteQuery ("Select * from t_auth");//T_auth indicates the table name. While(Rs. next () {System. out. println (rs. getString ("Auth_path"));//Auth_path is a table.T_auth Field
}
//insert into code
PreparedStatement=Connect. prepareStatement ("INSERTT_authVALUES (?,?)");Statement. setString (1,"2"); Statement. setString (2,"System Management"); Statement.exe cuteUpdate ();}Catch(Exception e) {System. out. print ("Get data error!"); E. printStackTrace ();}}}
Displayed and inserted correctly!