java.sql.SQLException: Before start of result set解决方案

news/2024/5/18 22:59:11 标签: java, sql, jdbc

java.sql.SQLException: Before start of result set解决方案

    • dao实现类
    • 测试方法
    • 得到的结果
    • 添加resultSet.next()

今天在写老师布置的利用servlet+JDBC+JSP做一个登录平台的时候遇到一个问题,就是按用户名查询数据库中的用户信息时一直包java.sql.SQLException: Before start of result set错,下面是的的部分代码

dao实现类

java">public class Userdao_Impl implements User_dao {
    @Override
    public User select(String name) throws SQLException {
        Connection connection = JDBC_Util.getConnection();
        PreparedStatement ps = connection.prepareStatement("select * from user_info where name = ?");
        ps.setString(1,name);
        ResultSet resultSet = ps.executeQuery();
        if (resultSet!=null){
            User user = new User();
            user.setName(resultSet.getString("name"));
            user.setId(resultSet.getInt("id"));
            user.setPassword(resultSet.getString("password"));
            JDBC_Util.close(ps, connection, resultSet);
            return user;
        }
        JDBC_Util.close(ps, connection, resultSet);
        return null;

    }

测试方法

java"> @Test
    public void sele_name() throws SQLException {
        User_dao dao = new Userdao_Impl();
        String name = "lxh";
        User user = dao.select(name);
        System.out.println(user);
    }

得到的结果

运行结果
这让我很疑惑,我开始还以为是我的sql语句有问题,debug后发现没问题,最后才发现,原来resultSet没有移动光标,直接得到的resultSet就直接去获取此 ResultSet 对象的当前行中指定列的值。后面增加了一行resultSet.next()问题就解决了。

添加resultSet.next()

在这里插入图片描述
有时候一个细节不知道会让你无从下手,希望这个问题可以帮助到和我一样的“新猿”!


http://www.niftyadmin.cn/n/1253931.html

相关文章

BZOJ4589 Hard Nim(博弈+FWT)

即使n个数的异或为0。如果只有两堆&#xff0c;将质数筛出来设为1&#xff0c;做一个异或卷积即可。显然这个东西满足结合律&#xff0c;多堆时直接快速幂。可以在点值表示下进行。 #include<iostream> #include<cstdio> #include<cmath> #include<cstrin…

SpringBoot遇到org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)可能原因

今天在学习发现数据一直查不出来&#xff0c;报的错为org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)&#xff0c;在检查mapper命名&#xff0c;映射&#xff0c;mapper路径等等都没问题之后&#xff0c;还是报错&#xff0c;然后到网上查阅…

scala中创建时间序列_如何在Scala中创建地图?

scala中创建时间序列Scala | 建立地图 (Scala | Creating a map) A map is a special type of collection that stores data in key-value pairs. These are also known as hashtables. The keys that are used to extract the value should be unique. 映射是一种特殊的集合类…

小程序自定义的拍照组件横屏拍照改变方向解决方案

自定义的拍照组件横屏拍照改变方向 本人最近在做一个涉及到拍照的小程序&#xff0c;由于微信原生的拍照组件不能个性化定制&#xff0c;所以就自己改装了一个拍照组件 但是发现&#xff0c;正常竖向拍照的时候可以用&#xff0c; 但是横向拍照&#xff0c;所得的结果还是横…

scala递归函数_Scala中的递归函数

scala递归函数Scala中的递归函数 (Recursion function in Scala) Recursion function is a function that calls itself again and again until a specific condition becomes true. In functional programming, recursion plays a big role and being a functional programmin…

Linux磁盘空间查看、磁盘被未知资源耗尽

Linux系统中&#xff0c;当我们使用rm在Linux上删除了大文件&#xff0c;但是如果有进程打开了这个大文件&#xff0c;却没有关闭这个文件的句柄&#xff0c; 那么Linux内核还是不会释放这个文件的磁盘空间&#xff0c;最后造成磁盘空间占用100%&#xff0c;整个系统无法正常运…

关于io.seata.common.exception.FrameworkException: can not connect to services-server.一种原因及解决方案

背景 近日在学习分布式事务组件Seata&#xff0c;构建了一个订单系统的Demo&#xff0c;有三个微服务之间的调用&#xff0c;采用nacos作为注册中心&#xff0c;在本机实现了Seata的分布式事务的功能&#xff0c;于是我就想试试把其中一个服务部署到另一台主机试试&#xff0c…

scala中命名参数函数_Scala中的默认参数

scala中命名参数函数Scala中的默认参数 (Default parameters in Scala) A default parameter is a parameter that is declared with a default value in arguments. These default values are used by the functions when no value is passed by the programmer while calling…