JDBC is a set of database programming APIs available in Java that defines a standard Java class library (located in the java.sql and javax.sql packages) to access the database. With JDBC, we can write programs in Java, make connections to specific databases, send SQL statements to the database, implement specific operations on the database, and process the results returned by the database.
JDBC Programming Steps:
Step one: Select the JDBC driver type according to the database used by the application;
Step two: Connect to the database, get connection object;
Step three: Create statement objects through connection;
Step four: Submit the SQL statement using the statement object;
Step five: operation result set;
Step SIX: Reclaim database resources.
The JDBC driver is divided into four types:
The Jdbc-odbc Bridge:jdbc-odbc Bridge is a common driver for sun, with access to a wide range of databases, but with very little efficiency.
Native-api Partly-java driver: Local library Java driver execution is efficient, the client must install local driver, maintenance is inconvenient.
Net-protocal All-java Driver (JDBC Proxy) network protocol Pure Java driver (generic) client does not have to install a local library, and is easy to use, but with relatively low performance.
Native-protocal All-java Driver: Local protocol A full Java driver translates JDBC calls into a network protocol for a specific database, which is highly efficient.
Load the driver by calling the class's static method Forname () to explicitly load the driver: for example:
Class.forName (Driver Class);
Let's look at a piece of code so we know how to make a database connection:
Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;/*** JDBC Test * *@authorAdministrator **/ Public classJDBCDemo3 { Public Static voidMain (string[] args) {Connection conn=NULL;//Connection ObjectStatement stmt =NULL;//Statement Object /*Load Driver*/ Try{class.forname ("Com.mysql.jdbc.Driver"); } Catch(ClassNotFoundException e) {e.printstacktrace (); } /*Create a Connection object*/ //Connection StringString url = "Jdbc:mysql://localhost:3306/jdbc"; //User nameString user = "root"; //PasswordString password = "123456"; Try { //Create a connectionconn =drivermanager.getconnection (URL, user, password); } Catch(SQLException e) {e.printstacktrace (); }
/* Do is to add multiple data to the database. * /Try{stmt=conn.createstatement (); Stmt.addbatch ("INSERT into Users VALUES (5, ' tom1 ', ' 000000 ')"); Stmt.addbatch ("INSERT into Users VALUES (6, ' tom2 ', ' 000000 ')"); Stmt.addbatch ("INSERT into Users VALUES (7, ' tom3 ', ' 789000 ')"); Stmt.addbatch ("INSERT into Users VALUES (8, ' tom4 ', ' 456000 ')"); Stmt.addbatch ("INSERT into Users VALUES (9, ' tom5 ', ' 123 ')"); Stmt.executebatch (); } Catch(SQLException E1) {e1.printstacktrace (); } /*freeing resources: Must-do*/ Try { if(stmt! =NULL) Stmt.close (); } Catch(SQLException e) {e.printstacktrace (); } finally { if(Conn! =NULL) Try{conn.close (); } Catch(SQLException e) {e.printstacktrace (); } } }}
Java Database programming