0

Currently making a java program that grabs data off of a MSAccess Database and some of these errors are extremely frustrating. I keep getting this SQL.Exception : Too few parameters. Expected 1 error on the last remaining bugs in this program.

Little background on the db: It has 3 tables (A player table (11 columns), a team table (3 columns), and an Opponent table (6 columns).

These are both of the functions and I am fairly certain the problem lies in here somewhere

conn = Connect.ConnectDB();
    String sql = "insert into Player ("+"PlayerLastName,"+"PlayerFirstName,"+"Position)"+ "values("+txtid.getText()+ ",'"+txtname.getText()+"','"+txtaddress.getText()+"')" ;
    try{
        pst = conn.prepareStatement(sql);
        pst.executeQuery();
        pst.setString(1, txtid.getText());
        pst.setString(2, txtname.getText());
        pst.setString(3, txtaddress.getText());
        JOptionPane.showMessageDialog(null, txtid.getText() + " Saved");
        UpdateJTable();
        //conn.close();
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }

or this function

        String sql = "select * from Player where PlayerLastName = " +txtid.getText()+ "";
    String pine = null;
    try{
        pst = conn.prepareStatement(sql);

        ResultSet res;
        res = pst.executeQuery();

        pine.equalsIgnoreCase(jTable1.getModel().getValueAt(rowsu, 10).toString());

        while(res.next()){
            JOptionPane.showMessageDialog(null, txtname + " " + txtid.getText() + " has a total of " +"4");//+ pine);//res.getInt("Penalties") );

        }
        UpdateJTable();

    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
1
  • This error message typically resolves to a misspelled column name. Commented Apr 25, 2013 at 15:01

1 Answer 1

1

For one thing, it looks like you're missing single quotes around the last name in the insert statement.

There may be other errors as well, that's just the first thing I noticed.

This should be pretty easy to debug if you just log the sql string before executing it.

EDIT

I think your calls to setString() are also a problem. Here is how you should do this:

conn = Connect.ConnectDB();
    String sql = "insert into Player (PlayerLastName, PlayerFirstName, Position) values(?, ?, ?)";
    try{
        pst = conn.prepareStatement(sql);
        pst.setString(1, txtid.getText());
        pst.setString(2, txtname.getText());
        pst.setString(3, txtaddress.getText());
        pst.execute();
        JOptionPane.showMessageDialog(null, txtid.getText() + " Saved");
        UpdateJTable();
        //conn.close();
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I am very new to sql. When you say log do you mean output into the console?
FYI, it's a very bad practice to concatenate user input into a SQL statement. Use parameters instead. Google "SQL injection".

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.