JDBC Select * from Where x=(?)
Posted
技术标签:
【中文标题】JDBC Select * from Where x=(?)【英文标题】: 【发布时间】:2015-09-23 04:27:52 【问题描述】:我正在尝试创建一种从数据库中检索客户并将其存储在对象中的方法。但由于某种原因,我的 sql 语句不起作用。
这是我的方法。
public class CustomerDAO
private String url = "jdbc:h2:tcp://localhost/project;IFEXISTS=TRUE";
Set<Customer> customerCollection = new TreeSet<>();
public Customer loginCustomer(String user,String pass) throws SQLException
String sql = "SELECT * FROM Customers WHERE USERNAME=(?) AND PASSWORD=(?) ;";
try(
//get connection form database
Connection dbCon = JdbcConnection.getConnection(url);
//create a statement
PreparedStatement stmt = dbCon.prepareStatement(sql);
)
stmt.setString(1, user);
stmt.setString(2, pass);
ResultSet rs = stmt.executeQuery();
Customer c = new Customer(rs.getString("username"), rs.getString("name"), rs.getString("email"),
rs.getString("address"), rs.getString("creditCardDetails"), rs.getString("password"));
if(c.getUsername().contains(user))
return c;
else
return null;
catch (SQLException ex)
throw new RuntimeException(ex);
我用这个测试过
public static void main(String[] args) throws SQLException
CustomerDAO dao = new CustomerDAO();
Customer c = dao.loginCustomer("jesus", "1234");
System.out.println(c.getName());
System.out.println(c.getPassword());
System.out.println(c.getAddress());
但我得到了这些错误。
Exception in thread "main" java.lang.RuntimeException: org.h2.jdbc.JdbcSQLException: No data is available [2000-187]
at DAO.CustomerDAO.loginCustomer(CustomerDAO.java:54)
at web.NewMain.main(NewMain.java:25)
Caused by: org.h2.jdbc.JdbcSQLException: No data is available [2000-187]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.message.DbException.get(DbException.java:144)
at org.h2.jdbc.JdbcResultSet.checkOnValidRow(JdbcResultSet.java:3186)
at org.h2.jdbc.JdbcResultSet.get(JdbcResultSet.java:3192)
at org.h2.jdbc.JdbcResultSet.get(JdbcResultSet.java:3209)
at org.h2.jdbc.JdbcResultSet.getString(JdbcResultSet.java:302)
at DAO.CustomerDAO.loginCustomer(CustomerDAO.java:43)
... 1 more
Java Result: 1
直接从数据库控制台运行语句可以正常工作。例如
SELECT * FROM Customers WHERE USERNAME='jesus' AND PASSWORD='1234' ;
【问题讨论】:
可能与***.com/questions/26905227/…重复 【参考方案1】:您必须将光标移动到下一行: 添加
rs.next();
之后:
ResultSet rs = stmt.executeQuery();
【讨论】:
【参考方案2】:在调用结果集的 getter 方法之前,您需要将光标向前移动。看看http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#next%28%29
另外,为了避免空结果集,您可以将代码修改为
Customer c=null;
if(rs.next())
c = new Customer(rs.getString("username"), rs.getString("name"), rs.getString("email"),
rs.getString("address"), rs.getString("creditCardDetails"), rs.getString("password"));
if(null != c && c.getUsername().contains(user))
return c;
else
return null;
【讨论】:
非常感谢!现在很有意义。 @MattLaird 如果它解决了您的问题,那么您可以接受答案以上是关于JDBC Select * from Where x=(?)的主要内容,如果未能解决你的问题,请参考以下文章
select * from table where something VS select column from table where something
数据库中select...from....where....各表示啥意思