A detailed explanation of how to use the JSP database connection class

Source: Internet
Author: User
Tags anonymous odbc null null

DriverManager class

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

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.