我有一个问题,我不知道如何解决它。我得到这个错误,但我不确定这个错误是否导致我试图用扩展名调用2个函数
com.microsoft.sqlserver.jdbc.SQLServerException: There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1454)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:786)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:685)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:185)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:160)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeUpdate(SQLServerStatement.java:642)
at mylogin.Orders.executeSQLQuery(Orders.java:147)
at mylogin.Orders.Make_OrderActionPerformed(Orders.java:439)
at mylogin.Orders.access$100(Orders.java:23)
这是我的代码似乎有问题(在mylogin.Orders.executeSQLQuery(Orders.java:147))
//execute the sql query REFRESH JTABLE
public void executeSQLQuery (String query,String message) {
Connection con =getConnection();
Statement stt;
Statement sttt;
try{
stt =con.createStatement();
sttt =con.createStatement();
// **1 error 147**
if(((stt.executeUpdate(query))==1) || ((sttt.executeUpdate(query)) )==1)
{
//refresh jtable data
DefaultTableModel model=(DefaultTableModel)jTable_Orders.getModel();
model = (DefaultTableModel)jTable_Prordes.getModel();
model.setRowCount(0);
show_Products_in_Jtable();
show_Orders_in_Jtable();
JOptionPane.showMessageDialog(null,"Data "+message+" Succefully");
}else{
JOptionPane.showMessageDialog(null,"Data Not "+message+ "Error");
}
}catch (Exception ex){
ex.printStackTrace();
}
}
这是我的代码似乎有问题(在mylogin.Orders.Make_OrderActionPerformed)
private void Make_OrderActionPerformed(java.awt.event.ActionEvent evt) {
String query="INSERT INTO Orders(Pro_Id ,Pro_Name,Order_Quantity,Order_Date,Order_Id)VALUES ('"+Pro_Id.getText()+" ','"+Pro_Name.getText()+" ','"+Order_Quantity.getText()+" ,'"+Order_Date.getText()+" ,'"+Order_Id.getText()+" ') ";
executeSQLQuery(query,"Inserted");
}
并且这一点(在mylogin.Orders.access $ 100)
public class Orders extends javax.swing.JFrame {
/**
* Creates new form Orders
*/
public Orders() {
initComponents();
show_Products_in_Jtable();
show_Orders_in_Jtable();
}
我被困在这几天任何帮助??? ty !!!
答案 0 :(得分:1)
你遗漏了这个符号'在,'"+Order_Date.getText()+"'
之后
INSERT INTO Orders(Pro_Id ,Pro_Name,Order_Quantity,Order_Date,Order_Id)VALUES ('"+Pro_Id.getText()+" ','"+Pro_Name.getText()+" ','"+Order_Quantity.getText()+" ,'"+Order_Date.getText()+"' ,'"+Order_Id.getText()+" ') "
这是更好的
String query = " insert into Orders(Pro_Id ,Pro_Name,Order_Quantity,Order_Date,Order_Id)"
+ " values (?, ?, ?, ?, ?)";
PreparedStatement Stmt = conn.prepareStatement(query);
preparedStmt.setString (1, Pro_Id.getText());
preparedStmt.setString (2, Order_Quantity());
preparedStmt.setDate (3, Order_Quantity.getText());
preparedStmt.setBoolean(4, Order_Date.getText());
preparedStmt.setInt (5, Order_Id.getText());
Stmt.execute();
答案 1 :(得分:0)
这是错误的:
INSERT INTO Orders(Pro_Id ,Pro_Name,Order_Quantity,Order_Date,Order_Id)VALUES ('"+Pro_Id.getText()+" ','"+Pro_Name.getText()+" ','"+Order_Quantity.getText()+" ,'"+Order_Date.getText()+" ,'"+Order_Id.getText()+" ') "