0

Im getting an error message while inserting values from the form. Error is Column count doesn't match value count at raw 1. My 5th string value must be stored as date data type in sql.

String s1=this.txtUsername.getText();
String s2=this.txtPassword.getText();
String s3=this.txtName.getText();
String s4=this.txtAddress.getText();
String s5=this.txtContractEndDetails.getText();


         connection getcon = new connection();
         Connection conn;


    try{
        conn=getcon.creatConnection();
        Statement stmt=conn.createStatement();

        stmt.executeUpdate("insert into TravelGuide(username,password,name,address,contract_end_date)values ('"+s1+"','"+s2+"','"+s3+"','"+s4+"'+'"+s5+"')");



        }
    catch(Exception ex){
        JOptionPane.showMessageDialog(PanelTG, ex.getMessage(),"Error Occured",2);
    }

Here's my relevant table for SQL query.

  create table TravelGuide(
  username char(20),
  password char(20),
  name varchar(100),
  address varchar(150),
  contract_end_date date,
  constraint TravelGuide_PK primary key(username)
  );

edited

Im confused about

preparedStatement("insert into TravelGuide (username,password,name,address,contract_end_date) values (?, ?, ?, ?, ?)");

Could u explain this furthermore..

Is this right? The codes which are inside ** marks are giving errors illegal start of expression and cannot find symbol.. Please help me..

try {
    conn=getcon.creatConnection();

    **String sql="insert into TravelGuide("+"username,"+"password,,"+,"name,"+"address,"+"contract_end_date)"+"values(?,?,?,?,?)";**
    PreparedStatement stmt = conn.**preparedStatement**(sql);
    java.sql.Date dtValue = java.sql.Date.valueOf(s5); 

    stmt.setString(1, s1);
    stmt.setString(2, s2);
    stmt.setString(3, s3);
    stmt.setString(4, s4);
    stmt.setDate(5, dtValue);
    stmt.executeUpdate();                
}
6
  • 2
    DON'T USE DYNAMIC SQL! You're opening yourself to SQL injection attacks. Use a PreparedStatement and bind parameters. Commented Apr 11, 2012 at 3:12
  • 2
    You need to learn about PreparedStatements and bind variables and then do something like this: stackoverflow.com/questions/370852/… Commented Apr 11, 2012 at 3:13
  • 1
    My name is Bobby Tables. Where can I login to your system? Commented Apr 11, 2012 at 5:38
  • My apologies for the typo. The method name is prepareStatement, not preparedStatement. That would be the reason for the "cannot file symbol" error. Commented Apr 11, 2012 at 11:56
  • By the way, there's no need to construct the sql string that way. You don't need to do a concatenation of literals. You can just use one long literal: "insert into TravelGuide (username,password,name,address,contract_end_date) values (?, ?, ?, ?, ?)" Commented Apr 11, 2012 at 11:57

2 Answers 2

4

Inside the try, do this instead:

conn = getconn.creatConnection();
PreparedStatement stmt = conn.prepareStatement("insert into TravelGuide (username,password,name,address,contract_end_date) values (?, ?, ?, ?, ?)");

java.sql.Date date = someFunctionToConvertYourDateStringToADate(s5);

stmt.setString(1, s1);
stmt.setString(2, s2);
stmt.setString(3, s3);
stmt.setString(4, s4);
stmt.setDate(5, date);
stmt.executeUpdate();

[and so on]

That way you are protected from SQL injection attacks and you don't have to worry about how to massage your string into a format a specific database requires for a date column. The JDBC driver will handle that for you, given a java.sql.Date object.

Sign up to request clarification or add additional context in comments.

3 Comments

+1. A variation would be to pass the date as a string and let MySQL figure it out with some to_date function it probably has.
Im bit confused about preparedStatement("insert into TravelGuide (username,password,name,address,contract_end_date) values (?, ?, ?, ?, ?)"); Could u explain this furthermore.. Btw i changed the conversion as this java.sql.Date dtValue = java.sql.Date.valueOf(s5);
Sorry. typo on my end. it's conn.prepareStatement, not conn.preparedStatement.
0
stmt.executeUpdate("insert into TravelGuide(username,password,name,address,contract_end_date)values ('"+s1+"','"+s2+"','"+s3+"','"+s4+"'**+**'"+s5+"')");

why + ,i think i should be ,

Comments

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.