• Java(33)_ JDBC指针移动


    package MYSQK;
    import java.sql.*;
    
    /**
     * PreparedStatement 对象可以对sql语句进行预编译,预编译的信息会存在存储该对象中,当相同的sql语句再次执行时,程序
     *   会使用PrepareStatement对象中,而不需再次编译去查询数据库,大大提高了数据的访问效率
     */
    
    public class Insert {
        public  static  void main(String[] args) throws SQLException{
            Connection conn=null;
            PreparedStatement pst =null;
    
            try {
                // 1 加载驱动类
                Class.forName("com.mysql.jdbc.Driver");
                // 2 通过DriverManager获取connection对象
                 String url="jdbc:mysql://192.168.64.129:3306/jdbc?" +
                          "user=root&password=815qza&useUnicode=true&characterEncoding=UTF8";
                 conn = DriverManager.getConnection(url);
                 if (!conn.isClosed()){
                     System.out.println("Succeeded connecting to the Database!");
                 }else{
                     System.out.println("Sorry,failed  connecting to the Database");
                 }
                 // 3 获取pre对象
                  String sql = "select * from USERS" ;
                  pst = conn.prepareStatement(sql);
    
                 //4  使用prepare对象执行sql语句
                   ResultSet rs = pst.executeQuery();
    
                 //5 操作result结果集
                  rs.absolute(2);//将指针定位到第二行数据
                  System.out.println("第二行记录的name为:"+rs.getString("name"));
                  rs.beforeFirst();//指针定位到第一行记录值,这是定位到第一行之前的,调用next方法后才指向第一行
                  rs.next();
                  System.out.println("第一行记录的name为:"+rs.getString("name"));
                  rs.afterLast();//将指针定位到最后一行后
                  rs.previous();//指针前移,定位到最后一行!
                  System.out.println("最后一条记录的name值为:"+rs.getString("name"));
            }catch (ClassNotFoundException e){
                e.printStackTrace();
            }finally {
                // 6 关闭连接
                 pst.close();
                 conn.close();
            }
        }
    }

  • 相关阅读:
    golang 查询数据库并返回json
    logrus日志使用详解
    英文文章写作的一点个人思考
    AKS (1) AKS最佳实践
    Azure Application Insight (3) ASP.NET MVC增加Application Insight
    oracle 创建存储过程后,重新编译错误,如何查看错误信息
    ORACLE 存储过程IN关键字使用问题
    Oracle/for loop循环如何进行跳过、跳出操作
    (转)Golang struct{}的几种特殊用法
    (转)Golang 延迟函数 defer 详解
  • 原文地址:https://www.cnblogs.com/sunnybowen/p/9873834.html
Copyright © 2020-2023  润新知