Asif Ameer
Asif Ameer

Reputation: 95

SQL Exception When Sending values from jtable to sql

I am stuck in a problem. When I try to send data from jtable to sql database through stored procedure. here what I am doing:

inserting data to jtable

String b= jLabel116.getText(),c=jTextField6.getText(),e=jTextField20.getText(),f=jTextField25.getText(),g=jTextField48.getText();

float x,y,z;
x=Float.parseFloat(jTextField25.getText());
y=Float.parseFloat(jTextField48.getText());
z=x*y;
String total1=String.valueOf(z);

DefaultTableModel df = (DefaultTableModel) jTable5.getModel();
df.addRow(new Object[]{b,c,d,f,g,total1});
int rowsCount = jTable5.getRowCount();
int Price = 0,Qty=0, total=0;
for(int i=0;i<rowsCount;i++){
Price += Integer.parseInt(jTable5.getValueAt(i,3).toString());
Qty += Integer.parseInt(jTable5.getValueAt(i,4).toString());  
}
total = Price*Qty;
System.out.println(total);
jTextField26.setText(String.valueOf(total));
jTextField51.setText(String.valueOf(total));
jTextField50.setText(String.valueOf(Qty));
jTable5.setModel(df);

Sending Data to Database

try{
    DefaultTableModel df = new DefaultTableModel();
    df=(DefaultTableModel) jTable5.getModel();
             CallableStatement cs=m.XC.prepareCall("call Prod_SALE (?,?,?,?,?,?,?,?)");    
        for (int i = 0; i < df.getRowCount(); i++) {
            for (int j = 0; j < df.getColumnCount(); j++) {
                Object o = df.getValueAt(i, j);
                System.out.println("object from table is  : " +o);
            cs.setString(j+1, (String)o);
          cs.addBatch();
            }
      cs.executeBatch();
          cs.clearParameters();
        }
    }
    catch(Exception ex){
         ex.printStackTrace();

Error Exception:

java.sql.SQLException: Parameter-Set has missing values.
    at sun.jdbc.odbc.JdbcOdbcPreparedStatement.addBatch(JdbcOdbcPreparedStatement.java:1546)

Please help me....I am unable to solve it

Upvotes: 1

Views: 67

Answers (2)

wero
wero

Reputation: 33000

You call addBatch in the inner loop (variable j) after you have set one parameter. Obviously this fails since you have 8 parameters. The Javadoc of PreparedStatement.addBatch says:

Adds a set of parameters to this PreparedStatement object's batch of commands.

You need to move the call to addBatch out of the inner loop. (And probably the executeBatch should be moved out of the outer loop (variable i) too.

DefaultTableModel df = (DefaultTableModel) jTable5.getModel();
CallableStatement cs=m.XC.prepareCall("call Prod_SALE (?,?,?,?,?,?,?,?)");    
for (int i = 0; i < df.getRowCount(); i++) 
{
    for (int j = 0; j < df.getColumnCount(); j++) 
    {
        Object o = df.getValueAt(i, j);
        System.out.println("object from table is  : " +o);
        cs.setString(j+1, (String)o);
    }
    cs.addBatch();
}
cs.executeBatch();

Upvotes: 1

As the message says, you have not set all values in your SQL.

Upvotes: 0

Related Questions