Esclipse connecting MySQL Database

Source: Internet
Author: User

How to connect to the database in the Eclipse development environment and test the connection success

1) There is no integrated MySQL driver in the Eclipse development environment, you need to download the connection driver Mysql-connector-java-xx-xx-xx.zip from the following address:

Http://dev.mysql.com/downloads/connector/j

2) Extract, just take the file Mysql-connector-java-xx.xx.xx-bin.jar, reference it to the project you need to connect to the MySQL database, Such as: I built a test database connection in the Eclipsel project Conmysql. Here's how:

Right-click on the project Conmysql

Properties->java Build path->libraries

Click Add External JARS ...

Select the Mysql-connector-java-xx.xx.xx-bin.jar after decompression

2) The driver has been imported, let's write a program to verify

 Packagetest1;ImportJava.sql.*; Public classMYSQLJDBC { Public Static voidMain (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/test1", "root", "Aa-12345"); //connection URL is jdbc:mysql//server address/database name, the following 2 parameters are login username and password, respectivelySystem.out.println ("Success Connect Mysql server!"); Statement stmt=connect.createstatement (); ResultSet RS= Stmt.executequery ("Select * from Stu"); //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 Run Program: The following code appears to indicate a successful connection

Success Loading Mysql driver!
Success Connect Mysql server!
NPU (what you write in MySQL)

Esclipse connecting MySQL Database

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.