Just finished learning the Java JDBC Connection database and summed up 2 of these methods
The first type:
The code is as follows:
Import java.sql.Connection;
Import java.sql.driver;//
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import java.util.Properties;
Import org.junit.test;//
public class Demo1 {
URL of the connection database
Private String url= "jdbc:mysql://localhost:3306";//Sub-protocol using the JDBC Protocol database: MySQL. Jdbc:mysql: Full coprocessor, host: localhost port: 3306
Private String url= "JDBC:MYSQL://LOCALHOST:3306/DAY17";//Connect to a specific database
Private String user= "root";//user name
Private String password= "root";//Password
@Test
public void Test1 () throws exception{
1. Creating a Driver class object
Driver driver=new com.mysql.jdbc.Driver ();//driver is an implementation class for the Com.mysql.jdbc.Driver interface, which is the driver class that needs to be loaded into the project. (In this project create a folder directory, named Lib (both), the driver (suffix to the jar file) Copy to the Lib, and then right-click on the driver class, point build Path,add to build Path)
Set user name and password:
Properties Pros=new properties ();//Construction Method Properties()
: Creates a list of empty properties without a default value.
Pros.setproperty ("user", user);
/*setproperty Method:
Object |
setProperty(String key, String value) Call the Hashtable method put . |
*/
Pros.setproperty ("password", password);
2. Connect to the database
Connection Conn=driver.connect (Url,pros);
/* Returns the connection interface, which is actually the implementation class that returns the connection interface (returns the interface, and has connection conn= accepted, which is the implementation class that is returned)
Connection |
connect(String url, Properties info) An attempt was made to create a database connection to a given URL. |
*/
SYSTEM.OUT.PRINTLN (conn);//If it is not empty, it indicates a successful connection
}
}
The second type:
Import java.sql.Connection;
Import Java.sql.Driver;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import java.util.Properties;
Import Org.junit.Test;
public class Demo1 {
Private String url= "jdbc:mysql://localhost:3306";
Private String user= "root";
Private String password= "root";
@Test
public void Test2 () throws sqlexception{
1. Creating a Driver class object
Driver driver=new com.mysql.jdbc.Driver ();
Driver driver1=new com.oracle.jdbc.Driver ();//may register multiple drivers
2. Registering drivers
/*
static void |
registerDriver(Driver driver)
DriverManager registers the given driver. |
*/
Drivermanager.registerdriver (driver);
Drivermanager.registerdriver (driver1);//Register multiple drivers
3. Connect to a specific database
/*
static Connection |
getConnection(String url, Properties info) An attempt was made to establish a connection to a given database URL. |
*/
Connection conn= drivermanager.getconnection (url, user, password);
SYSTEM.OUT.PRINTLN (conn);
}
}
Analysis of the second method:
@Test
public void Test2 () throws sqlexception{
1. Creating a Driver class object
Driver driver=new com.mysql.jdbc.Driver ();//Enter New Com.mysql.jdbc.Driver, this is the Driver interface implementation class, view its source code, as follows:
Look at its static code block code, that is, when the driver implementation class new Com.mysql.jdbc.Driver is loaded into the Java virtual machine, it will execute without creating an object.
In code: Java.sql.DriverManager.registerDriver (New Driver ()); This sentence indicates that the implementation class new com.mysql.jdbc.Driver of the driver interface is loaded into the Java Virtual machine, and an object of the implementation class new Com.mysql.jdbc.Driver class of the driver interface is registered.
/*
public class Driver extends Nonregisteringdriver implements Java.sql.Driver {
~ Static fields/initializers
// ---------------------------------------------
//
Register ourselves with the DriverManager
//
static {
try {
Java.sql.DriverManager.registerDriver (New Driver ());
} catch (SQLException E) {
throw new RuntimeException ("Can ' t Register driver!");
}
}
~ Constructors
// -----------------------------------------------------------
/**
* Construct a new driver and register it with DriverManager
*
* @throws SQLException
* If a database error occurs.
*/
Public Driver () throws SQLException {
Required for Class.forName (). newinstance ()
}
}
*/
Driver driver1=new com.oracle.jdbc.Driver ();//may register multiple drivers
2. Registering drivers
/*
static void |
registerDriver(Driver driver)
DriverManager registers the given driver. |
*/
Drivermanager.registerdriver (driver);
Drivermanager.registerdriver (driver1);//Register multiple drivers
3. Connect to a specific database
/*
static Connection |
getConnection(String url, Properties info) An attempt was made to establish a connection to a given database URL. |
*/
Connection conn= drivermanager.getconnection (url, user, password);
SYSTEM.OUT.PRINTLN (conn);
}
The second method is changed (abbreviated (common)):
public void Test2 () throws sqlexception{
Class.forName ("Com.mysql.jdbc.Driver");//code means to get Com.mysql.jdbc.Driver class bytecode object, get this class bytecode object, actually already loaded this class into the Java Virtual machine// Inside//Load a static code block (load this class) by getting a bytecode object to register the driver.
2. Connect to a specific database
Connection conn= drivermanager.getconnection (url, user, password);
SYSTEM.OUT.PRINTLN (conn);
}
. Java is the source file suffix for Java, and the code you write needs to be written in a. java file.
The. Class is a bytecode file, which is a file generated by the. Java source file compiled with the Javac command.
The Java virtual machine is just going to run the. class file to run the program.
JDBC Connection database method and some related knowledge points