如何将 sql 数据库中的列总和显示到 Java 应用程序中?
How to display sum of columns from sql database into Java application?
我有一个存储项目记录的数据库,在名为 'quantity' 的列之一中,我希望能够检索此 'quantity' 金额并显示在我的 java 申请。如果您在下面看到我的代码,您会发现我正在尝试从我的数据库中打印出该金额,但它不起作用。当我 运行 我的应用程序时,它总是 运行ning 而不是尝试。我不确定我是否没有正确执行 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.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.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.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)
您必须先调用 rs.next()
将第一行加载到结果集中,然后才能使用 rs.getString('...')
。
ResultSet rs = pst.executeQuery();
rs.next();
sum = rs.getString("sumquantity");
System.out.print("############################" + sum);
您还应该使用 rs.next()
检查您是否收到了结果。例如:
ResultSet rs = pst.executeQuery();
if (rs.next()) {
sum = rs.getString("sumquantity");
System.out.print("############################" + sum);
} else {
System.out.print("Query didn't return any results");
}
另一件引起注意的事情:您使用 getString()
来获取数值。您可能想改用 rs.getInt()
或 rs.getDouble()
。
我有一个存储项目记录的数据库,在名为 'quantity' 的列之一中,我希望能够检索此 'quantity' 金额并显示在我的 java 申请。如果您在下面看到我的代码,您会发现我正在尝试从我的数据库中打印出该金额,但它不起作用。当我 运行 我的应用程序时,它总是 运行ning 而不是尝试。我不确定我是否没有正确执行 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.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.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.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)
您必须先调用 rs.next()
将第一行加载到结果集中,然后才能使用 rs.getString('...')
。
ResultSet rs = pst.executeQuery();
rs.next();
sum = rs.getString("sumquantity");
System.out.print("############################" + sum);
您还应该使用 rs.next()
检查您是否收到了结果。例如:
ResultSet rs = pst.executeQuery();
if (rs.next()) {
sum = rs.getString("sumquantity");
System.out.print("############################" + sum);
} else {
System.out.print("Query didn't return any results");
}
另一件引起注意的事情:您使用 getString()
来获取数值。您可能想改用 rs.getInt()
或 rs.getDouble()
。