java - JDBC select where 查询问题

标签 java jdbc

我的代码:

con = jdbc.connect();
        System.out.println("status of con"+con);
String query = " select * from BmgCPUTotalUsage where CPUserID='"+CPUserID+"' and MessageTypeID='"+MessageTypeID+"'";
        System.out.println(query);
        PreparedStatement statement = con.prepareStatement(query);
    ResultSet result = statement.executeQuery();
    System.out.println(result);
    int QuotaUsed=result.getInt("QuotaUsed"); 

输出:

   status of concom.mysql.jdbc.JDBC4Connection@182600f
     select QuotaUsed from BmgCPUTotalUsage where CPUserID='msdp' and MessageTypeID='SMS'
    com.mysql.jdbc.JDBC4ResultSet@16e1dd8
    java.sql.SQLException: Before start of result set.

任何人都可以告诉我如何删除此异常。并从表中获取 QuotaUsed。其中 QuotaUsed 是一个整数。

最佳答案

您确实需要多阅读 JDBC Tutorial ,但简单地说,试试这个:

ResultSet resultSet = statement.executeQuery();
if (resultSet.next() {
    int quotaUsed = resultSet.getInt("QuotaUsed"); 
}

关于java - JDBC select where 查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6041334/

相关文章:

java - 使用对象生成新值时遇到问题

java - 微调器上的选项菜单

java - 如何在单个查询中从多个数据库中检索数据?

java - 如何使用java JDBC获取MySql的数据库列表 "Schema"名称

java - java.lang.ClassNotFoundException 或无法找到或加载主类错误

java - 使用 PreparedStatement 将日期插入 MySQL 数据库

java - 线程中的异常 "main"java.lang.IllegalArgumentException : Cannot instantiate interface org. springframework.context.ApplicationListener

java - Spring 启动 : SpringBootServletInitializer is deprecated

java - 我应该如何针对 "expression+"模式运行操作?

java - 在准备另一个Statement之前是否需要关闭PreparedStatement