jdbc preparedstatement 使用

 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
     Class.forName("oracle.jdbc.driver.OracleDriver");//引用Oracle数据库连接驱动包
     Connection con =DriverManager.getConnection("jdbc:oracle:thin:@172.18.2.248:1521:leoorcl","bruce","bruce");
     //preparedStatement 是预编译sql
     PreparedStatement ps = con.prepareStatement("select * from userr where money>? and name>?");//?叫占位符
      //此处有多少 ? 后面就必要有多少setInt(index,值) index从1开始  从左到右
     ps.setInt(1,30000);//设置占位符的值  从左到右
     ps.setInt(2,2);
     ResultSet rs = ps.executeQuery();
     while(rs.next()){  //从结果中取数据
       
      System.out.println("money:"+rs.getString("money"));//通过属性名取
      System.out.println("name:"+rs.getString(1));//通过列号取
     }
        if(rs == null){//用完过后必须关闭 从子到父的顺序关闭
          rs.close();
        }
        if(ps == null){
         ps.close();
       }
        if(con == null){
         con.close();
       }
 }
 

/*

   //executeUpdate() 用法

// 第一个参数不变,第二个参数从0取到9

  ps.setString(1, "name");

  for (int i = 0; i < 10; i++) {

  ps.setInt(2, i);

  int rowCount = ps.executeUpdate();//executeUpdate() 返回是一个int 变量

  }

 

 

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值