Home > Java > javaTutorial > body text

5 steps to connect to the database using jdbc

angryTom
Release: 2019-07-20 15:38:53
Original
26276 people have browsed it

5 steps to connect to the database using jdbc

Recommended tutorial: java tutorial

1. Loading Driver

Before connecting to the database, you need to load the database driver to the JVM (Java Virtual Machine). This requires the static method forName(String className) of the java.lang.Class class. ) implementation.
For example:

//加载Oracle的驱动
try{
Class.forName("oracle.jdbc.OracleDriver");
}catch(ClassNotFoundException e){
System.out.println("找不到程序驱动类,加载驱动失败!");
e.printStackTrace();
}
Copy after login

After successful loading, the instance of the driver class will be registered in the DriverManager class.

2. Create a database connection

## If you want to connect to the database, you need to ask java.sql.DriverManager Request and obtain the Connection<strong></strong> object, which represents a database connection. Use DriverManager's
getConnection(url,uname,upass)<strong></strong>Pass in the path to the database, username, and password to get a connection

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.31.50:8080:orcl","uname","upass");
Copy after login

3. Create a Statement

To execute a SQL statement, you must obtain a java.sql.Statement instance, Statement instance Divided into the following 3 types:
1. Execute static statements, usually implemented through Statement instances.

Statement st = conn.createStatement();
Copy after login

 2. Execute dynamic statements, usually implemented through PreparedStatement.

PreparedStatement ps = conn.prepareStatement(sql);
Copy after login

3. Execute the database stored procedure. Usually implemented through CallableStatement

CallableStatement cs = conn.prepareCall("{CALL demoSp(?,?)}");
Copy after login

4. Execute sql statement

ResultSet executeQuery(sql) <span style="font-size: 14px;"></span>Suitable for traversing multiple results, inserting into a set, and returning a result set. You can use .next to traverse the object and use getString("field name") to obtain the field value 
int executeUpdate(sql ) Applicable to Update, insert or delete statements and sqlDDL statements, such as creating tables and deleting tables, etc., returning the number of changed items

5. Close JDBC Object

##The closing order must be opposite to the declaration order

 1.Close the record set
 2.Close the statement 3.Close the connection object


if(rs != null){
    rs.close();
    }
Copy after login

The above is the detailed content of 5 steps to connect to the database using jdbc. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template