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 ();
}