First, the most straightforward definition of JDBC:
the technique of sending SQL statements using Java Code (Programs) is the jdbc technology
Ii. Prerequisites for JDBC to send SQL
To connect to the database server:
- The IP address of the database
- Port
- Database user Name
- Password
/*** JDBC Connection database *@authorAPPle **/ Public classDemo1 {//URL of the connection database PrivateString url = "Jdbc:mysql://localhost:3306/day01"; //JDBC Protocol: Database sub-protocol: Host: Port/Connected Database// PrivateString user = "root";//User name PrivateString password = "root";//Password /*** First Method *@throwsException*/@Test Public voidTest1 ()throwsexception{//1. Creating a Driver class objectDriver Driver =NewCom.mysql.jdbc.Driver ();//New version//Driver Driver = new Org.gjt.mm.mysql.Driver ();//older versions//set user name and passwordProperties props =NewProperties (); Props.setproperty ("User", user); Props.setproperty ("Password", password); //2. Connect to the database and return to the Connection objectConnection conn =driver.connect (URL, props); SYSTEM.OUT.PRINTLN (conn); } /*** Use the Driver Manager class to connect to the database (registered two times, not necessary) *@throwsException*/@Test Public voidTest2 ()throwsexception{Driver Driver=NewCom.mysql.jdbc.Driver (); //Driver driver2 = new Com.oracle.jdbc.Driver (); //1. Registering a driver (multiple drivers can be registered)drivermanager.registerdriver (driver); //Drivermanager.registerdriver (DRIVER2); //2. Connect to a specific databaseConnection conn =drivermanager.getconnection (URL, user, password); SYSTEM.OUT.PRINTLN (conn); } /*** (This is the recommended way to connect to the database) * recommended to use the load driver class to register the driver *@throwsException*/@Test Public voidTest3 ()throwsexception{//Driver Driver = new Com.mysql.jdbc.Driver (); //registers a driver by loading a static block of code by getting a byte-code objectClass.forName ("Com.mysql.jdbc.Driver"); //Driver driver2 = new Com.oracle.jdbc.Driver (); //1. Registering a driver (multiple drivers can be registered)//drivermanager.registerdriver (driver); //Drivermanager.registerdriver (DRIVER2); //2. Connect to a specific databaseConnection conn =drivermanager.getconnection (URL, user, password); SYSTEM.OUT.PRINTLN (conn); }}
Third, the API of the JDBC interface core
|-Driver Interface: Represents the Java driver interface. All the specific database vendors want to implement this interface. |-Connect (URL, properties): A way to connect to a database. URL: URL URL syntax for connection database: JDBC Protocol: Database Sub-protocol://HOST: Port/Database User: Username of database Password: Database user password |-DriverManager class: Driver manager class for managing all registered drivers |-registerdriver (driver): Register the Driver class object |-connection getconnection (Url,user,password); Get Connection Object |-Connection interface: A Connection object that represents a Java program and database. |-Statement createstatement (): Creating Statement objects |-PreparedStatement preparestatement (String sql) creating PreparedStatement objects |-callablest Atement preparecall (String sql) Create CallableStatement Object |-statement interface: For executing static SQL statements | |-int EX Ecuteupdate (String sql): Execute static Update SQL statement (DDL,DML) |-ResultSet executeQuery (String sql): Static query SQL statement executed ( DQL) |-preparedstatement interface: Used to execute precompiled SQL statements |-int executeupdate (): Perform Precompiled update SQL statement (DDL,DML) |-resultset executeQuery (): Perform pre-compilation Query SQL statement (DQL) |-callablestatement interface: SQL statement for execution of stored procedures (call XXX) |-resultset ex Ecutequery (): How to call a stored procedure |-resultset interface: Used to encapsulate the data of a query |-Boolean next (): Move the cursor to the next line |-GETXX (): Gets the value of the column
Getting Started with Jdbc