我正在尝试使用供应商数据库创建一个swing应用程序。以下Howewer SQL查询由于某种原因返回null。如果在数据库中找不到增值税号,则此查询应创建供应商帐户。这个错误背后的原因是什么?
我的代码:
public void actionPerformed(ActionEvent e) {
try {
String vName = vendorField.getText();
String adress = adressField.getText();
String country = (String) countryField.getSelectedItem();
String vat = vatField.getText();
String pTerms = ptermsField.getText();
String iban = ibanField.getText();
String myDriver = "com.mysql.jdbc.Driver";
String myUrl = "jdbc:mysql://localhost:3306/masterdata_db?autoReconnect=true&useSSL=false";
Connection conn = DriverManager.getConnection(myUrl, "root", "");
Statement st = conn.createStatement();
ResultSet resultSet;
String check = "SELECT * FROM vendorcreation WHERE VAT = '"+vatField.getText()+"'";
resultSet = st.executeQuery(check);
boolean status = true;
if(resultSet.next()!=status){
String sql1 = "INSERT INTO vendorcreation"
+ "(VendorName, Adress, VAT, PaymentTerms, IBAN, Country) VALUES"
+ "(?,?,?,?,?,?)";
PreparedStatement pSt2=conn.prepareStatement(sql1);
pSt2.setString(1, vName);
pSt2.setString(2, adress);
pSt2.setString(3, vat);
pSt2.setString(4, pTerms);
pSt2.setString(5, iban);
pSt2.setString(6, country);
pSt2.executeUpdate(sql1);
JOptionPane.showMessageDialog(frame, "VENDOR ACCOUNT CREATED!");
} else {
JOptionPane.showMessageDialog(frame, "VENDOR ALREADY EXISTS!", "Inane error",JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e1) {
System.err.println("AN ERROR OCCURRED! PLEASE CONTACT YOUR SYSTEM ADMINISTRATOR.");
System.err.println(e1.getMessage());
}
}
答案 0 :(得分:-1)
在select语句中将“=”更改为“LIKE”,
String check = "SELECT * FROM vendorcreation WHERE VAT LIKE '"+vatField.getText()+"'";