博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
preparedStatement平台:
阅读量:4631 次
发布时间:2019-06-09

本文共 1171 字,大约阅读时间需要 3 分钟。

public class cs{

  public static void main(String[] args){

      try{

        class.forName("com.mysql.jdbc.Driver");//注册驱动

        Connection conn = DriverManager.getConnection("jdbc:mysql:///cs","root","root");//获得链接

        String sql="select * from tb";//sql语句

        PerparedStatement pare = conn.prepareStatement(sql);//获取链接对象

//作为 Statement 的子类,PreparedStatement 继承了 Statement 的所有功能。另外它还添加了一整套方法,用于设置发送给数据库以取代 IN 参数的值。同时,三种方法 execute、 executeQuery 和 executeUpdate 已被更改以使之不再需要参数。这些方法的 Statement 形式(接受 SQL 语句参数的形式)不应该用于 PreparedStatement 对象。

        ResultSet rs=pare.executeQuery(sql);

        //ResultSet接受executeQuery传递过来的对象数据之后方便进行操作

        while(rs.next()){

          int id=rs.getInt(1);

          //接受第一个next传递过来的第一个值,定义为INT

          String name=rs.getString("name");

          int age=rs.getInt(3);

          

System.out.println("id:" + id + "   " + "name:" + name + "   " + "age:" + age);

        }

      rs.close();

       pare.close();

        conn.close();

      }catch(ClssNotFoundException e){

        e.printStackTrace();

      }catch(SQLException e){

        e.printStackTrace();

      }

  }

}

转载于:https://www.cnblogs.com/fanqiexin/p/10641922.html

你可能感兴趣的文章
设置border为虚线
查看>>
HDU - 5753 多校联萌3-2
查看>>
lucene3.6.0的文档评估机制
查看>>
ZOJ 3822 Domination DP
查看>>
阿里云的服务器内网互通的前提条件
查看>>
document/window的基础知识
查看>>
复习事件委托
查看>>
wordpress 如何防止盗链
查看>>
请大家一定善用emule!
查看>>
Educational Codeforces Round 13 B. The Same Calendar 水题
查看>>
纠正部分Linux初学者对ctime的误解
查看>>
shell命令快捷键
查看>>
树链剖分(模板)
查看>>
c输出格式
查看>>
mod(%)之规律(除数与被除数的正负分析)
查看>>
C#编程(三十六)----------元组
查看>>
Django 第十课 4.【ORM查询操作】
查看>>
ffmpeg实战系列——001
查看>>
采样器----Debug Sampler
查看>>
ifup / ifdown eth0 / eno1 reports unknown interface when it exists!
查看>>