首页 技术 正文
技术 2022年11月9日
0 收藏 519 点赞 3,551 浏览 5672 个字

JDBC API中包含四个常用的接口和一个类分别是:

1、Connection接口

2、Statement接口

3、PreparedStatement接口

4、ResultSet接口

5、DriverManager类

下面一一介绍这四个接口和一个类

(1)Connection接口

Connection接口位于java.sql包当中,是与数据库连接会的对象,只有获得特定的数据库连接对象,才可以访问数据库进行数据库操作。在进行数据库连接的时候还要用到DriverManager类中的getConnection(url,username,password)方法。例如:

 String url = "jdbc:mysql://localhost:3306/mysqltest";
//数据库用户名
String userName = "root";
//数据库密码
String passWord = "123456";
//创建Connection连接
Connection conn = (Connection)DriverManager.getConnection(url,userName,passWord);

此外该接口中还有close()方法用于关闭数据库连接,但是该数据库还是会占用jdbc资源。

(2)DriverManager类

该类中包含了与数据库交互操作的方法,该类中的方法全部有数据库厂商提供。DriverManager类中有6个常用的也是重要的方法分别是:

public static void deregisterDriver(Driver driver) throws SQLException

该方法是从DriverManager的管理列表中删除一个驱动程序。其中driver参数是要删除的驱动对象。

public static Connection getConnection(String url) throws SQLException

该方法是根据指定数据库连接URL,建立与数据库连接Connection。其中参数url是数据库连接的URL

public static Connection getConnection(String url,Properties info)

该方法是根据指定数据库连接URL,以及数据库连接属性信息建立数据库连接Connection。其中参数url为数据库连接URL,参数info是数据库连接属性。

public static Connection getConnection(String url,String user,String password) throws SQLException

该方法是根据数据库连接URL、用户名以及密码建立数据库连接Connection。参数url是数据库连接URL,参数user是连接数据库的用户名,参数password是连接数据库的密码。

public static Enumeration<Driver>getDrivers()该方法是获取当前DriverManager中已加载的所有驱动程序,它的返回值为Enumeration。

public static void registerDriver(Driver driver) throws SQLException

该方法是向DriverManager注册一个驱动对象,参数driver是要注册的驱动。

(3)Statement接口

Statement接口是Java程序执行数据库操作的重要接口,用于已经建立数据库连接的基础之上,向数据库发送要执行的SQL语句。它用于执行不带参数的简单SQL语句。该接口中包含九个常用的重要方法。

void addBatch(String sql) throws SQLException该方法是将SQL语句添加到此Statement对象的当前命令列表中,此方法用于SQL命令的批处理。

void clearBatch() throws SQLException该方法是清空Statement对象中的命令列表。

void close() throws SQLException 该方法是立即释放此Statement对象的数据库和JDBC资源,而不是等待该对象自动关闭时发生此操作。

boolean excute(String sql) throws SQLException 该方法是执行指定的SQL语句。如果sql语句返回结果,此方法返回true,否则返回false。

int[] excuteBatch() throws SQLException 该方法是将一批SQL命令提交给数据库执行,返回更新计数组成的数组。

ResultSet excuteQuery(String sql) throws SQLException 该方法是执行查询类型(select)的SQL语句,此方法返回查询所获取的结果集ResultSet对象。

excuteUpdate int excuteUpdate(String sql) throws SQLException 该方法执行SQL语句中DML类型(insert、update、delete)的SQL语句,返回更新所影响的行数。

Connection getConnection() throws SQLException 该方法获取生成此Statement对象的Connection对象

boolean isClosed() throws SQLException 该方法用来判断Statement对象是否已被关闭,如果Statement对象被关闭,则不能再调用此Statement对象执行SQL语句,此方法返回布尔值。

(4)PreparedStatement接口

PreparedStatement接口位于java.servlet包当中,它继承了Statement,但是PreparedStatement与Statement有这两方面的不同,第一:由于 PreparedStatement 对象已预编译过,所以其执行速度要快于 Statement 对象。因此,多次执行的 SQL 语句经常创建为 PreparedStatement 对象,以提高效率。

作为 Statement 的子类,PreparedStatement 继承了 Statement 的所有功能。另外它还添加了一整套方法,用于设置发送给数据库以取代 IN 参数占位符的值。

创建过程:

以下的代码段(其中 con 是 Connection 对象)创建包含带两个 IN 参数占位符的 SQL 语句的 PreparedStatement 对象:

 PreparedStatement pstmt = con.prepareStatement("UPDATE table4 SET m = ? WHERE x = ?");

