How to Use Eclipse to connect to the MySQL database)

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.

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, will not friends can see the connection: http://www.duote.com/tech/1/2430_1.html

Create a data file as follows:

 
 
  1. Mysql> create database test; // CREATE a DATABASE
  2. Mysql> use test; // specify test as the database to be operated
  3. Mysql> create table user (name VARCHAR (20), password VARCHAR (20); // CREATE a TABLE user and set two fields.
  4. 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.

My project list:

3. the driver has been imported. Let's write a program to verify it.

 
 
  1. Import java. SQL .*;
  2. Public class MysqlJdbc {
  3. Public static void main (String args []) {
  4. Try {
  5. Class. forName ("com. mysql. jdbc. Driver"); // load the mysql jdbc Driver
  6. // Class. forName ("org. gjt. mm. mysql. Driver ");
  7. System. out. println ("Success loading Mysql Driver! ");
  8. }
  9. Catch (Exception e ){
  10. System. out. print ("Error loading Mysql Driver! ");
  11. E. printStackTrace ();
  12. }
  13. Try {
  14. Connection connect = DriverManager. getConnection (
  15. "Jdbc: mysql: // localhost: 3306/test", "root", "198876 ");
  16. // The Connection URL is jdbc: mysql // server address/database name, followed by the login user name and password
  17.  
  18. System. out. println ("Success connect Mysql server! ");
  19. Statement stmt = connect. createStatement ();
  20. ResultSet rs = stmt.exe cuteQuery ("select * from user ");
  21. // User: name of your table
  22. While (rs. next ()){
  23. System. out. println (rs. getString ("name "));
  24. }
  25. }
  26. Catch (Exception e ){
  27. System. out. print ("get data error! ");
  28. E. printStackTrace ();
  29. }
  30. }
  31. }

Click Run program:

 
 
  1. Success loading Mysql Driver! 
  2. Success connect Mysql server! 
  3. 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.

 
 
  1. Import java. SQL .*;
  2.  
  3. Public class Myjproject {
  4. Public static void main (String args [])
  5. {
  6. Try {
  7. Class. forName ("com. mysql. jdbc. Driver"); // load the mysql jdbc Driver
  8. // Class. forName ("org. gjt. mm. mysql. Driver ");
  9. System. out. println ("Success loading Mysql Driver! ");
  10. }
  11. Catch (Exception e ){
  12. System. out. print ("Error loading Mysql Driver! ");
  13. E. printStackTrace ();
  14. }
  15. Try {
  16. Connection connect = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test", "root", "198876 ");
  17. Int num = 100;
  18. PreparedStatement Statement = connect. prepareStatement ("insert into user VALUES (?,?) ");
  19. For (int I = 0; I <num; I ++) // defines a 100 cycle and inserts one hundred pieces of information into the table.
  20. {
  21. Statement. setString (1, "chongshi" + I );
  22. Statement. setString (2, "bo" + I );
  23. Statement.exe cuteUpdate ();
  24. }
  25.  
  26. //} Catch (ClassNotFoundException e ){
  27. // TODO Auto-generated catch block
  28. // System. out. println ("An error has occurred:" + e. toString ());
  29. // E. printStackTrace ();
  30. } Catch (SQLException e)
  31. {
  32. }
  33. }
  34. }

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

 
 
  1. Mysql> show tatabases; // view the database
  2. Mysql> use test; // set test as the database to be operated
  3. Mysql> show tables; // view all tables of the current database
  4. View sourceprint?
  5. 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.

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.