Use Eclipse to connect to MySQL database _ MySQL

Source: Internet
Author: User
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. 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

Eclipse: any version, free of charge, can be obtained by Baidu.

1. if you do not want to install MySQL, you can refer to the text here for details: http://www.bitscn.com/article/23876.htm.

Create a data file as follows:

Mysql> create database test; // CREATE a DATABASE mysql> use test; // specify test as the DATABASE 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.

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. The two parameters are respectively 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 is your table name 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
 
  

5. now let's open the MySQL database for viewing.

Mysql> show tatabases; // view the mysql database> use test; // Set test to the mysql database to be operated on.> show tables; // view all tables in 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.

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.