0822jdbc--java Connection Database

Source: Internet
Author: User

JDBC Definition:
JDBC Full name: Java DataBase Connectivity
A common interface for SQL database access and operations, independent of specific data management systems, is an interface-oriented programming
JDBC provides different ways to access different databases

Ojdbc6.jar is a required driver package for the JDBC Connection database, and some of the specific methods in this driver package implement the following three interfaces to connect to the database
We refer to this three-interface jar called a database driver package

The connection interface is the interface of a database connection defined in the JDK, and there is no implementation class defined in JAVAAPI that implements the interface, and these implementation classes will be provided by the database vendor, and the same statement, resultset both interfaces are defined in this form. , such programming patterns become interface-oriented programming

A class and three interfaces used primarily by JDBC:
The driver management class, which gets an instance of the connection interface by this class

Connection:Gets the connection to the database, sets the auto-commit method, gets the statement (including its subclasses) object
preparestatement (SQL)-returns an object used to execute the SQL statement
Callablestatement--> returns an object that executes a stored procedure
Setautocommit (True)--Set the database submission method (Auto/manual)

Statement: Execute SQL statement, return result set
Returns the Boolean type Preparestatement.execute ()--for executing stored procedures and functions
returns resultset Preparestatement.executequery ()--Statements for queries
returns int Preparestatement.executeupdate ()--for adding, deleting, changing statements

Batch processing:
Preparestatement.addbatch ()-One SQL statement per processing
return int[] Preparestatement.executebatch () and submit a batch of commands to the database for execution

Building SQL statements as placeholders
String sql = "SELECT * FROM tablename t where t.id=?" and T.name in (?,?,?) ";
Preparestatemenet.setint (1, 24)
Preparestatemenet.setstring (2, "Tom");
Preparestatemenet.setstring (3, "Jack");
Preparestatemenet.setstring (4, "Kitty");
Preparestatemenet.executequery ();
To call the set placeholder parameter method, you need to pass two parameters:
First parameter: Placeholder index value, how many? , just write a few, starting from 1 number
The second parameter: the value of the placeholder, what type of parameter to set, and what type of method to use

Result set returned after query SQL executed by statement
Returns the Boolean type Next () and determines whether the result set has the next row of data
Get Data
Resultset.getstring ()---> method you can pass a string that represents the name of a field in a table, or an integer that represents the first column
Resultset.getint ()
Resultset.getdate ()
Get the data in the result set by rewriting the various methods

Code for the Java Connection database
Define four properties: Username, password, URL, driver

    private static final String USERNAME = "Database login name";    private static final String PASSWORD = "Database login password";    private static final String URL = "Jdbc:oracle:thin: @localhost: 1521:xe";    private static final String Driverclassname = "Oracle.jdbc.OracleDriver";

1, class loader to load the database driver

Class.forName (Dirver);
2, directly call DriverManager static method getconnection () Get the connection
Connection conn = drivermanager.getconnection (URL, username, password);

0822jdbc--java Connection Database

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.