常用的JDBC类与方法

常用的JDBC类与方法

1、DriverManager类:
负责管理JDBC驱动程序。使用JDBC驱动程序之前,必须先将驱动程序加载并向DriverManager注册后才可以使用,同时提供方法来建立与数据库的连接。

方法:
A、Class.forName(String driver); //加载注册驱动程序
B、Static Connection getConnection(String url,String user,String password) throws SQLException;
//取得对数据库的连接
C、Static Driver getDriver(String url) throws SQLExcetion;
//在已经向DriverManager注册的驱动程序中寻找一个能够打开url所指定的数据库的驱动程序


2、Connection类
负责维护JSP/JAVA数据库程序和数据库之间的联机。可以建立三个非常有用的类对象。

方法:
A、Statement createStatement() throws SQLException; //建立Statement类对象
Statement createStatement(int resultSetType,int resultSetConcurrency) throws SQLException;
// 建立Statement类对象

resultSetType值
TYPE_FORWARD_ONLY 结果集不可滚动
TYPE_SCROLL_INSENSITIVE 结果集可滚动,不反映数据库的变化
TYPE_SCROLL_SENSITIVE 结果集可滚动,反映数据库的变化

resultSetConcurrency值
CONCUR_READ_ONLY 不能用结果集更新数据
CONCUR_UPDATABLE 能用结果集更新数据

JDBC2.0中才支持滚动的结果集,而且可以对数据进行更新

B、DatabaseMetaData getMetaData() throws SQLException; //建立DatabaseMetaData类对象
C、PreparedStatement prepareStatement(String sql) throws SQLException;
//建立PreparedStatement类对象
D、boolean getAutoCommit() throws SQLException //返回Connection类对象的AutoCommit状态
E、void setAutoCommit(boolean autoCommit) throws SQLException
//设定Connection类对象的AutoCommit状态
F、void commit() throws SQLException //确定执行对数据库新增、删除或修改记录的操作
G、void rollback() throws SQLException //取消执行对数据库新增、删除或修改记录的操作
H、void close() throws SQLException //结束Connection对象对数据库的联机
I、boolean isClosed() throws SQLException //测试是否已经关闭Connection类对象对数据库的联机

3、Statement类

通过Statement类所提供的方法,可以利用标准的SQL命令,对数据库直接新增、删除或修改操作

方法:

A、ResultSet executeQuery(String sql) throws SQLException //使用SELECT命令对数据库进行查询
B、int executeUpdate(String sql) throws SQLException
//使用INSERT\DELETE\UPDATE对数据库进行新增、删除和修改操作。
C、void close() throws SQLException //结束Statement类对象对数据库的联机


4、PreparedStatement类

PreparedStatement类和Statement类的不同之处在于PreparedStatement类对象会将传入的SQL命令事先编好等待使用,当有单一的SQL指令比多次执行时,用PreparedStatement类会比Statement类有效率

方法:

A、ResultSet executeQuery() throws SQLException //使用SELECT命令对数据库进行查询
B、int executeUpdate() throws SQLException
//使用INSERT\DELETE\UPDATE对数据库进行新增、删除和修改操作。
C、ResultSetMetaData getMetaData() throws SQLException
//取得ResultSet类对象有关字段的相关信息
D、void setInt(int parameterIndex,int x) throws SQLException
//设定整数类型数值给PreparedStatement类对象的IN参数
E、void setFloat(int parameterIndex,float x) throws SQLException
//设定浮点数类型数值给PreparedStatement类对象的IN参数
F、void setNull(int parameterIndex,int sqlType) throws SQLException
//设定NULL类型数值给PreparedStatement类对象的IN参数
G、void setString(int parameterIndex,String x) throws SQLException
//设定字符串类型数值给PreparedStatement类对象的IN参数
H、void setDate(int parameterIndex,Date x) throws SQLException
//设定日期类型数值给PreparedStatement类对象的IN参数
I、void setTime(int parameterIndex,Time x) throws SQLException
//设定时间类型数值给PreparedStatement类对象的IN参数


5、DatabaseMetaData类

DatabaseMetaData类保存了数据库的所有特性,并且提供许多方法来取得这些信息。

方法:

A、String getDatabaseProductName() throws SQLException //取得数据库名称
B、String getDatabaseProductVersion() throws SQLException //取得数据库版本代号
C、String getDriverName() throws SQLException //取得JDBC驱动程序的名称
D、String getDriverVersion() throws SQLException //取得JDBC驱动程序的版本代号
E、String getURL() throws SQLException //取得连接数据库的JDBC URL
F、String getUserName() throws SQLException //取得登录数据库的使用者帐号

