George Kontonikolaou George Kontonikolaou - 2 months ago 13
Java Question

com.microsoft.sqlserver.jdbc.SQLServerException: There are more columns in the INSERT statement than values specified in the VALUES clause

I have a problem and i dont know how to fix it . Iam getting this errors but i am not sure if this error apear cause i am trying to call 2 functions with extends

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)


Here is my code that appear to having the problem (at 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();
}
}


Here is my code that appear to having the problem (at 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");
}


and finnaly this point (at 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();
}


i am stuck at this points for days any help ??? ty !!!

Answer

Your ommiting a this symbol ' after ,'"+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()+" ') "

this is better

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();
Comments