gpt4 book ai didi

java - 代码未检测到用户的空输入

转载 作者:行者123 更新时间:2023-12-02 02:28:26 24 4
gpt4 key购买 nike

我的删除按钮有问题。当我在文本字段中输入任何内容并按删除按钮时,我没有收到弹出菜单作为异常(exception)。

 private void billdeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
int id=Integer.parseInt(billidtext.getText());
try (//step2 create the connection object
Connection con = DriverManager.getConnection("jdbc:oracle:thin:localhost:xe","hr","****")) {
Statement stmt=con.createStatement();
stmt = con.createStatement();
String sql = "DELETE FROM bill " +
"WHERE bid = ('"+id+"')";
int w=stmt.executeUpdate(sql);
if(w!=0)
JOptionPane.showMessageDialog(null,"Deleted Successfully!"); //this is displayed successfully
else
JOptionPane.showMessageDialog(null,"value does not exists!");// this is displayed successfully
supplieridtext.setText("");

//view trigger
String sql1="SELECT * FROM bill";

stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(sql1);
//STEP 5: Extract data from result set
billtable.setModel(DbUtils.resultSetToTableModel(rs));
rs.close();

//step5 close the connection object
}
}catch( ClassNotFoundException | SQLException e){
JOptionPane.showMessageDialog(null,"no value entered!");} //this line is not displayed when the text field is empty
}

最佳答案

if (!billidtext.getText().trim().isEmpty()) {
// Do query
}

如前所述,您需要显式捕获 int id=Integer.parseInt(billidtext.getText());java.lang.NumberFormatException,因为它是一个“未经检查的异常”

if (!billidtext.getText().trim().isEmpty()) {
try {
int id=Integer.parseInt(billidtext.getText();
// Do query
} catch (java.lang.NumberFormatException exp) {
JOptionPane.showMessageDialog(null,"Invalid value!");
}
} else {
JOptionPane.showMessageDialog(null,"no value entered!");
}

您还应该使用PreparedStatements

关于java - 代码未检测到用户的空输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47486590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com