查看代码

pstmt 对象包含语句 “UPDATE table4 SET m = ? WHERE x = ?”,它已发送给DBMS,并为执行作好了准备。

void setBinaryStream(int parameterIndex,InputStream x) throws SQLException  将输入流x作为SQL语句中的参数值,parameterIndex是参数位置索引。

void setBoolean(int parameterIndex,boolean x) throws SQLException 将布尔值x作为SQL语句中的参数值,parameterIndex为参数位置索引。

void setByte(int parameterIndex,byte x) throws SQLException 将byte值x作为SQL语句中的参数值,parameterIndex为参数位置的索引。

void setDate(int parameterIndex,Date x) throws SQLException 将java.sql.Date值x做为SQL语句中的参数值,parameterIndex为参数位置的索引。

void setDouble(int parameterIndex,double x) 将double值x做为SQL语句中的参数值,parameterIndex为参数索引。

void setFloat(int parameterIndex,floatx) throws SQLException 将float值x做为SQL语句中的参数值,parameterIndex为参数位置的索引。

void setInt(int parameterIndex,int x) throws SQLException 将int值x做为SQL语句中的参数值,parameterIndex为参数位置的索引。

void setInt(int parameterIndex,long x) throws SQLException 将long值x做为SQL语句中的参数值,parameterIndex为参数位置的索引。

void setObject(int parameterIndex,Object x) throws SQLException 将object对象x做为SQL语句中的参数值,parameterIndex为参数位置的索引。

void setShort(int parameterIndex,short x) throws SQLException 将short值x做为SQL语句中的参数值,parameterIndex为参数位置的索引。

void setString(int parameterIndex,String x) throws SQLException 将String值x做为SQL语句的参数值,parameterIndex为参数位置的索引。

void setTimestamp(int parameterIndex,Timestamp x) throws SQLException 将java.sql.Timestamp值x做为SQL语句中的参数值,parameterIndex为参数位置的索引。

(5)Result接口

数据库结果集的结果表,通常通过查询数据库的语句生成。

以下代码片段(其中 con 为有效的 Connection 对象)演示了如何生成可滚动且不受其他更新影响的、可更新的结果集。请参阅 ResultSet 字段以了解其他选项。

Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2"); // rs will be scrollable, will not show changes made by others, // and will be updatable

查看代码

ResultSet 接口提供用于从当前行检索列值的获取方法(getBoolean、getLong 等)。可以使用列的索引编号或列的名称检索值。一般情况下,使用列索引较为高效。列从 1 开始编号。为了获得最大的可移植性,应该按从左到右的顺序读取每行中的结果集列,而且每列只能读取一次。ResultSet接口中常用的方法有:boolean absolute(int row) throws SQLException 将光标移动到此ResultSet对象的给定行编号,参数row为行编号。void afterLast() throws SQLException 将光标移动到此ResultSet对象的最后一行之后,如果结果集中不包含任何行,则此方法无效。void beforeFirst() throws SQLException 立即释放此ResultSet对象的数据库和JDBC资源void deleteRow() throws SQLException 从此ResultSet对象和底层数据库中删除当前行。boolean first() throws SQLException 将光标移动到此ResultSet对象的第一行InputStream getBinaryStream(String columnLabel) throws SQLException 以byte流的方式获取ResultSet对象当前行中指定列的值,参数columnLabel为列名称。Date getDate(String columnLabel) throws SQLException 以java.sql.Date的方式获取ResultSet对象当前行中指定列的值,参数columnLabel为列名称。double getDouble(String columnLabel) throws SQLException 以double的方式获取ResultSet对象当前行中指定列的值,参数columnLabel为列名称。float getFloat(String columnLabel) throws SQLException 以float的方式获取ResultSet对象当前行中指定列的值,参数columnLabel为列名称int getInt(String columnLabel) throws SQLException 以int的方式获取ResultSet对象当前行中指定的列的值,参数columnLabel为列名称。String getString(String columnLabel) throws SQLException 以String的方式获取ResultSet对象当前行中指定列的值,参数columnLabel为列名称。boolean isClosed() throws SQLException 判断当前ResultSet对象是否已关闭。boolean last() throws SQLException 将光标移动到此ResultSet对象的最后一行。boolean next() throws SQLException 将光标位置向后移动一行,如移动的新行有效返回true,否则返回false。boolean previous() throws SQLException 将光标位置向前移动一行,如移动的新行有效返回true,否则返回false。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774