java jdbc(二)各接口介绍--Connection,Statement,ResultSet,PreparedStatement,ResultSetMetaData,DatabaseMetaDa

1.Connection接口

提供了许多 transaction管理方法,比如commit(),rollback()等等,可以获得tatement, PreparedStatement, and DatabaseMetaData对象。
常用方法:

1) public Statement createStatement(): creates a statement object that can be used to execute SQL queries.
2) public Statement createStatement(int resultSetType,int resultSetConcurrency): Creates a Statement object that will generate ResultSet objects with the given type and concurrency.
3) public void setAutoCommit(boolean status): is used to set the commit status.By default it is true.
4) public void commit(): saves the changes made since the previous commit/rollback permanent.
5) public void rollback(): Drops all changes made since the previous commit/rollback.
6) public void close(): closes the connection and Releases a JDBC resources immediately.
2.Statement接口

提供了一组方法执行数据库查询,可以获得ResultSet对象
常用方法:

The important methods of Statement interface are as follows:
1) public ResultSet executeQuery(String sql): is used to execute SELECT query. It returns the object of ResultSet.
2) public int executeUpdate(String sql): is used to execute specified query, it may be create, drop, insert, update, delete etc.
3) public boolean execute(String sql): is used to execute queries that may return multiple results.
4) public int[] executeBatch(): is used to execute batch of commands.
3**ResultSet**接口

包含一个指针指向特定行的数据,它的初始指向第一行的前面。
默认的,ResultSet 的指针是只能向前移动,并且ResultSet对象不可更新。但是我们可以通过输入either TYPE_SCROLL_INSENSITIVE 和 TYPE_SCROLL_SENSITIVE 中的一个 createStatement(int,int),我们就可以使这个对象能够前后移动,并可更新。
eg:

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,  
                     ResultSet.CONCUR_UPDATABLE);  

常用方法:

1) public boolean next():   is used to move the cursor to the one row next from the current position.
2) public boolean previous():   is used to move the cursor to the one row previous from the current position.
3) public boolean first():  is used to move the cursor to the first row in result set object.
4) public boolean last():   is used to move the cursor to the last row in result set object.
5) public boolean absolute(int row):    is used to move the cursor to the specified row number in the ResultSet object.
6) public boolean relative(int row):    is used to move the cursor to the relative row number in the ResultSet object, it may be positive or negative.
7) public int getInt(int columnIndex):  is used to return the data of specified column index of the current row as int.
8) public int getInt(String columnName):    is used to return the data of specified column name of the current row as int.
9) public String getString(int columnIndex):    is used to return the data of specified column index of the current row as String.
10) public String getString(String columnName): is used to return the data of specified column name of the current row as String.
4.PreparedStatement接口

是StateMent的子接口,它通常是用来执行带参的查询,比如:
String sql=”insert into emp values(?,?,?)”;
我们传递了三个参数(?)作为值,它的值可以调用PreparedStatement的setter方法设定。顺序是由1开始的
使用PreparedStatement可以改善性能
通过Connection接口的prepareStatement()方法,传入一个SQL查询语句作为参数,获得PreparedStatement对象。

public PreparedStatement prepareStatement(String query)throws SQLException{}  

常用方法:

The important methods of PreparedStatement interface are given below:
Method  Description
public void setInt(int paramIndex, int value)   sets the integer value to the given parameter index.
public void setString(int paramIndex, String value) sets the String value to the given parameter index.
public void setFloat(int paramIndex, float value)   sets the float value to the given parameter index.
public void setDouble(int paramIndex, double value) sets the double value to the given parameter index.
public int executeUpdate()  executes the query. It is used for create, drop, insert, update, delete etc.
public ResultSet executeQuery() executes the select query. It returns an instance of ResultSet.
5.ResultSetMetaData接口

用来获取resulset对象的元数据,例如:总列数,列名,列的类型等
通过ResultSet的getMetaData()方法就可以获取ResultSetMetaData对象
public ResultSetMetaData getMetaData()throws SQLException

常用方法:

public int getColumnCount()throws SQLException  it returns the total number of columns in the ResultSet object.
public String getColumnName(int index)throws SQLException   it returns the column name of the specified column index.
public String getColumnTypeName(int index)throws SQLException   it returns the column type name for the specified index.
public String getTableName(int index)throws SQLException    it returns the table name for the specified column index.
6.DatabaseMetaData接口

用来获取数据库的元数据,比如:数据库产品名称,产品版本,驱动名称,表格的数量及名称,视图的数量及名称等。
通过Connection接口的getMetaData()方法可以获得DatabaseMetaData对象
public DatabaseMetaData getMetaData()throws SQLException
常用方法:

public String getDriverName()throws SQLException: it returns the name of the JDBC driver.
public String getDriverVersion()throws SQLException: it returns the version number of the JDBC driver.
public String getUserName()throws SQLException: it returns the username of the database.
public String getDatabaseProductName()throws SQLException: it returns the product name of the database.
public String getDatabaseProductVersion()throws SQLException: it returns the product version of the database.
public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)throws SQLException: it returns the description of the tables of the specified catalog. The table type can be TABLE, VIEW, ALIAS, SYSTEM TABLE, SYNONYM etc.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值