Eclipse connection to MySQL database

Source: Internet
Author: User

This year since the beginning of the AS3 development, feel more and more distant from Java, even the basic connection database to check the information ... Do not say, learn an article yourself.

Download and install MySQL before development, installation method: Http://www.duote.com/tech/1/2430_1.html

Once installed, the database can be developed using myeclipse or IntelliJ idea. But before you connect to the database, you need to have a database driver, which is the Java jar package.

You can download the Java MySQL driver package here: http://download.csdn.net/detail/weierbufan/8396207

1. Import the database driver:

Open Eclipse, create a project (my),

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

In the list of items you can see:

2, verify that the database is not already connected available. The procedure is as follows:

ImportJava.sql.*; Public classTestjdb { 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/tx", "root", "126"); //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 MyClass");  while(Rs.next ()) {System.out.println (Rs.getint (1) + "\ T" +rs.getstring (2) + "\ T" +rs.getint (3) + "\ T" +rs.getdouble (4) + "\ T" +rs.getint (5)); }    }    Catch(Exception e) {System.out.print ("Get Data error!");    E.printstacktrace (); }      }}

Running the above program will see the following results:

Indicates that the database has been successfully connected.

Eclipse connection to MySQL 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.