- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
public UserBean authenticate(String username,String password){
PostGresDAO pg=new PostGresDAO(); //creates new connection
Connection conn=pg.getConnecion(); //return connection object
PreparedStatement ps;
ResultSet rs;
String query="select password,name from scg_users where username=?";
UserBean ub=null;
boolean authenticated=false;
try{
ps=conn.prepareStatement(query);
ps.setString(1, username);
rs=ps.executeQuery();
if(rs!=null){
authenticated=password.equals(rs.getString(1)); //exception raised here
if(authenticated){
ub=new UserBean();
ub.setUser(rs.getString(2));
ub.setUsername(username);
}
}
}
catch(SQLException e){
e.printStackTrace();
}
return ub;
}
我正在使用此代码对用户进行身份验证。用户名和密码从请求参数中提取并传递给此方法进行身份验证。但它抛出一个:
org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
请指教。
最佳答案
错误告诉您确切出了什么问题 - 您没有在 ResultSet 上调用 next()
以获取结果的第一行。
这一行:
if(rs!=null)
据我所知毫无意义;我不相信 executeQuery
会返回 null。如果您的查询有问题,将抛出异常。如果没有结果,它将返回一个空结果集。要查看是否有一行,您应该调用 next()
并检查返回值:
if (rs.next())
另外:
关于java - PSQLException : ResultSet not positioned properly, 也许你需要接下来调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12103915/
我在测试中期望 PSQLException: @Test(expected = org.postgresql.util.PSQLException.class) public void whenAdd
我正在使用 Tomcat 6 和 Postgresql 8.4。我的代码如下所示: try { // Prepared statement inserting something... } cat
嗨!我们使用 Postgres 作为现有应用程序的新数据库。这个问题是最近我们从 mysql 数据库迁移到 postgres 后出现的。迁移到 postgres 后,eclipse BIRT 报告开始
我开发了一个 API 服务,可以将一些数据添加到数据库中。我为实体的 name 字段设置了唯一约束。每当我尝试插入重复的 name 值时,我都会给出以下堆栈跟踪。 Caused by: org.pos
我尝试使用 Postman 将等同于类 Album 的 json 保存到 Postgres。我在 Postgres 中为 Album 类和 Album 类的嵌入对象创建了两个实体 - Duration
更新:我从问题中排除了 Hibernate。我完全修改了问题描述以尽可能简化它。 我有带 noop 触发器的 master 表和 detail 表,master 和 detail 表之间有两种关系:
我的 SQL 请求有问题,当我运行请求时,我收到此消息错误: org.postgresql.util.PSQLException: A result was returned when none wa
我正在使用 Jersey 创建一个 RESTful API,其中实现了 ExceptionMapper 类来捕获所有错误。 public class ExceptionFilter implement
环境 视窗 HikariCP 版本:2.6.0 JDK 版本:1.8.0_65 数据库:PostgreSQL 驱动程序版本:42.0.0 org.postgresql 它工作了一段时间,比如 2 小时
我对我的一个实体有一个唯一的约束,每当我收到一个 PSQLException,只要违反该约束就会发生,我想用一个错误的请求来响应。 这是我尝试实现的异常处理程序: @ControllerAdvice
使用 spring-boot-starter-data-jpa(版本 2.0.4.RELEASE)和 PostgreSQL(版本 9.5.10),我不断收到 PSQLException:查询没有返回结
我正在使用 Hibernate 5.2,我想删除一条记录。 表与另一个表有关系,并不总是可以删除。我想捕获我的 PSQLException 并找到原因。我尝试捕获它,例如HibernateExcept
我正在尝试在表上进行插入并获取自动生成的主键 PreparedStatement pstm = dbcon.prepareStatement(sql,Statement.RETURN_GENE
我遇到一个性能问题,即当访问特定页面且该页面无法加载时,应用程序无法执行数据库查询。错误的堆栈跟踪如下: Caused by: java.rmi.UnmarshalException: Error u
我们已经部署了几个在同一个 tomcat 容器中运行的应用程序,并通过 JNDI 查找连接到我们的 postgresql 数据库。这是经过清理的配置 (server.xml): 这几个月都运行良好,
你好,我正在尝试在 postgresql 数据库中上传文件,但出现错误:这是代码的堆栈跟踪。任何人都可以告诉我哪里出了问题吗?我的查询是- String sql = "insert into proj
我正在尝试将我的数据库和网络应用程序从 PostgreSQL 迁移到 MySQL,该应用程序使用的是 play 2.3.0。 我使应用程序与 MySQL 一起工作,但我还没有进行错误管理,我需要你的帮
public UserBean authenticate(String username,String password){ PostGresDAO pg=new PostGresDAO();
我正在尝试在 PostgreSQL 8.4.2 数据库上运行 hibernate 。每当我尝试运行一个简单的 java 代码时,例如: List users = service.findAllUser
我正在尝试获取 PlaceEntity。我之前存储了一堆 GooglePlaceEntity 对象,其中 @Entity @Table(name = "place") @Inheritance(
我是一名优秀的程序员,十分优秀!