I didn't want to write such a simple personArticle, Search for my title on Baidu, which is totally consistent 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, 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 ' , ' 123456 ' ); // Insert a piece of data into the table
2. Open eclipse, create a project (my ),
Operation:Right-clickMy ---> build path ---> Add external archiver...Select the JDBC driver and click OK.
My project list:
3. The driver has been imported. Let's writeProgramVerify
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 two parameters: 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 . 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 = Connect. preparestatement ( " Insert into user values (?,?) " );
For ( Int I = 0 ; I < Num; I ++ ) // Define a 100 cycle and insert 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
Mysql> select * from user; // view all information of the current table (User)
note : if you cannot connect to your database, check whether the driver, user name, password, table, and other information are correct in Code , do not copy others' Code directly.