首页 > 其他分享 >JDBC-ResultSet基本使用

JDBC-ResultSet基本使用

时间:2022-10-28 11:22:24浏览次数:47  
标签:基本 JDBC String res ResultSet stmt throwables catch null

ResultSet:结果集对象 封装查询结果

  • next():游标向下移动一行 判断当前行是否是最后一行末尾(是否有数据) 如果是 则返回false 如果不是则放回true
  • getxxx(参数):获取数据
    • xxx:代表数据类型 如:int getInt(),String getString()    
    • 参数:
      • int:代表列的编号 从1开始 如:getString(1)
      • String:代表列名称 如:getDouble("balance")  

代码案例:

复制代码
  public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet res=null;
        try {
            // 注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 定义SQL语句
            String sql = "SELECT * FROM account";
            // 获取Connection对象
            conn = DriverManager.getConnection("jdbc:mysql:///videopractice", "root", "root");
            // 获取执行SQL的对象 statement
            stmt = conn.createStatement();
            // 执行sql
       // 影响行数 res = stmt.executeQuery(sql); // 处理结果 // 让游标向下移动一行 res.next(); // 获取数据 int id = res.getInt(1); String name = res.getString("name"); double balance = res.getDouble(3); System.out.println(id+"---"+name+"---"+balance);
      // 处理结果 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); } finally {
       // 释放资源 if (stmt != null) { try { stmt.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (res != null) { try { res.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
复制代码

运行结果

注意:

1.游标向下移动一行

2.判断是否有数据

3.获取数据

遍历结果集

代码案例:

复制代码
 public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet res = null;
        try {
            // 1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 定义SQL语句
            String sql = "SELECT * FROM account";
            // 获取Connection对象
            conn = DriverManager.getConnection("jdbc:mysql:///videopractice", "root", "root");
            // 获取执行SQL的对象 statement
            stmt = conn.createStatement();
            // 执行sql
            res = stmt.executeQuery(sql);
            // 处理结果
            // 让游标向下移动一行
            while (res.next()) {
                // 获取数据
                int id = res.getInt(1);
                String name = res.getString("name");
                double balance = res.getDouble(3);

                System.out.println(id + "---" + name + "---" + balance);
            }
      // 处理结果 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); } finally {
       // 释放资源 if (stmt != null) { try { stmt.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (res != null) { try { res.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
复制代码

运行结果

 测试

标签:基本,JDBC,String,res,ResultSet,stmt,throwables,catch,null
From: https://www.cnblogs.com/shenziyi/p/16835181.html

相关文章

  • 第一章 Spring Boot基本介绍
    官方文档官网:https://spring.io/projects/spring-boot学习文档:https://docs.spring.io/spring-boot/docs/current/reference/html/在线API:https://docs.spring.io/spri......
  • SVN常用基本命令windows
      SVN是目前很常用的开源版本控制工具,这里我们介绍一下svn的常用的命令针对的是windows系统,Linux系统的话也是同样的命令只不过是通过命令行的方式。......
  • Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class
    这是在弄那个政策查询系统的时候遇到的报错其实明眼就能看出来是mysql的版本问题,关键是怎么改首先mysql8版本以下的用的是:com.mysql.jdbc.Drivermysql8以上的用的是......
  • 08JMETER之在JDBC元件中执行多条数据
    JDBC元件中执行多条数据 1.JDBCConnectionConfiguration中配置DatabaseURL时在URL后面添加?allowMultiQueries=true2.JDBCConnection Configurationupdate-Quer......
  • Canvas基本绘制操作
    Canvas绘制线条中常见的属性和方法属性(方法)说明linwWidth定义线条的宽度,属性值为整数,默认是1,默认单位是pxlineCap定义线条开始和结尾处的线帽样式,属性值:butt(默认值,无线帽)......
  • UE4 C++实现第三人称角色基本功能
    首先基于Character创建一个角色类,在头文件为其添加弹簧臂和摄像机组件UPROPERTY(VisibleAnywhere,Category="Comp")classUCameraComponent*CameraComp......
  • 【笔记02】Javascript - 基本概念 - (语句、练习)
    Javascript基本概念:语句if、ifelsefor 循环while 循环dowhile 循环switchcasebreakcontinueif、ifelse语法:if(条件){语句}elseif(){语句}else{语句}条件成......
  • 第四章 文件基本操作
    申明:file是python的关键词,不能用于变量名,这里仅为示例,便于说明变量指向打开的文件;文件操作:file=open(filename[,mode[,buffering]]) 1.open()函数第一个参数,即文件名......
  • 060_索引及文档基本操作
    目录基本Rest命令关于索引的基本操作创建索引及文档创建索引,指定字段类型查询索引创建索引及文档查询索引及文档默认的信息扩展命令修改索引PUT覆盖POST修改删除索引关于文......
  • JDBC各个类详情-Connection、Statement
    2、Connection:数据库连接对象功能:1.获取执行sql的对象StatementcreateStatement()PreparedStatementprepareStatment(Stringsql)2.管理事务:开启事务:setAutoCommit(......