6、ResultSet类

负责存储查询数据库的结果。并提供一系列的方法对数据库进行新增、删除和修改操作。也负责维护一个记录指针(Cursor),记录指针指向数据表中的某个记录,通过适当的移动记录指针,可以随心所欲的存取数据库,加强程序的效率。

方法:

A、boolean absolute(int row) throws SQLException //移动记录指针到指定的记录
B、void beforeFirst() throws SQLException //移动记录指针到第一笔记录之前
C、void afterLast() throws SQLException //移动记录指针到最后一笔记录之后
D、boolean first() throws SQLException //移动记录指针到第一笔记录
E、boolean last() throws SQLException //移动记录指针到最后一笔记录
F、boolean next() throws SQLException //移动记录指针到下一笔记录
G、boolean previous() throws SQLException //移动记录指针到上一笔记录
H、void deleteRow() throws SQLException //删除记录指针指向的记录
I、void moveToInsertRow() throws SQLException //移动记录指针以新增一笔记录
J、void moveToCurrentRow() throws SQLException //移动记录指针到被记忆的记录
K、void insertRow() throws SQLException //新增一笔记录到数据库中
L、void updateRow() throws SQLException //修改数据库中的一笔记录
M、void update类型(int columnIndex,类型 x) throws SQLException //修改指定字段的值
N、int get类型(int columnIndex) throws SQLException //取得指定字段的值
O、ResultSetMetaData getMetaData() throws SQLException //取得ResultSetMetaData类对象

7、ResultSetMetaData类

ResultSetMetaData类对象保存了所有ResultSet类对象中关于字段的信息,提供许多方法来取得这些信息。

方法:

A、int getColumnCount() throws SQLException //取得ResultSet类对象的字段个数
B、int getColumnDisplaySize() throws SQLException //取得ResultSet类对象的字段长度
C、String getColumnName(int column) throws SQLException //取得ResultSet类对象的字段名称
D、String getColumnTypeName(int column) throws SQLException //取得ResultSet类对象的字段类型名称
E、String getTableName(int column) throws SQLException //取得ResultSet类对象的字段所属数据表的名称
F、boolean isCaseSensitive(int column) throws SQLException //测试ResultSet类对象的字段是否区分大小写
G、boolean isReadOnly(int column) throws SQLException //测试ResultSet类对象的字段是否为只读


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/geekwang/archive/2008/05/09/2425665.aspx
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/*============================================================================== * * Filename: TSQLServer.cs * Description: 本次常用代码封装为数据库通用访问: * 包含以下方法: * 1.获取数据库连接字符串,创建数据库连接命令,判断数据字符数组 * 2.执行非查询ExecuteNonQuery返回受影响的行数,增加,修改,删除, * 3.执行Sql语句返回DataRow一行数据信息 * 4.执行Sql语句返回DataTable数据信息 * 5. 执行多条SQL语句,实现数据库事务。如果插入或者修改数据运用事务时候,其中一行报错,将事务回滚。 * 6.在执行命令文本时后,传递sql语句运用Regex re = new Regex(@"@[a-zA-Z0-9]") 则正则表达式进行配匹, * 提高安全性。 * Version: 1.0 * Created: 2012.08.02 * Author: liangjw * Company: Copyright (C) Create Family Wealth Power By Peter * ==============================================================================*/ * 实用方法简单讲解,下面以TSQLServer.cs为例子: 1.我们最常用的方式就是增删改查,在使用时简单方面,例如我们需要做修改一行记录需要返回DataRow一行记录 需要调用方法调用如下: public static DataRow GetRow(params object[] values) { return TSQLServer.ExecDr("select * from [tb] where id = @1", values); } 2.返回一个DataTable数据集合调用如下: public static DataTable GetTable(object[] s = null) { return TSQLServer.ExecDt("select * from [tb] ", s); } 3.更新一条数据 public static int Update(params object[] values) { return TSQLServer.ExecuteNonQuery(@"update [tb] set test1= @1,test2= @2 where id = @id", values); } 4.插入一条数据 public static int Insert(params object[] values) { return TSQLServer.ExecuteScalar(@"insert into [tb](test1,test2) values(@1,@2);SELECT @@IDENTITY",values).ToInt32(); } * 以上该文件的代码封装的是部分代码,仅供参考 * 备注信息: 上传部分自己总结的常用方法的封装,有不足和不完美之处,希望大家指出来,愿意一起 * 学习和讨论有关asp.net mvc ,Ajax ,jquery ,,html/css,xml ,sqlserver ,wpf,IIS相关技术的交流和学习。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值