JDBC拾遗

来源:互联网 发布:linux cfg文件 编辑:程序博客网 时间:2024/05/23 19:20

1  ResultSet对象

1.1  getXxx()方法

1、此类方法有两个版本:

(1)  使用数据库列序号

注意:数据库列序号从1开始;其中使用列序号的版本效率高。

(2) 使用列名

2、合理类型转换

当列数据类型与get方法返回结果类型不一致时,会进行合理的类型转换。

2  执行sql语句

Statement 对象用于执行不带参数的简单 SQL 语句;PreparedStatement 对象用于执行带或不带 IN 参数的预编译 SQL 语句;CallableStatement 对象用于执行存储过程。

2.1  Statement

   Statement statement = conn.createStatement();  
   ResultSet  rs = statement.executeQuery(sql); 

2.2  PreparedStatement

2.2.1  方法

1、executeQuery()
      Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.
2、executeUpdate()

      Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such asINSERT,UPDATE orDELETE; or an SQL statement that returns nothing, such as a DDL statement.

3、setBlob(int parameterIndex, Blob x)
      Sets the designated parameter to the given java.sql.Blob object.

4、setBinaryStream(int parameterIndex, InputStream x)
      Sets the designated parameter to the given input stream.

5、其它

      setDouble(int parameterIndex, double x);

      setFloat(int parameterIndex, float x);

      setInt(int parameterIndex, int x);

      setNull(int parameterIndex, int sqlType) ;           //Sets the designated parameter to SQL NULL.

2.3  读取Blob字段值

PreparedStatement statement = conn.prepareStatement(sql);
statement.setXxx(1, "");

ResultSet result =statement.executeQuery();

if(result.next()){

   Blob blob= result.getBlob(1);

   Image image= ImageIO.read(blob.getBinaryStream());

}

2.4  写入Blob对象

Blob blob=conn.createBlob();

int offset=0;

OutputStream out=blob.setBinaryStream(offset);

ImageIO.write(image, "PNG", out);


PreparedStatement statement = conn.prepareStatement(sql);

statement.setBlob(blob);

statement.executeUpdate();

3  SQL转义

为以下特性提供类sql转义:日期和时间字面量、调用表量函数、调用存储过程、外连接、LIKE子句中的转已字符;

3.1  日期和时间字面量转义

各数据库日期和时间字面量不同,转义如下:

1、为DATE使用  d,如{d '2017-04-05'}

2、为TIME使用  t,如{t '11:25:26'}

3、为TIMESTAMP使用  ts,如{ts '2017-04-05 11:25:26.889'}

3.2  调用表量函数转义

表量函数,即返回单个值的函数;转义如下:

{fn left(7,10)}

{fn user()}

3.3  调用存储过程转义

3.3.1  存储过程转义方法

This escape syntax has one form that includes a result parameter and one that does not. If used, the result parameter must be registered as an OUT parameter. The other parameters can be used for input, output or both. Parameters are referred to sequentially, by number, with the first parameter being 1.

使用=获取返回值;当没有参数时,可省略括号;

{?= call <procedure-name>}
{call <procedure-name>(<arg1>,<arg2>, ...)}

3.3.2  CallableStatement

该接口用于执行sql存储过程,扩展自 PreparedStatement。 Connection对象的prepareCall方法可返回此对象。

A CallableStatement can return one ResultSet object or multiple ResultSet objects. Multiple ResultSet objects are handled using operations inherited from Statement.
For maximum portability, a call's ResultSet objects and update counts should be processed prior to getting the values of output parameters.
注册out参数的方法

void registerOutParameter(int parameterIndex, int sqlType)  throws SQLException

Parameters:
    parameterIndex - the first parameter is 1, the second is 2, and so on
    sqlType - the JDBC type code defined by java.sql.Types. If the parameter is of JDBC type NUMERIC or DECIMAL, the version of registerOutParameter that accepts a scale value should be used.

3.4  外连接

由于各数据库没有标准的外连接语法,故需要转义,格式如下:

{oj tbl1 LEFT OUTER JOIN tbl2 ON tbl1.fieldx=tbl2.fieldx}

除类左外连接,还有RIGHT OUTER JOIN 和FULL OUTER JOIN;

具体用法如下:

SELECT  ×  FROM {oj ...};

3.5  LIKE子句转义

_和%在LIKE子句中有特殊含义,而对于使用其字面量,没有标准方法;当使用其字面量时,需要转义:

WHERE  ? LIKE %!_% {escape '!'}

此处将!定义为转义字符,!_ 指示下划线字面量。



以上内容摘自:

1、<java核心技术-卷2>

2、oracle官方文档: http://docs.oracle.com

0 0