JSP various database connection class collection (1/4)

Source: Internet
Author: User
Tags anonymous odbc null null stmt port number oracle database

JSP connection oracle8/8i/9i database (in thin mode)

This class is used to load the driver, all of its members are static members, so there is no need to instantiate it in the program and access it directly through the class name.
The DriverManager class is a JDBC management layer that acts to load drivers between users and drivers
Class.forName ("Company name. Database name. Driver name")
such as: Class.forName ("Sun.jdbc.odbc.jdbcOdbcDriver")
Establish a connection
After loading the driver class and registering with the DriverManager class, you can use it to establish a connection to the database. When the call to Driver.Manager.getConnection () makes a connection request, DriverManager checks each driver to see if it can establish a connection.
Method: Connection getconnection (String url,string user,string password)
Where user and password are users and passwords that log on to the database
The first parameter is the URL that points to the database, which is formatted as follows:
JDBC: (subprotocol):(subname)
Subprotocol: A child protocol that specifies which database to connect to or how to connect to the database
SubName: Establish a connection that can be a data source name or point to an online database
For example: The following are examples of commonly used drivers (JDBC-ODBC bridge drivers) and a student data source with anonymous logins:
Class.forName ("Sun.jdbc.odbc.jdbcOdbcDriver");//Load Driver
String url= "Jdbc:odbc:student";
Connection cn=drivermanager.getconnection (URL, "anonymous", "");
Getconnection (): Returns a Connection class object. If successful, this object points to a connection to this database; otherwise, this object will be null NULL
Connection class
The connection class is a connection that points to the same database.
Role: Manage connections to the database, such as: queries that send queries to the database and query results from the receiving database are based on it, and the connection is closed after all tasks that have connected to the database have been completed.
Method:
Statement createstatment (): Creates a new Statement object that can send query information to the database
void Close (): Closes the connection to the database and frees the occupied JDBC resource
Boolean Isclose (): Determines whether the database is still connected
Statement class
Function: The statement object is used to send SQL statements to the database.
Creating statement objects
Once you have established a connection to a specific database, you can use the connection to send the SQL statement. The statement object is created using the connection method createstatement.
Connection cn=drivermanager.getconnection (Rul, "Sunny", "");
Statement stmt=cn.createstatement ();
In order to execute the statement object, the SQL statement sent to the database will be supplied as a parameter to the statement method
ResultSet rs=stmt.executequery ("Select A,b,c from table2");
Executing statements using the statement object
The statement interface provides three ways to execute SQL statements
ExecuteQuery (): statement used to produce a single result set, such as: SELECT statement
Executeupdate (): For INSERT, update, or delete, statement, and so on, the return value is an integer indicating the number of rows affected (that is, the update count)
Execute (): Used to execute statements that return multiple result sets, multiple update counts, or combination of both
Statement completion
Statement is considered complete when it is executed and all results are returned.
For the ExecuteQuery () method that returns a result set, the statement completes after retrieving all rows of the ResultSet object.
For method Executeupdate (), the statement completes when it executes
In the case of a few calls to execute (), the statement completes only after retrieving all the result sets or the update count that it generates
Close Statement Object
The statement object will be automatically closed by the Java garbage collector. But we'd better show them off because the data management system resources will be released immediately, helping to avoid potential memory problems.
Main methods
ResultSet executequery (String sql): Returns a static SQL query result
int executeupdate (String sql): Query the number of inserts, update, deletes, or return 0 in a row of SQL declarations
void Close (): Closes the connection to the database and the JDBC resources it occupies
ResultSet class (record set)
Role: Load query results, and can extract the results of the query through its different methods. ResultSet contains all the 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 that can access different columns in the current row.
Resultset.next (): Moves the record pointer to the next line in the ResultSet record set to make it the current row.
Note: A record set is a two-dimensional table with the column headings and corresponding values returned by the query.
For method Executeupdate (), the statement completes when it executes
In the case of a few calls to execute (), the statement completes only after retrieving all the result sets or the update count that it generates

Testoracle.jsp is as follows: <%@ page contenttype= "text/html;charset=gb2312"%>

The code is as follows Copy Code

<%@ page import= "java.sql.*"%>
<%
String result = ""; Query result string

String sql = "SELECT * from Test"; SQL string

Connection string, format: "JDBC: Database driver Name: Connection mode: @ Database server IP: Port number: Database Sid"
String url = "Jdbc:oracle:thin: @localhost: 1521:ORCL";
String username = "Scott"; User name
String password = "Tiger"; Password

To create an Oracle database driver instance
Class.forName ("Oracle.jdbc.driver.OracleDriver"). newinstance ();

Get a connection to a database
Connection conn = drivermanager.getconnection (URL, username, password);
To create an execution statement object
Statement stmt = Conn.createstatement ();
Execute SQL statement, return result set
ResultSet rs = stmt.executequery (SQL);

while (Rs.next ())
{
result = = "N/a first field content:" + rs.getstring (1) + "<BR>";
}

Rs.close (); Close result set
Stmt.close (); Close Execution Statement Object
Conn.close (); Close the connection to the database
%>

<HTML>
<BODY>
<%=result%>
</BODY>
</HTML>

home 1 2 3 4 last page
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.