My environment: Mysql:mysql-essential-5.1.51-win32
Preparation before installation:
JDBC Driver
Eclipse: Any version, free, can Baidu to.
1.MySQL Installation
Here's how to create a data:
Mysql>create DATABASE test; Create a database
mysql>use test;//specify that test is the database
mysql>create TABLE User (name VARCHAR) that is currently being manipulated, password VARCHAR ()); Create a table user and set two fields.
Mysql>insert into user VALUES (' Huzhiheng ', ' 123456 ');//Insert a piece of data into a table
2. Open Eclipse, create a project (my),
Action: Right-click my--->build Path--->add external archiver ... Select JDBC Driver, click OK.
List of my projects:
3. The driver has been imported, let's write a program to verify
import java.sql.*; public class Mysqljdbc {public static void main (String args[]) {try {class.forname ("com.mysql.jdbc.Driver");
Load 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 following 2 parameters are login username and password System.out.println ("Success connect MySQL server!");
Statement stmt = Connect.createstatement ();
ResultSet rs = stmt.executequery ("SELECT * from user");
User is the name of your table while (Rs.next ()) {System.out.println (rs.getstring ("name"));
The catch (Exception e) {System.out.print ("Get Data error!");
E.printstacktrace (); }
}
}
Click to run the program:
Success Loading Mysql driver!
The above results show that you are connected to the database successfully.
4. You can view the contents of MySQL, then we are not want to insert data in MySQL.
In the following example, insert 100 data into the MySQL user table
Import java.sql.*; public class Myjproject {public static void main (String args[]) {try {class.forname ("com.mysql.jdbc.Drive R ");
Load 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++)//define a 100-time loop, insert 100 messages into the table.
{statement.setstring (1, "Chongshi" +i);
Statement.setstring (2, "Bo" +i);
Statement.executeupdate (); ///\ catch (ClassNotFoundException e) {//TODO auto-generated catch block//SYSTEM.OUT.PRINTLN (' An error has
occurred: "+e.tostring ());
E.printstacktrace (); }catch(SQLException e)
{
}
}
}
5. Below we open the MySQL database for viewing
Mysql> show tatabases; View the database
mysql> use test; Make test the current database you want to manipulate
mysql> show tables;//view all Table View Sourceprint for the current database
?
Note: If you can not connect your database, please check your code, driver, username, password, table and other information is correct, do not copy other people's code directly over, see also use.
The above is the use of Eclipse to connect the MySQL database all the process, I hope to help you learn.