My environment: mysql: mysql-essential-5.1.51-win32
JDBC driver: I have uploaded to csdn last: http://download.csdn.net/source/3451945
Eclipse: Any version, free of charge, can be obtained by Baidu.
1. MySQL installation, not friends can see the connection: http://www.duote.com/tech/1/2430_1.html
Create a data file as follows:
Mysql> Create Database test; // create a database
Mysql> use test; // specify test as the database to be operated
Mysql> Create Table user (name varchar (20), password varchar (20); // create a table user and set two fields.
Mysql> insert into user values ('huzhiheng', '000000'); // insert a data entry to the table.
2. Open eclipse, create a project (my ),
Operation: Right-click my ---> build
Path ---> Add external archiver... select the JDBC driver and click OK.
My project list:
3. The driver has been imported. Let's write a program to verify it.
Import Java. SQL. *; public class mysqljdbc {public static void main (string ARGs []) {try {class. forname ("com. mySQL. JDBC. driver "); // load the MySQL JDBC driver // class. forname ("org. gjt. mm. mySQL. driver "); system. 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/test", "root", "198876"); // The Connection URL is JDBC: mySQL // server address/database name. The two parameters are respectively the login user name and password system. out. println ("Success connect MySQL server! "); Statement stmt = connect. createstatement (); resultset rs = stmt.exe cutequery ("select * from user"); // user is your table name while (RS. next () {system. out. println (RS. getstring ("name") ;}} catch (exception e) {system. out. print ("Get data error! "); E. printstacktrace ();}}}
Success loading MySQL driver!
Success connect MySQL server!
Huzhiheng
4. We can see the content in MySQL. Do we want to insert data into MySQL. In the following example, 100 data entries are inserted into the MySQL user table.
Import Java. SQL. *; public class myjproject {public static void main (string ARGs []) {try {class. forname ("com. mySQL. JDBC. driver "); // load the MySQL JDBC driver // class. forname ("org. gjt. mm. mySQL. driver "); system. 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/test", "root", "198876"); int num = 100; preparedstatement Statement = connect. preparestatement ("insert into user values (?,?) "); For (INT I = 0; I <num; I ++) // defines a 100 cycle and inserts one hundred pieces of information into the table. {Statement. setstring (1, "chongshi" + I); statement. setstring (2, "Bo" + I); statement.exe cuteupdate ();} //} catch (classnotfoundexception e) {// todo auto-generated Catch Block // system. out. println ("an error has occurred:" + E. tostring (); // E. printstacktrace ();} catch (sqlexception e ){}}}
5. Now let's open the MySQL database for viewing.
?
?
?
?