Java-commonly used classes and interfaces in JDBC

JDBC overview

JDBC is a Java API (Application Porgramming Interface) that can be used to execute SQL statements. It is a link between the database and Java applications.

The full name of JDBC is Java DataBase Connectivity, which is a set of object-oriented application program interfaces that specify a unified standard interface for accessing various relational databases.

In the Java language, a wealth of classes and interfaces are provided for database programming. These classes and interfaces can be used to facilitate data access and processing. This section will introduce some commonly used JDBC interfaces and classes. These classes and interfaces are all in java. .sql package.

Connection interface

The Connection interface represents a connection to a specific database, executes SQL statements in the connection context and returns the results.

Common methods of Connection interface:

method Function description
createStatement() Create Statement object
createStatement(int resultSetType, int resultSetConcurrency) Create a Statement object, which will generate a ResultSet object with a given type, concurrency and saveability
preparedStatement() Create a preprocessed object preparedStatement
isReadOnly() Check whether the reading mode of the current Connection object is read-only
setReadOnly() Set the read and write mode of the current Connection object, the default is non-read-only mode
commit() Make all changes made after the last commit/rollback become persistent changes, and release all database locks currently held by this Connection object
roolback() Cancel all changes made in the current transaction, and release all database locks currently held by this Connection object
close() Release the database and JDBC resources of this Connection object immediately, instead of waiting for them to be automatically released

Statement interface

The Statement interface is used to send SQL statements to the database based on the established connection. There are 3 kinds of Statement objects in JDBC, namely Statement, PreparedStatement and CallableStatement. The Statement object is used to execute simple SQL statements without parameters. PreparedStatement inherits Statement to execute dynamic SQL statements. CallableStatement inherits PreparedStatement to execute calls to stored procedures in the database.

Common methods in the Statement interface:

method Function description
execute(String sql) Execute a static SELECT statement, which may return multiple result sets
executeQuery(String sql) Execute the given SQL statement, which returns a single ResultSet object
clearBarch () Clear the current SQL command list of this Statement object
executeBarch() Submit a batch of commands to the database for execution. If all commands are executed successfully, an array of update counts will be returned. The order of the array elements corresponds to the order in which SQL statements are added.
addBarch(String sql) Add the given SQL command to the current command list of this Statement object. If the driver does not support batch processing, an exception will be thrown
close() Release the database and JDBC resources occupied by the Statement instance

PreparedStatement interface

The PreparedStatement interface is used to dynamically execute SQL statements. The SQL statement executed by the PreparedStatement instance will be compiled and saved in the PreparedStatement instance, so that the SQL statement can be executed repeatedly.

Common methods of PreparedStatement interface:

method Function description
setInt(int index , int k) Set the parameter at the specified position to an int value
setFloat(int index , float f) Set the parameter at the specified position to a float value
setLong(int index , long l) Set the parameter at the specified position to a long value
setDouble(int index , double d) Set the parameter at the specified position to a double value
setBoolean(int index , boolean b) Set the parameter at the specified position to a boolean value
setDate(int index , date date) Set the parameter at the specified position to the date value
executeQuery() Execute SQL query in this PreparedStatement object and return the ResultSet object generated by the query
setString(int index , String s ) Set the parameter at the specified location to the corresponding String value
setNull(int index , intsqlType) Set the parameter at the specified position to SQL NULL
executeUpdate() Execute dynamic INSERT, UPDATE or DELETE statements with the parameters included in the previous section
clearParameters() Clear the current values ​​of all parameters

DriverManager class

The DriverManager class is used to manage all drivers in the database. He is the management layer of JDBC, acting between users and drivers, tracking available drivers, and establishing connections between database drivers. If the connection can be established through the getConnection() method, the connection is returned, otherwise an exception is thrown.

Common methods of the DriverManager class:

method Function description
getConnection(String url, String user ,String password) Specify 3 entry parameters (the url to connect to the database, user name, and password in turn) to obtain the connection to the database
setLoginTimeout() Get the maximum time that the driver can wait when trying to log in to a database, in seconds
println(String message) Print a message to the current JDBC log stream

ResultSet interface

ResultSet 接口类似于一个临时表,用来暂时存放数据库查询操作所获得的结果集。ResultSet 实例具有指向当前数据行的指针,指针开始的位置在第一条记录的前面,通过next()方法可将指针向下移动。

ResultSet 接口的常用方法:

方法 功能描述
geInt() 以int形式获取此ResultSet 对象当前行的指定列值。如果列值是NULL,则返回0
getFloat() 以float形式获取此ResultSet 对象当前行的指定列值。如果列值是NULL,则返回0
getDate() 以data形式获取此ResultSet 对象当前行的指定列值。如果列值是NULL,则返回null
getBoolean() 以boolean形式获取此ResultSet 对象当前行的指定列值。如果列值是NULL,则返回null
getString() 以String形式获取此ResultSet 对象当前行的指定列值。如果列值是NULL,则返回null
getObject() 以Object形式获取此ResultSet 对象当前行的指定列值。如果列值是NULL,则返回null
first() 将指针移到当前记录的第一行
last() 将指针移到当前记录的最后一行
next() 将指针向下移一行
beforeFirst() 将指针移到集合的开头(第一行位置)
afterLast() 将指针移到集合的尾部(最后一行位置)
absolute(int index) 将指针移到ResultSet 给定编号的行
isFrist() 判断指针是否位于当前ResultSet 集合的第一行。如果是返回true,否则返回false
isLast() 判断指针是否位于当前ResultSet 集合的最后一行。如果是返回true,否则返回false
updateInt() 用int值更新指定列
updateFloat() 用float值更新指定列
updatelLong() 用指定的long值更新指定列
updateString() 用指定的String值更新指定列
updateObject() 用Object值更新指定列
updateNull() 将指定的列值修改为NULL
updateDate() 用指定的date值更新指定列
updateDouble() 用指定的double值更新指定列
getrow() 查看当前行的索引号
insertRow() 将插入行的内容插入到数据库
updateRow() 将当前行的内容同步到数据库
deleteRow() 删除当前行,但并不同步到数据库中,而是在执行close()方法后同步到数据库

Guess you like

Origin blog.csdn.net/javanofa/article/details/105469746