我有一个数据库,它将我的项目的记录存储在一个名为'quantity'的列中,我希望能够检索这个'quantity'数量并显示在我的java应用程序中。如果你看到我下面的代码,你可以看到我正试图打印出从我的数据库,但它是不起作用的金额。当我运行应用程序时,运行的总是捕获,而不是尝试。我不确定是否可能我没有做的sql代码正确,但这只是我的猜测,如果你知道如何解决这个问题,请让我知道谢谢你。
private void initComponents() {
private String sum;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con1 = DriverManager.getConnection("jdbc:mysql://localhost/restock", "root", "password");
String template = "SELECT SUM(quantity) as sumquantity FROM buysupply WHERE itemtype IN ('Disposable mask', 'Clothed mask', 'N95 mask')";
PreparedStatement pst = con1.prepareStatement(template);
ResultSet rs = pst.executeQuery();
sum = rs.getString("sumquantity");
System.out.print("############################" + sum);
} catch (ClassNotFoundException ex) {
Logger.getLogger(stock.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(stock.class.getName()).log(Level.SEVERE, null, ex);
}
}
关于您的信息,错误如下:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Aug 06, 2020 1:22:03 AM pleasework.stock initComponents
SEVERE: null
java.sql.SQLException: Before start of result set
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.result.ResultSetImpl.checkRowPos(ResultSetImpl.java:492)
at com.mysql.cj.jdbc.result.ResultSetImpl.getString(ResultSetImpl.java:845)
at com.mysql.cj.jdbc.result.ResultSetImpl.getString(ResultSetImpl.java:863)
at pleasework.stock.initComponents(stock.java:92)
at pleasework.stock.<init>(stock.java:30)
at pleasework.stock$16.run(stock.java:823)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
1条答案
按热度按时间kwvwclae1#
你得打电话
rs.next()
在使用之前将第一行加载到结果集中rs.getString('...')
.你也应该使用
rs.next()
检查是否收到结果。例如:另一个吸引注意力的东西:你用
getString()
获取数值。你可能想用rs.getInt()
或者rs.getDouble()
相反。