PreparedStatement not inserting Strings
Hi there, I've written a simple bean to insert about 4 values to MySQL 4.1.
The test shows that the operation is successful at runtime. However, when I checked on the sql server from the terminal only the CURRENT_TIMESTAMP went through. The other fields just come out blank.
I've already done a test to check if the code actually captures the parameters sent to it and it all checks out. I can print the values on screen but for some reason won't insert to the database.
I tried changing to just Statement instead and it does the job. However, I need to use PreparedStatement to take care of escaping special characters. For some reason it doesn't work.
Can someone help me please.
The code is as follows(without the imports):
public class AddCompanyTypeBean {
private String companyType = "";
private String description = "";
private String username = "";
private String successMessage = "";
private String errorMessage = "";
private String table = "company_types";
private Connection con = null;
private DBConnection connection = new DBConnection();
public void setErrorMessage(String value){
errorMessage = value;
}
public void setSuccessMessage(String value){
successMessage = value;
}
public void setUsername(String value){
username = value;
}
public void setCompanyType(String value){
companyType = value;
}
public void setDescription(String value){
description = value;
}
public String getAll(){
return username + companyType + description;
}
public String storeData()throws NullPointerException, SQLException, Exception{
connection.setDriver(DBConfigConstants.JDBC_MYSQL_DRIVER);
connection.setHost(DBConfigConstants.DBHOST);
connection.setUser(DBConfigConstants.DBUSER);
connection.setPassword(DBConfigConstants.DBPASSWORD);
connection.setDatabase(DBConfigConstants.DBNAME);
con = connection.getConnection();
//String insertQuery = "insert into "+table+"(branch, description, timestamp_posted, posted_by) values('"+branch+"', '"+description+"', CURRENT_TIMESTAMP, '"+username+"')";
PreparedStatement stmt = con.prepareStatement("insert into "+table+"(company_type, description, posted_by, timestamp_posted) values(?, ?, ?, CURRENT_TIMESTAMP)");
stmt.setString(1, companyType);
stmt.setString(2, description);
stmt.setString(3, username);
int affectedRows = stmt.executeUpdate();
con.close();
if (affectedRows > 0){
return successMessage;
}else{
return errorMessage;
}
}
}//class

