我正在开发一个简单的应用程序,可用于创建啤酒配方。该应用程序采用存储在数据库中的特定值,并使用数学公式和数量(用户输入)来计算啤酒规格。当我运行这段代码(即“计算”按钮下的代码)时,我收到很多异常,其中主要是 java.awt 异常、javax.swing 异常和一些 SQL 异常。列出的变量是正确的,并且变量的拼写没有输入错误。private void Calculate_BrewActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: nameBrew = jTextField1.getText(); Connection connFerm = null; Connection connHops = null; try { vols = Float.parseFloat(jTextField2.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Enter a valid number..."); } fermone = (String) jComboBox1.getSelectedItem(); try { fermmass1 = Float.parseFloat(jTextField6.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Enter a valid number..."); } hop1 = (String) jComboBox9.getSelectedItem(); try { hopmass1 = Float.parseFloat(jTextField10.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Enter a valid number..."); } //............................................................................... try { connFerm = DriverManager.getConnection("jdbc:mysql://localhost:3306/fermentable_info", "root", "nerdswonka"); } catch (SQLException ex) { Logger.getLogger(Create_Page.class.getName()).log(Level.SEVERE, null, ex); } try { connHops = DriverManager.getConnection("jdbc:mysql://localhost:3306/hops_info", "root", "nerdswonka"); } catch (SQLException ex) { Logger.getLogger(Create_Page.class.getName()).log(Level.SEVERE, null, ex); }
1 回答
海绵宝宝撒
TA贡献1809条经验 获得超8个赞
简短回答:将 SELECT发送到数据库,使用返回的方法ResultSet来获取值。类似于(使用虚构的列和表名称,缺少错误处理,关闭,...):
String sql = "SELECT value FROM table WHERE check = ?";
Connection conn = DriverManger.createConnection (...)
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, condition);
ResultSet rset = stmt.executeQuery();
if (rset.next()) { // or, for multiple results: while(rset.next()) {
String result = rset.getString("value");
// TODO use `result`
} // else for error mesage (not found)
显然我会使用try-with resource
并捕获异常,...没有 IDE 或测试编写的代码可能有错误,无法正常工作
添加回答
举报
0/150
提交
取消