what is JDBC?Java Data Base Connectivity
JDBC is:
The APIs that access the database in a uniform way can access any type of table column data, especially data stored in relational data. JDBC represents a Java database connection.
JDBC provides:
1. Platform-Independent database access
2. Transparent position
3, the problem of the proprietary data is transparent
jdbc and JDBC DriversJDBC Drivers is a class that implements database operations
Drivers is divided into two categories: 2-tier-customer direct and database sessions
3-tier-middle-tier session for customer and representative database
How do I use JDBC? ①: Loading the JDBC driver (different database loaded drivers are not the same)
②: Establishing a connection
③: Creating statement objects
④: Result set processing
⑤: Close ConnectionAfter the operation is complete, all the JDBC objects used are closed to release the JDBC resource, and the order of closing and declaration is reversed:
1. Close record set
2. Closing the statement
3. Close the Connection object implemented by the static method forname (String className) of the Java.lang.Class class
An instance of a direct connection to data, such as:
Import java.sql.*;...try{//Load Pointbase driver class, here can be changed to oracle,mysql and other types of database, After a successful load, an instance of the driver class is registered to the DriverManager class Class.forName ("Com.pointbase.jdbc.jdbcUniversalDriver"); Create a connection to the database, to connect to the database, you need to request to DriverManager and get the Connection object, which represents a connection to the data, using the DriverManager Getconnectin method to pass in the path of the specified database to connect to. Connection con=drivermanager.getconnect ("jdbc:pointbase://server:port/mydatabase");//Create a statementstatement stmt =con.createstatement (); Executes the SQL statement that queries the database, returning a result set object string sql= "select * from MyTABLE"; ResultSet res=stmt.executequery (SQL);//processing results//Two cases: 1, performing the update returns the number of records affected by this operation; 2. The result returned by the execution query is a ResultSet object. //ResultSet contains all rows that conform to the conditions in the SQL statement, and it provides access to the data in those rows through a set of Get methods. <span style= "White-space:pre" ></span>//gets the data using the access method of the result set (ResultSet) object: While (Res.next ()) { String col1=res.getstring ("MYCOLUMN1"); int col2=res.getint ("MYCOLUMN2"); } catch (Exception e) {...}}
The JDBC API supports both the two-tier model of database access (c/s) and the three-tier model (b/s), in which Java applets or applications will directly interact with the database. This will require a JDBC driver to communicate with the specific database management system you are accessing
In a three-tier model, the command is first sent to the "middle tier" of the service and then sent to the database by the SQL statement.
The disadvantage of JDBC: Although JDBC is unified at the Java language level, there are still many differences between databases. In order to achieve a better cross-database operation, the Hibernate project was born, Hibernate is the re-encapsulation of JDBC, the implementation of the database operation of a broader unified and better portability.
JDBC Database connection