Jdbc:java Data Base Connection
JDBC is used to run SQL statements and obtain new new Java APIs from the database.
JDBC is very important to (let our program) operate the database through the network, and JDBC technology is one of the core technologies of Java.
is the preferred way to access the database using the JDBC driver
Working with the database through JDBC-steps:
1th Step: Manual Drive (only once)
2nd Step: Establish a connection (Connection)
3rd Step: Create a statement that runs SQL (Statement)
4th step: Run the statement
5th step: Processing Running results (RESULTSET)
6th step: Freeing up resources
Use JDBC First step: Load Driver
There are three ways to drive a brochure:
1. Class.forName ("Com.mysql.jdbc.Driver");
This is not a recommended way to rely on a detailed driver class.
2. Drivermanager.registerdriver (Com.mysql.jdbc.Driver);
will be dependent on the detailed driver class.
3. System.setproperty ("Jdbc.drivers", "Driver1:driver2");
Although it is not dependent on the detailed driver class, it is not convenient to register. So very little use
Use JDBC Step two: Establish a connection
A connection is established through connection, and connection is an interface class. Its function is to connect to the database (session).
To establish an connection interface class object:
Connection conn =drivermanager.getconnection (URL, user, password);
The format requirements for URLs are:
JDBC: Sub-Protocol: Sub-name//hostname: port/database name? Property name = attribute value &.
such as: "Jdbc:mysql://localhost:3306/test"
User is the username of the login database, such as root
Password is the password for the login database, empty to fill ""
Use JDBC Step three: Create a Running object
Running the object statement is responsible for running the SQL statement. Generated by the connection object.
Statement st = Connection.createstatement ();
The statement interface class also derives two interface classes PreparedStatement and CallableStatement, both of which provide us with more powerful data access capabilities.
PreparedStatement is able to precompile SQL statements, which prevents SQL injection from improving security.
PreparedStatement ps=connection.preparestatement ("Update user set id=?") where username=? "); ———— SQL statements mean? As a wildcard, the value of the variable is set by the parameter: Ps.setobject (1, object);
And the precompiled results can be stored in the PreparedStatement object. Improves efficiency when running SQL statements more than once.
As a subclass of statement, PreparedStatement inherits all the functions of statement.
CallableStatement interface
The CallableStatement class inherits the PreparedStatement class, which is primarily used to run SQL stored procedures.
Running SQL stored procedures in JDBC needs to be escaped.
The JDBC API provides an escape syntax for a SQL stored procedure:
{call<procedure-name>[<arg1>,<arg2>, ...]}
Procedure-name: Is the name of the SQL stored procedure that you want to run
[<arg1>,<arg2>, ...] : The number of parameters required for the corresponding SQL stored procedure
Use JDBC Step Fourth: Run SQL statements
Running the object statement or PreparedStatement provides two frequently used methods to run SQL statements.
ExecuteQuery (Stringsql), which is used to run SQL statements that implement query functionality. The return type is resultset (result set).
such as: ResultSet RS =st.executequery (SQL);
Executeupdate (Stringsql), which is used to run SQL statements that implement add, delete, and change functions, with the return type int, which is the number of rows affected.
such as: int flag = st.executeupdate (SQL);
Using JDBC Step Fifth: Working with running results
ResultSet Object
The ResultSet object is responsible for saving the query results generated after the statement run.
The result set resultset is manipulated by cursors.
A cursor is a controllable pointer that can point to a random record.
With this pointer we can easily point out which of the records in the result set we want to change, delete, or which record to insert the data before. Only one cursor is included in a result set object.
In addition, with the help of Resultsetmeta, we can find out the structure information of the data table.
ResultSetMetaData rsmd= resultset.getmetadata ();
Using JDBC Step Sixth-releasing resources
See essay: Java MySQL database link and resource shutdown
The database resource does not shut down, its occupied memory will not be freed, the consumption of resources, affect the system.
Article changed from: learn Java JDBC, see this is enough
Java JDBC Programming process steps