---
title: "JDBC常用类和方法-java.sql.*"
author: "prodream"
date: "2008-08-23T15:49:00Z"
tags: []
slug: "jdbc-java-sql"
draft: false
lastmod: "2008-08-23T15:49:00Z"
id: 578038
url: "https://prodream.iteye.com/blog/578038"
---

1、DriverManager类：  
&lt;clk&gt;&lt;/clk&gt; 负责管理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类  
&lt;clk&gt;&lt;/clk&gt; 负责维护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类对象的字段是否为只读