|
16.3.2 Statement类及其子类
例16.2 Insert.java及程序说明
1: import java.net.URL;
2: import java.sql.*;
3:
4: class Insert{
5: public static void main(String args[]){
6: String url="jdbc dbc:demo";
7: try{
//下载jdbc-odbc bridge 驱动器
8: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver" ;//参见例16.1的第11句的注释
9: //与驱动器建立连接
10: Connection con=DriverManager.getConnection(url,"user","password" ;
//创建一个Statement对象
11: Statement stmt=con.createStatement();
12: //执行SQL声明
13: int count1=stmt.executeUpdate("INSERT INTO testTable(id,name) VALUES(1,'wu')" ;
14: int count2=stmt.executeUpdate("INSERT INTO testTable(id,name) VALUES(2,'wang')" ;
15: //打印执行结果
16: System.out.println("Insert successfully!" ;
17: System.out.println("Updated rows is"+(count1+count2)+"." ;
//关闭连接
18: stmt.close();
19: con.close();
20: }catch(SQLException ex){
//SQL异常信息
21: System.out.println("\n***SQLException caught ***\n" ;
22: while(ex!=null){
23: System.out.println("SQLState:"+ex.getSQLState());
24: System.out.println("Message:"+ex.getMessage());
25: System.out.println("Vendor:"+ex.getErrorCode());
26: ex=ex.getNextException();
27: System.out.println("" ;}
28: }catch(java.lang.Exception ex){
29: ex.printStackTrace();
30: }
31: }
32: }
-----
-------
例16.3 Insert2.java及程序说明
1:import java.net.URL;
2:import java.sql.*;
3:
4:class Insert2{
5: public static void main(String args[]){
6: String url="jdbc dbc:demo";
7: String data[][]={{"5","xu"},{"6","yan"}};
8: try{
//下载jdbc-odbc bridge 驱动器
9: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver" ;//关于此句请参见例16.1的11句注释
10: //与驱动器建立连接
11: Connection con=DriverManager.getConnection(url,"user","password" ;
12: //创建一个ParepareStatement对象
13: PreparedStatement pstmt=con.prepareStatement(
14: "INSERT INTO testTable (id,name) VALUES(?,?)");
15: //参数赋值,执行SQL声明
16: for (int i=0;i<data.length;i++){
17: pstmt.setInt(1,Integer.parseInt(data[0]));
18: pstmt.setString(2,data[1]);
19: pstmt.executeUpdate();
20: }
21: System.out.println("Insert successfully!");
22: //关闭连接
23: pstmt.close();
24: con.close();
25: }catch(SQLException ex){
//打印SQL异常信息
26: System.out.println("\n***SQLException caught ***\n");
27:while(ex!=null){
28:System.out.println("SQLState:"+ex.getSQLState());
29:System.out.println("Message:"+ex.getMessage());
30:System.out.println("Vendor:"+ex.getErrorCode());
31:ex=ex.getNextException();
32:System.out.println("");}
33:}catch(java.lang.Exception ex){
34:ex.printStackTrace();
35:}
36:}
37:}
----------
------------
例16.4 Callable.java。
import java.net.URL;
import java.sql.*;
class Callable{
public static void main(String args[]){
String url="jdbc dbc:test";//注意这里不再是demo数据源
try{
//下载jdbc-odbc bridge 驱动器
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//关于此句请参见例16.1的11句注释
//建立连接
Connection con=DriverManager.getConnection(url,"my-user","my-password");
//创建一个CallableStatement用于执行储存过程
CallableStatement cstmt=con.prepareCall("{call Search(?)}");
//参数赋值
cstmt.setInt(1,934678);
//执行储存过程。
cstmt.execute();
//处理执行结果
display(cstmt,"Search");
CallableStatement stmt=con.prepareCall("{call Delete(?)}");
stmt.setInt(1,934655);
stmt.execute();
display(stmt,"Delete");
//关闭连接
stmt.close();
cstmt.close();
con.close();
}
catch(SQLException ex){
//打印SQL异常信息
System.out.println("\n***SQLException caught ***\n");
while(ex!=null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
catch(java.lang.Exception ex){
ex.printStackTrace();
}
}
//处理执行储存过程的结果
private static void display(CallableStatement cstmt,String name)
throws SQLException{
System.out.println("Excute procedure"+name);
while(true){
//处理情况为:执行结果影响了记录
int rowCount=cstmt.getUpdateCount();
if(rowCount>0){
System.out.println("Updated rows is"+rowCount+".");
//判断是否还有等待处理的结果
if(!cstmt.getMoreResults())break;
continue;
}
//处理情况为:执行结果为ResultSet
ResultSet rs=cstmt.getResultSet();
if(rs!=null){
ResultSetMetaData rsmd=rs.getMetaData();
//打印结果集的标题
int numCols=rsmd.getColumnCount();
for(int i=1;i<=numCols;i++)
System.out.println("\t\t"+rsmd.getColumnLabel(i)+" ");
System.out.println();
//打印结果集的内容
boolean more = rs.next();
if(!more) System.out.println("\t\t0 \t\t\t0 \t\t0");
while(more){
for(int i=1;i<=numCols;i++)
System.out.print("\t\t"+rs.getString(i)+" ");
more=rs.next();
System.out.println();
}
//判断是否还有等待处理的结果
if(!cstmt.getMoreResults()) break;
continue;
}
//处理情况为:执行结果影响了0条记录
if(rowCount==0)
System.out.println("No row is updated!");
if(!cstmt.getMoreResults()) break;
}
}
} |
|