Originally do not want to write such a simple person article, Baidu search me this title, fully meet the title of a large pile. But I've been following those articles for a long time.
My environment: Mysql:mysql-essential-5.1.51-win32
JDBC Driver
Eclipse: Any version, free, can Baidu to.
1.MySQL installation, will not be friends can see the text here to explain http://www.jb51.net/article/23876.htm, very detailed.
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 (20)); 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.
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"))
;
}
catch (Exception e) {
System.out.print ("Get Data error!");
E.printstacktrace ();
}
Click to run the program:
Success Loading Mysql driver!
Success Connect Mysql server!
Huzhiheng
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.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");
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 O
Ccurred: "+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 to operate
mysql> show tables;//view all Table View Sourceprint for the current database
?
mysql> Select *from User; View all information for the current table (user)
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.