Click here to Skip to main content
14,972,762 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Sir !
i have written the module named registration.java to store the date in the mysql using the java
But When i execute this module gives the exception:
java.sql.SQLException: Column count doesn't match value count at row 1


but it also shows the message that "data has been inserted successfully


Update: Code from comments (AndyLanng)
Java
String name = txtName.getText();  
String Address = txtAddress.getText(); 
String contact_no = txtContact.getText(); 
mail_id = txtEmail.getText(); 
String username = txtUsername.getText(); 
String passcode = pf_password.getText().toLowerCase(); 
String months = (String)cmbPackage.getSelectedItem();  
String key = txtkey.getText(); 


//String start_date = txtStart.getText(dateformat); 

try{  
   Class.forName(JDBC_Driver); 
Connection con = DriverManager.getConnection(DB_URL,
   USER,
   password); 
String sql="insert into registration (name,
   address,
   contact,
   email,
   username,
   Password,
   Package_Duration,
   Voucher_keys,
   EndDate)values(?,
   ?,
   ?,
   ?,
   ?,
   ?,
   ?,
   ?)"; 
PreparedStatement ps = con.prepareStatement(sql); 
ps.setString(1,
   name); 
ps.setString(2,
   Address); 
ps.setString(3,
   contact_no); 
ps.setString(4,
   mail_id); 
ps.setString(5,
   username); 
ps.setString(6,
   passcode); 
ps.setString(7,
   months); 
ps.setString(8,
   key); 
// ps.setDate(1,
   (Date) dt); 

// int i= ps.executeUpdate(); 

// if(i==1) 
//   {  
      JOptionPane.showMessageDialog(this,
      "Data has been inserted Successfully"      ); 
//  
//
   }   if(cmbPackage.getSelectedIndex()==0)   {  
      Calendar calender = Calendar.getInstance(); 
calender.add(calender.DATE,
      +90); 
java.sql.Date d1 = new java.sql.Date(calender.getTime().getTime()); 
// String sql1 = "insert into registration(EndDate)values(?)"; 
ps.setDate(8,
      d1); 
ps.executeUpdate();
   }   else if(cmbPackage.getSelectedIndex()==1)   {  
      Calendar calender = Calendar.getInstance(); 
calender.add(calender.DATE,
      +180); 
java.sql.Date dl = new java.sql.Date(calender.getTime().getTime()); 
// String sql1 = "insert into registration(EndDate) values(?)"; 
ps.setDate(8,
      dl); 
ps.executeUpdate();
   }   else if(cmbPackage.getSelectedIndex()==2)   {  
      Calendar calender = Calendar.getInstance(); 
calender.add(calender.DATE,
      +365); 
// String sql1 = "insert into registration(EndDate) values(?)"; 
java.sql.Date dl = new java.sql.Date(calender.getTime().getTime()); 
ps.setDate(8,
      dl); 
ps.executeUpdate();
   }
}catch (SQLException|ClassNotFoundException e){  
   e.printStackTrace();
}
}


What I have tried:

i have tried statement inteerface but no result
Posted
Updated 10-Jul-17 3:30am
v2
Comments
Richard MacCutchan 10-Jul-17 9:13am
   
You need to show the definition of the column and the code that does the insert. We cannot guess what you have coded; although I suspect you display your success message without testing the result.
Andy Lanng 10-Jul-17 9:34am
   
Question updated
tusharkaushik 10-Jul-17 9:16am
   
I m posting my whole code here
[code moved t question (AndyLanng)]
Andy Lanng 10-Jul-17 9:34am
   
I see you're quite new to this forum, so please take these notes kindly
Please don't post large amounts of code in comments. It loses all formatting and can flood the discussion.
Use the "Improve Question" button that appears below your original question to add details such as code blocks
When replying to someones comments, use the "Reply" button next to their name in their comment. This way they get a notification that you have responded and can reply back.

Thanks ^_^
Richard MacCutchan 10-Jul-17 10:18am
   
You have 9 column names in your SQL statement but only 8 placeholders for values. You are also, as I suspected, displaying a success message before you even execute the SQL.
tusharkaushik 11-Jul-17 0:01am
   
Sorry !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900