2013年(23)
分类: Java
2013-07-10 09:17:04
下面是一个statement的列子 :
Java代码
package com.JDBC.proc;
import java.sql.*;
public class StatementTest {
public static void main(String args[]){
Connection conn=null;
Statement stm=null;
ResultSet rs=null;
try {
conn=DBTool.getConnection();
String sql="select EmpNo,EName from emp " +
"where empNo=7499";
stm=conn.createStatement();
rs=stm.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getInt(1)+"---"+rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
DBTool.release(rs, stm, conn);
}
}
}
他的主要作用阐述Statement的用法。
下面是关于prepareStatement的列子:
Java代码
package com.JDBC.proc;
import java.sql.*;
public class PrepareStatement {
public static void main(String[] args){
Connection conn=null;
PreparedStatement psmt=null;
ResultSet rs=null;
try {
conn=DBTool.getConnection();
String sql="select EmpNo,Ename " +
"from emp " +
"where EmpNo=?";
psmt=conn.prepareStatement(sql);
psmt.setInt(1, 7499);
rs=psmt.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1)+"---"+rs.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
DBTool.release(rs, psmt, conn);
}
}
}