PreparedStatement

来源:互联网 发布:解决网络高峰期问题 编辑:程序博客网 时间:2024/05/17 01:48
/*
 * preparedstatement 
 * 1.PreparedStatement 可以提高执行效率(因为他有预编译功能)
 * 2.PreparedStatement 可防止sql注入漏洞
 */
import java.sql.*;
public class test2 {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
     PreparedStatement ps=null;
     ResultSet rs=null;
     Connection ct=null;
     try {
//加载驱动
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    //得到连接
    ct=DriverManager.getConnection("jdbc:odbc:mytest");
    //创建ps
//     ps=ct.prepareStatement("select * from dept where deptno=?");
//         ps.setInt(1,50);
//     rs=ps.executeQuery();
//    
//     while(rs.next()){
//    
//     System.out.print(rs.getInt(1)+rs.getString(2));
//     }
    ps=ct.prepareStatement("insert into dept values(?,?,?)");
    ps.setInt(1,20);
    ps.setString(2,"60");
    ps.setString(3,"love");
    int i=ps.executeUpdate();
    if(i==1)
    {
    System.out.println("insert ok\n");
    }
    else {
    System.out.println("insert error\n");
    }
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
//关闭资源
if(rs!=null){

}
ps.close();
if(ct!=null){
ct.close();
}
} catch (Exception e2) {
// TODO: handle exception
// e2.printStackTrace();
}

}

}


}
原创粉丝点击