Java JDBC Technology can be used in JSP to implement queries, modifications, and deletions of table records in the database. JDBC technology occupies a very important position in JSP development.
JDBC (Java Database Connectivity) is the Java DB Connection API. To put it simply,
JDBC can accomplish three things:
(1) Establish a connection with a database,
(2) Sending SQL statements to the database,
(3) processing the results returned by the database.
A common way to establish a connection between JDBC and a database is to establish a JDBC─ODBC bridge. Because of the widespread use of ODBC drivers, the creation of such a bridge makes JDBC capable of accessing almost
There are types of databases. JDBC can also directly load the database driver to access the database,
If you use the Jdbc─odbc Bridge to access the database, you must set up the data source beforehand.
1 Data sources
Use the system to establish an ODBC data source.
2.JDBC-ODBC Bridge Connector
Set up a JDBC─ODBC bridge, that is, load the bridge driver.
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
Class is a class in the package Java.lang that can establish JDBC-ODBC bridges by calling its static method forname.
3 Query Records
(1) Connecting to the database
You first use the connection class in the package java.sql to declare an object, and then use the static method getconnection the class DriverManager call it to create the connection object:
This column more highlights: http://www.bianceng.cn/webkf/JSP/
Connection con = drivermanager.getconnection ("JDBC:ODBC: Data Source name", "Login name", "password");
(2) Send SQL statements to the database.
First use Statement to declare an SQL statement object, and then create the SQL statement object by calling Method Createstatment () with object con of the connection database just created.
Statement sql=con.createstatement ();
(3) Processing query results
With the SQL statement object, the object can invoke the appropriate method to implement queries and modifications to the tables in the database. and store the results of the query in a ResultSet class declaration object,
That is, the SQL statement's query operation on the database returns a ResultSet object:
ResultSet rs=sql.executequery ("SELECT * from Performance form");
The ResultSet object is composed of data rows organized in a unified form. The ResultSet object can see only one row of data at a time, using the next () method to go to the next row of data, after obtaining a row of data,
The ResultSet object can use the Getxxxx method to get the field value, and the index of the position (the first column uses 1, the second column uses 2, and so on) or the field name is passed to the Getxxxx method's arguments
The next () method that uses result set results can be a sequential query. A result set positions the cursor first in front of the first row, and the next () method is invoked the first time to move the cursor to the first row.
The next () method returns a Boolean data that returns FALSE when the cursor moves to the last row.
Scrollable result set:
Statement stmt=con.createstatement (int type, int concurrency);
stmt returns the result set of the corresponding type, depending on the value of the parameter's type and concurrency:
The value of type determines how to scroll, and the value can be:
Resultset.type_forword_only: The cursor of the result set can only scroll down.
Resultset.type_scroll_insensitive: The cursor of the result set can move up and down, and the current result set is unchanged when the database changes.
Resultset.type_scroll_sensitive: Returns a scrollable result set in which the current result set changes synchronously when the database changes.
Concurrency value determines whether the database can be updated with the result set, concurrency value:
Resultset.concur_read_only: Cannot update a table in the database with a result set.
Resultset.concur_updatetable: Can update the tables in the database with the result set.
Scrolling queries often use the following methods of ResultSet:
public Boolean Previous (): Moves the cursor up, which returns a Boolean data that returns False when moved to the first row of the result set.
public void Beforefirst (): Moves the cursor to the initial position of the result set, before the first row.
public void Afterlast (): Moves the cursor after the last row of the result set.
public void First (): Moves the cursor to the top row of the result set.
public void Last (): Moves the cursor to the final row of the result set.
public boolean isafterlast (): Determines whether the cursor is after the last row.
public boolean Isbeforefirst (): Determines whether the cursor is before the first row
public boolean Iffirst (): Determines whether the cursor points to the first row of the result set.
public boolean islast (): Determines whether the cursor points to the last row of the result set.
public int GetRow (): Gets the row number of the current cursor, the line number starts at 1, and returns 0 if the result set has no rows
public boolean absolute (int row): Moves the cursor to the line number specified in the argument row.
Note: If the row takes a negative value, it is the number of lines that count backwards, absolute (-1) is moved to the last line, and absolute (-2) represents the 2nd line. When you move to the front of the first line or
After the last line, the method returns false.