jdbc基础2--翻译sun官方教程

Step by Step
进阶

Retrieving Information from a Database
获取数据库的数据信息

为了从数据库获取信息,你通过Statement的executeQuery方法向数据库
发送Select语句,这样信息会以行数据的形式存放在ResultSet对象中
默认的ResultSet通过next()方法一行一行的检索数据信息.可以通过
ResultSet的getXXX()方法来获取特定列数据.

考虑一下怎么去获取顾客在4J Cafe店内一天谁喝的最多的咖啡数.
如果用sql,一种方法是通过Order by 对表的Cups列进行降序排列,
那么返回ResultSet的第一行中就包含了期望的结果.SQL命令入下

SELECT Entry, Customer, DOW, Cups, Type
  FROM JJJJData
  ORDER BY Cups DESC

在程序中通过下面的方法来执行该sql语句

      ResultSet result = stmt.executeQuery(
        "SELECT Entry, Customer, DOW, Cups, Type " +
        "FROM JJJJData " +
        "ORDER BY Cups DESC");

ResultSet.next()返回一个boolean型数,true代表结果集中下一行还有记录,
false代表已经到了数据集的末尾.当ResultSet对象获得的时候此时的指针或者
游标总是指向第一条记录的上方.必须先调用next()方法,指针才能执行的一条记录.
用if(result.next()){..//取第一条记录}
while (result.next){..//取所有的记录}

根据数据类型带到列的结果
                     iEntry = result.getInt("Entry");
          Customer = result.getString("Customer");
          DOW = result.getString("DOW");
          Cups = result.getInt("Cups");
          TotalCups += Cups;  // increment total
          Type = result.getString("Type");
         
 前面大多讨论DriverManager的getConnection()方法,
 Connection本身具有以下的功能
 1.创建Statement, PreparedStatement,和 CallableStatement(通常用在存储过程中) 的实例
 2.获取DatabaseMetadata对象
 3.通过commit()和roolback()来实现事务的控制
 4.设置事务的隔离等级   

Statement对象
  Statement stmt=con.createStatement();
 
  通常用到的Statement的方法如下
    * executeQuery() 用来执行查询的sql语句返回结果集
    * executeUpdate() 用来执行sql语句对表或者表数据更新的sql语句返会被更新的行数,如果执行的是ddl语句那么将返回0
    * execute()       用来执行任何类型的sql语句,但是这是为那些既可以返回结果集又可以返回值的准备的.excute()在后面的章节不会被讨论.
   
    为了能在大多说数据库下运行良好,jdbc没有对sql的种类进行限制.
 
 Prepared Statements
 Prepared Statements 是statement的子接口,使用有如下好处
     * The contained SQL is sent to the database and compiled or prepared beforehand. From this point on, the prepared SQL is sent and this step is bypassed. The more dynamic Statement requires this step on every execution. Depending on the DB engine, the SQL may be cached and reused even for a different PreparedStatement and most of the work is done by the DB engine rather than the driver.
    * A PreparedStatement can take IN parameters, which act much like arguments to a method, for column values.
    * PreparedStatements deal with data conversions that can be error prone in straight ahead, built on the fly SQL; handling quotes and dates in a manner transparent to the developer, for example.

使用preparestament的例子

pstmtU = con.prepareStatement(
  "UPDATE myTable SET myStringColumn = ? " +
    "WHERE myIntColumn = ?"  );

pstmtQ = con.prepareStatement(
  "SELECT myStringColumn FROM myTable " +
    "WHERE myIntColumn = ? ");
   
    pstmtU.setString( 1, "myString" );
pstmtU.setInt( 2, 1024 );
pstmtU.executeUpdate();

pstmtQ.setInt( 1, 1024 );
pstmtQ.executeQuery();
需要注意的是preparestatement有它自己的execute方法
这些方法是无参的.还有就是它继承了statement所以当然包括了

所有statement的方法. java-sql类型等价转换 Common SQL Types--Standard Retrieval Methods
SQL Type Java Method
BIGINT getLong()
BINARY getBytes()
BIT getBoolean()
CHAR getString()
DATE getDate()
DECIMAL getBigDecimal()
DOUBLE getDouble()
FLOAT getDouble()
INTEGER getInt()
LONGVARBINARY getBytes()
LONGVARCHAR getString()
NUMERIC getBigDecimal()
OTHER getObject()
REAL getFloat()
SMALLINT getShort()
TIME getTime()
TIMESTAMP getTimestamp()
TINYINT getByte()
VARBINARY getBytes()
VARCHAR getString()
ResultSet的getString()可以应用到上面的所有类型,不过可能会抛出异常
SQL3 Types--Retrieval Methods

SQL Type Java Method
ARRAY getArray()
BLOB getBlob()
CLOB getClob()
DISTINCT get UnderlyingType ()
REF getRef()
STRUCT (castToStruct) getObject()
JAVA_OBJECT (castToObjectType) getObject()

ResultSet的getObject()可以应用与上面的所有类型
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值