Connect to MySQL database via eclipse

Source: Internet
Author: User
Tags stmt

My environment: Mysql:mysql 5.7.17

JDBC Driver: Mysql-connector-java-5.1.45-bin.jar

Eclipse: Any version, free, can Baidu's to.

1. MySQL installation, no friends can see the connection: http://www.duote.com/tech/1/2430_1.html

Here's how to create a data:

Mysql>create   DATABASE test;   Create a database

Mysql>use test; Specify test as the current database to be manipulated

Mysql>create TABLE User (name varchar, password varchar); Create a table user, 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),

Action: Right-click my--->build Path--->add external archiver ... Select the JDBC driver and click OK.

List of my projects:

3. Driver has been imported, let's write a program to verify

Import java.sql.*;
Publicclass MYSQLJDBC {
Publicstaticvoid 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 the jdbc:mysql//server address/database name, and the following 2 parameters are login username and password, respectively

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 results above show that you are connected to the database successfully.

4。 Can see the contents of MySQL, then we want to insert data into MySQL. In the following example, insert 100 data into the user table of MySQL
Import java.sql.*;

Publicclass Myjproject {
Publicstaticvoid 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 loop of 100 times and 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 databases;  //查看所数据库
mysql> use  test;    //使test为当前要操作的数据库
mysql> show tables; //查看当前数据库的所有表
mysql> select *from user;  //查看当前表(user)的所有信息

Note : If you do not connect to your database, please check your code, the driver, user name, password, table and other information is correct, do not copy the other people's code directly over, see also do not look at the use.

Connect to MySQL database via eclipse

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.