CallableStatement 使用实例
/**
* CallableStatement使用
*/
package com.testjdbc.jdbc;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
/**
* create date: 2011-3-26 下午03:30:06
*
* @author Tao
*
*/
public class TestCallableStatement {
private Connection conn;
private CallableStatement cstmt;
private String driver = “oracle.jdbc.OracleDriver”;
private String url = “jdbc:oracle:thin:@127.0.0.1:1521:orcl”;
/**
*
*/
public TestCallableStatement() {
// TODO Auto-generated constructor stub
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, “ty”, “ty”);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void useAdddatatest3(){
try {
cstmt = conn.prepareCall(“{call adddatatest3(?,?,?,?,?)}”);
cstmt.setInt(1, 3);
cstmt.setString(2, “mark”);
cstmt.setDouble(3, 6000);
cstmt.setDate(4, Date.valueOf(“2010-10-10”));
cstmt.setDate(5, Date.valueOf(“2001-10-10”));
int i = cstmt.executeUpdate();
System.out.println(i);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void useGetArea(){
try {
cstmt = conn.prepareCall(“{call get_area(?,?,?)}”);
cstmt.setInt(1, 20);
cstmt.setInt(2, 30);
cstmt.registerOutParameter(3, Types.DOUBLE);
cstmt.execute();
System.out.println(cstmt.getDouble(3));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 资源释放
*/
public void close(){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestCallableStatement tcs = new TestCallableStatement();
//tcs.useAdddatatest3();
tcs.useGetArea();
tcs.close();
}
}