1

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

1
  • LOL...........No Body can solve it? Commented May 16, 2016 at 10:30

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

but I am unable to understand which values?
Are you sure that df.getColumnCount() gives you the same number of columns that you have in the call Prod_SALE (?,?,?,?,?,?,?,?) ?
Unfortunately most DB's don't offer much help in which parameter is missing. Just make sure all of them are set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.