I'm getting the error: "Column count doesn't match value count at row 1", yet I have counted and I have the right number of columns and I feel that the error is with the setBoolean() method. I have 8 columns and you will see that I'm passing 7 arguments in my method as one of the columns is an AUTO_INCREMENTE. My coding works perfectly fine if I don't use the setBoolean method replacing it for a setString, setInt or whatever else, but not when I do it with setBoolean().
Here is my code, which I hope makes my problem clearer:
I have an automatic code in my Java that creates the Database & its respective table/s if it doesn't exist:
DB.createDB("userSecInfo");
DB.createTable("accountInfo", "userSecInfo", "("+
"Username VARCHAR(40) NOT NULL, "+
"Pass VARCHAR(40) NOT NULL, "+
"chkPwChange BOOLEAN NOT NULL DEFAULT FALSE, "+
"chkPwCntChange BOOLEAN NOT NULL DEFAULT FALSE, "+
"chkPwNoExpires BOOLEAN NOT NULL DEFAULT FALSE, "+
"chkPwExpires BOOLEAN NOT NULL DEFAULT FALSE, "+
"expirydays INT, "+
"UserID INT NOT NULL AUTO_INCREMENT, "+
"PRIMARY KEY (UserID))");
I have tried defining all the boolean variables for MySQL in different ways such as BOOL, also as BOOLEAN (as it's above) and as TINYINT & TINYINT(1); but the variable definition does not seem to be the problem.
Then the programme, once it has created the Database and the tables, it proceeds to extract the information that the user has inputted and inserts it into the Database:
DB.insertUserSecAccountInfo("userSecInfo", "accountInfo", tFUsername.getText(),
String.copyValueOf(pFPassword.getPassword()), chckbxUserMustChange.isSelected(),
chckbxUserCannotChange.isSelected(), chckbxPasswordNeverExpires.isSelected(),
chckbxPasswordExpiresAnd.isSelected(), Integer.parseInt(tFdays.getText()));
The insertUserSecAccountInfo method as below:
public void insertUserSecAccountInfo(String db_name,String table_name, String Username, String Pass,
boolean chkPwChange, boolean chkPwCntChange, boolean chkPwNoExpires,boolean chkPwExpires,
int expirydays) throws Exception {
try {
openConnection("whatever","password",db_name);
String Query ="INSERT into "+ table_name +" values(?,?,?,?,?,?,?)";
ps = Conexion.prepareStatement(Query);
ps.setString(1, Username);
ps.setString(2, Pass);
ps.setBoolean(3, chkPwChange);
ps.setBoolean(4, chkPwCntChange);
ps.setBoolean(5, chkPwNoExpires);
ps.setBoolean(6, chkPwExpires);
ps.setInt(7, expirydays);
ps.executeUpdate();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error saving data"));
ex.printStackTrace();
} finally {
closeConnection();
}
}
When I run it, it shows me the error: "Column count doesn't match value count at row 1". But then I run basically the same line directly in MySQL as below, and it inserts it perfectly fine:
insert into accountInfo(Username,Pass,chkPwChange,chkPwCntChange,chkPwNoExpires,chkPwExpires,expirydays) values ('Hola','No',true,true,true,true,56);
So it makes me feel it's a problem with the setBoolean() method, which might not be mapping or passing correctly the boolean variables from Java to MySQL; has anyone had any similar issue? I would very much appreciate any guidance with this.
Thank you in advance.