I didn't want to write such a simple article. I searched for my title on Baidu, which is totally in line with the title. However, I have been following these articles for a long time, but I cannot.
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, will 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 and create a project (my ),
Operation: Right-click my ---> build Path ---> add external Archiver... select 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, followed by 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: name of your table
- While (rs. next ()){
- System. out. println (rs. getString ("name "));
- }
- }
- Catch (Exception e ){
- System. out. print ("get data error! ");
- E. printStackTrace ();
- }
- }
- }
Click Run program:
- Success loading Mysql Driver!
- Success connect Mysql server!
- huzhiheng
The above result indicates that you have successfully connected to the database.
4. Check 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.
- Mysql> show tatabases; // view the database
- Mysql> use test; // set test as the database to be operated
- Mysql> show tables; // view all tables of the current database
- View sourceprint?
- Mysql> select * from user; // view all information of the current table user.
Note:: If you cannot connect to your database normally, check whether the driver, user name, password, table, and other information in your code are correct. Do not copy other people's Code directly, it can be used without reading it.