About eclispse connecting to MySQL JDBC

Source: Internet
Author: User

1. Create a new Java project in eclipse

2. Introduction of the JDBC Library (in the extenrnal of Bulid Path)

3.

1) Import SQL Package (import java.sql.*)

2) load (register) MySQL JDBC driver

Class.forName ("Com.mysql.jdbc.Driver");

3) connection database (URL)

Connection Connect=drivermanager.getconnection (
"Jdbc:mysql://localhost:3306/test", "root", "123");

Connection Connect=drivermanager.getconnection (
"Jdbc:mysql://localhost:3306/test");

Ok..

Hierarchical structure:

Application Layer---JDBC interface layer---JDBC Driver layer---Database

JDBC Interface layer: Provides a unified interface for accessing different databases

JDBC Driver Layer: True interaction of databases by specific database manufacturers

JDBC URL Problem:

Once the JDBC driver is registered, the database connection can be established. However, since many drivers have been registered, DriverManager is required to select the correct driver.

Therefore, JDBC has a dedicated JDBC URL as a self-identification for selection.

As follows:

Try
{

Class.forName ("Com.mysql.jdbc.Driver");
System.out.println ("suceess loading MySQL driver");
}
catch (Exception e)
{
System.out.print ("Eeor loading MySQL driver");
E.printstacktrace ();
}
Try
{
Connection Connect=drivermanager.getconnection (
"Jdbc:mysql://localhost:3306/test", "root", "123");
System.out.println ("sucess Connect Server");
Statement stmt=connect.createstatement ();
ResultSet rs=stmt.executequery ("SELECT * from user");
while (Rs.next ())
{
System.out.println (rs.getstring ("name"));
}
}
catch (Exception e)
{
System.out.print ("Get Data error!");
E.printstacktrace ();
}

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.