--------------------------------------------------------------------------------------------------------------- ----------
Concept
The JDBC Java database Connectivity provides Java access to various database APIs. The database vendor then completes the excuses.
Several important interfaces in the JDBC API
Drivermanger We are going to connect to one or n databases, Drivermanger is to manage one or more databases, we need to connect, we can take it from here.
Connection provides a way to interact with the database (self-understanding, a connection to the Drivermanger request)
Statement provides SQL-related operations (self-understanding, which is used to manipulate SQL statements)
ResultSet provides the SQL return result set operation (the result of SQL statement execution.) )
SQLException handles exceptions in several operations above.
The concept always finds it hard to understand. I understand that, the wrong place, please point out
Database in the opposite, there is a river in the middle, I want to operate the database, divided into several steps
1. I need to connect to the database, I first ask the database how to connect you. Then the database gives me a drive.
2.MangerDriver dad told me how to fix it, and then I fixed the passage.
3. The bridge is repaired. Find an object and you can work with the database.
4. The object takes me to perform the database operation results
--------------------------------------------------------------------------------------------------------------- ----------
into code
1 //Registration Driver2Class.forName ("Oracle.jdbc.driver.OracleDriver");3 //Create an Introduction4Connectoin connection = Drivermanger.getconnecton ("JDBC:ORACLE.THIN://192.168.93.93:1521/ORCL");5 //gets the object that operates SQL6Statement Statement =connection.createstatement ();7 //Execute SQL statement8ResultSet ResultSet = statament.executeupdate (sql);
--------------------------------------------------------------------------------------------------------------- ----------
Main methods
1. Registration Driver
Drivermanager.registerdriver (new drivermanager.registerdriver ()); This method is not recommended to load the driver Class.forName ( "Oracle.jdbc.driver.OracleDriver");
2.Connection can be obtained by two tired
--drivermanger the connection at the bottom of the java.sql.
--datasource in Javax.sql This container is encapsulated as a data source and then gets the connection. It is better to use this when the traffic is large (actually I do not know I look on the Internet, and finally try)
3.Statement PreparedStatement CallableStatement
The third inherits the second of the second inherits the first one
Statement provides the most basic SQL operation for static SQL statements, and incoming SQL statements cannot accept parameters
PreparedStatement can pass arguments in SQL to use SQL statements more than once, preventing SQL injection
CallableStatement can invoke PL/SQL stored procedures
// url three formats String URL = null ; // 1 url = jdbc:oracle:thin:@//// // 3 URL =jdbc:oracle:thin:@<tnsname>;
// Method // If there is a result return of true, otherwise flase, typically used for drop alter create Boolean Execute (String SQL) // int executeupdate (String SQL)// return a result set result is typically used to query select ResultSet ExecuteQuery (String SQL
Step Create object Execute SQL statement close statement
4.ResultSet
When querying the database, the data returned by the database is obtained through the ResultSet interface. ResultSet internally manages a cursor (cursor) that points to the data that is currently being read,
ResultSet provides the following three types of interfaces:
A cursor movement interface used to manipulate a moving cursor.
Gets the data interface that is used to point to the location from the current cursor
Updates the data interface to update the data that the current cursor points to, and to change the data in the corresponding database.
--------------------------------------------------------------------------------------------------------------- ----------
Case reading data from a single table
Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;ImportOracle.jdbc.driver.OracleDriver; Public classJdbcdemo { Public Static voidMain (string[] args) {//Connection Connection =NULL; Statement Statement=NULL; ResultSet ResultSet=NULL; String URL= "Jdbc:oracle:thin:@//192.168.93.93:1521/center"; String User= "Ghast"; String Password= "7654123"; Try { //1. Registration Driver--Class.forName ("Oracle.jdbc.driver.OracleDriver"); //2. Get a connection to the databaseConnection =drivermanager.getconnection (URL, user, password); //3. Objects that have to manipulate SQLstatement =connection.createstatement (); //4. SQL statement result setResultSet = Statement.executequery ("Select * from User1"); //5. Get query Results while(Resultset.next ()) {System.out.println (Resultset.getobject ("id")); System.out.println (Resultset.getobject ("Name")); System.out.println (Resultset.getobject ("Password")); System.out.println (Resultset.getobject ("Birthday")); System.out.println (Resultset.getobject ("Age")); } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); //listen to people say to close them}finally{ Try { if(Connection! =NULL) {connection.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Connection=NULL; Try { if(Statement! =NULL) {statement.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Statement=NULL; Try { if(ResultSet! =NULL) {resultset.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } ResultSet=NULL; } }}
JDBC Learning Essays