Summary of common JDBC interfaces and summary of jdbc

Source: Internet
Author: User

Summary of common JDBC interfaces and summary of jdbc

This article mainly introduces common JDBC interfaces. Let's take a look at the specific content below.

1. Driver Interface

Each database Driver must implement the Driver interface. When writing a program, you need to load the database Driver provided by the database vendor when you need to connect to the database. The loading method is as follows:

Class.forname("jdbc.driver_class_name");

At the same time, you must first introduce the java. SQL package when using Class. forname. The following code loads the SQL Server database DRIVER:

import java.sql.*;Class.forname("com.micsoft.jdbc.sqlserver.SQLServerDriver");

2. DriverManager interface.

The DriverManager class is the management layer of JDBC and acts between users and drivers. The DriverManager class tracks available drivers, establishes connections between the database and the corresponding drivers, and processes transactions such as driver Logon Time Control and login and tracking information display.

Static Conneciton getConnection(String url,String user,String password)

The url has three parts:

jdbc:<subprotocol>:<subname>


Different parts indicate different meanings:

Protocol: jdbc indicates the protocol, which is the only protocol of JDBC.

Sub-Protocol: it is mainly used to identify database drivers. The sub-protocols of different database drivers are different.

Sub-Name: Different proprietary drivers can adopt different implementations.

3. Connection

The Connection object indicates the Connection to the database, that is, the Connection between the loaded Driver and the database. You must create a Conneciton class instance.

Conneciton conn=DriverManager.getConneciton(url,user,password);

4. Statement

Statement allows you to run SQL statements on a base-level Connection. The Connection interface provides the method for generating Statement.

5. ResultSet

After Statement executes the SQL Statement, the result value is returned as a result set. ResultSet is the result set that contains the query results.

I. Example of connecting to Oracle.

Class.forname("oracle.jdbc.driver.OracleDriver").newlntance();String url="jdbc:oracle:thin:@localhost:1521:orcl";Sring user="test";String password="test";Connecion conn=DriverManager.getConnecion(url,user,password);

II. Example of connecting to SQL Server

Class.forname("com.microsoft.jdbc.sqlserver.SQLServerDriver").newlntance();String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";Sring user="ok";String password="";Connecion conn=DriverManager.getConnecion(url,user,password);

Summary

I hope this article will help you with the summary of common JDBC interfaces. If you have any questions, you can leave a message at any time. The editor will reply to you in time! I hope my friends will have a lot of support for helping customers!

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.