Operations Summary/Metabase driver for Eclipse connection to my SQL database

Source: Internet
Author: User
Tags stmt metabase

Actions that eclipse connects to the MySQL database

(Self-test, start learning Eclipse (my eclipse version is the 4.5.2,JDBC drive Jar package version is 5.1.7, pro-test can be used) when connected to the database, found that there are a lot of experience online, but found that there are many errors on the Internet, so published this blog, Hope to be helpful to everyone)

1: Preferred you need to download the JDBC Drive (mysql-connector-java-5.1.7-bin.jar) This file

(Baidu Cloud Disk http://pan.baidu.com/s/1hs9DxS4, password c22r)

Download the above file is also very bumpy ah, so for your convenience, hereby share.

2: Once you have downloaded the above JDBC Drive, you are ready to start the operation.

First open eclipse, create a project,

My project's name is demo, right click on Project demo, then new Find Floder, then

Then paste the downloaded JDBC drive under this Lib directory, then click on the jar that you just pasted and find the build path to continue to find the add to build path,

The results appear as shown, and after the addition is complete, you can use Eclipse to connect to my SQL database.

The connection database code is as follows (it is important to note that connection connect=drivermanage.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "password") )

The "password" in the above sentence is your database's own password, you need to modify it, the above "jdbc:mysql://localhost:3306/test" inside of the test is their own use of MySQL created a table, is created by themselves, need extra attention; (Create my SQL statement will be found elsewhere in this blog, stay tuned)

1PackageCom.ningmengxueyuan;23Import java.sql.*;45PublicClassmysqljdbc{67PublicStaticvoidMain (String args[]) {8Try{9 Class.forName ("Com.mysql.jdbc.Driver");//Load MySQL JDBC driver10//Class.forName ("Org.gjt.mm.mysql.Driver");System.out.println ("Success Loading Mysql driver!");12}Catch(Exception e) {System.out.print ("Error Loading Mysql driver!");14E.printstacktrace ();15}16Try{Connection connect =Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test1", "root", "123456");19//Connection URL is Jdbc:mysql//Server address/database name, the following 2 parameters are login username and password, respectively20System.out.println ("Success Connect Mysql server!");Statement stmt =Connect.createstatement ();ResultSet rs = stmt.executequery ("SELECT * from User");24//user for the name of your table 25 while (Rs.next ()) {26 System.out.println (rs.getstring ("name" 27 } 28}catch (Exception e) {29 System.out.print (" Get Data error! " 30  E.printstacktrace (); 31 } 32 33 } 34 35}  

The above is a beginner's time configuration, the following is their own proficiency in the application configuration, can run, pro-Test.

The above is also the configuration operation that eclipse connects to the MySQL database, and later learns another configuration method, as follows. Pro-Test can, hereby share

1: First of all, create yourself a Java project, create it yourself, omit it, then right-click on the Java project you just created, find the build path, and find the Configure build path, as

2: Click on the Configure Build path above to open the page as shown, then click Add External JARs ...

3: Click Add External JARs ... Then find your downloaded MySQL drive Click to open it

4: Then if you have added the MySQL driver, then click OK

5: After you have created a good Java project, you can see that the import has been successful, the driver

6: This code can be copied and pasted, you need to be aware of the package name, Java file name, MySQL account and password, MySQL database name, data table name, data field name must be consistent with their own needs to create.

1PackageCom.ningmeng;2Import java.sql.*;3PublicClassTest {45PublicStaticvoidMain (String args[]) {6Try{7 Class.forName ("Com.mysql.jdbc.Driver");//Load MySQL JDBC driver8//Class.forName ("Org.gjt.mm.mysql.Driver");9 System.out.println ("Success Loading Mysql driver!");10}Catch(Exception e) {System.out.print ("Error Loading Mysql driver!");12E.printstacktrace ();13}14Try{Connection connect =Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "123456");17//Connection URL is Jdbc:mysql//Server address/database name, the following 2 parameters are login username and password, respectively18System.out.println ("Success Connect Mysql server!");Statement stmt =Connect.createstatement ();ResultSet rs = stmt.executequery ("SELECT * from Users");22//user for the name of your table 23 while (Rs.next ()) {24 System.out.println (rs.getstring ("username") Span style= "color: #000000")); 25 } 26}catch (Exception e) {27 System.out.print (" Get Data error! " 28  E.printstacktrace (); 29 } 30 31 } 32 33}  

7: Run to see your own written judgment statement, the database driver loaded successfully and the database connection was successful, and then output their own query fields.

Operations Summary/Metabase driver for Eclipse connection to my SQL database

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.