Connect and operate SQL Server database with Java

First, we need to add the corresponding sqljdbc to the software used jar。

General process

1. Load JDBC Driver:

Before connecting to the database, first load the driver of the database you want to connect to the JVM (Java virtual machine), which is implemented through the static method forname (string classname) of the java.lang.class class class.

2. Provide the URL of the JDBC connection.

3. Create a connection to the database:

• to connect to the database, you need to apply to Java sql. The drivermanager requests and obtains a connection object, which represents a connection to a database.

• use the getconnect in (string URL, string username, string password) method of drivermanager to pass in the path of the specified database to be connected, the user name and password of the database.

4. Create a statement:

• to execute SQL statements, you must obtain Java sql. Statement instance, which can be divided into the following three types: (1) execute static SQL statements. It is usually implemented through statement instance. Statement stmt = con. Createstatement(); (2) Execute dynamic SQL statements. It is usually implemented through the Preparedstatement instance. Preparedstatement pstmt = con.preparestatement (SQL); (3) Execute database stored procedures. Usually implemented through callablestatement instances. Callablestatement cstmt = con.preparecall ('{call demosp (?,?)}');

5. Execute SQL statement:

The statement interface provides three methods to execute SQL statements: executeQuery, executeupdate, and execute (1) resultset executeQuery (string sqlstring): execute SQL statements that query the database and return a resultset object. (2) int executeupdate (string sqlstring): used to execute insert, update, or delete statements and SQL DDL statements, For example, create table, drop table, etc. (3) execute (sqlstring): used to execute statements that return multiple result sets, multiple update counts, or a combination of the two. ResultSet rs = stmt. executeQuery("SELECT * FROM ...") ; int rows = stmt. executeUpdate("INSERT INTO ...") ; boolean flag = stmt. execute(String sql) ;

6. Processing results:

There are two situations: (1) executing the update returns the number of records affected by this operation. (2) executing the query returns a resultset object. • the resultset contains all rows that meet the conditions in the SQL statement, and it provides access to the data in these rows through a set of get methods. • using the result set Access method of (resultset) object to get data:

7. After the close JDBC object operation is completed, close all used JDBC objects to release JDBC resources. The closing sequence is opposite to the declaration sequence: 1. Close the recordset 2. Close the declaration 3. Close the connection object

1、 Write dbutil java

2、 Write an example model class person

3、 Write persondao java

4、 Write test

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>