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

[2727 byte] By [wpshopea] at [2007-11-26 16:19:08]
# 1
Ummm... I don't see anything that actually sets the username or description variables.Ahhh, the plans of mice and men.Keith.Message was edited by: corlettk - fuffle fingers.
corlettka at 2007-7-8 22:42:28 > top of Java-index,Java Essentials,New To Java...
# 2
And I am not sure why you are using a variable for the table name, when you are including columns in the insert sql. If you know the columns, you must know the table, right? ~Tim
SomeoneElsea at 2007-7-8 22:42:28 > top of Java-index,Java Essentials,New To Java...
# 3
In Addition, in the statement that is commented out, you are inserting branch as company_type, and when you are binding the parameter, you are using the company_type instead.~Tim
SomeoneElsea at 2007-7-8 22:42:28 > top of Java-index,Java Essentials,New To Java...
# 4

I'm sorry I'm quite new to this, but doesn't the setUsername(String value) and setDescription(String value) in the code supposed to set the variables? I've tested just echoing the values and they seem to be doing the job.

Also when I use just Statement to insert the data it works fine. It just doesn't work with PreparedStatement.

wpshopea at 2007-7-8 22:42:28 > top of Java-index,Java Essentials,New To Java...
# 5
Sorry Tim about the commented out code and the table variable, all that is a product of a series trial and errors, I'll clean it up.
wpshopea at 2007-7-8 22:42:28 > top of Java-index,Java Essentials,New To Java...
# 6

public class AddCompanyTypeBean {

private String companyType = "";

private String description = "";

private String username = "";

private String successMessage = "ok";

private String errorMessage = "bad";

private DBConnection connection = new DBConnection();

private Connection con = null;

public void setErrorMessage(String value){

this.errorMessage = value;

}

public void setSuccessMessage(String value){

this.successMessage = value;

}

public void setUsername(String value){

this.username = value;

}

public void setCompanyType(String value){

this.companyType = value;

}

public void setDescription(String value){

this.description = value;

}

public String toString() {

return "username="+this.username

+", companyType="+this.companyType

+", description="+this.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();

System.out.println("DEBUG: Storing: "+this.toString());

String sql =

"insert into company_types ("

+"company_type, description, posted_by, timestamp_posted"

+") values(?, ?, ?, CURRENT_TIMESTAMP)"

PreparedStatement stmt = con.prepareStatement();

stmt.setString(1, companyType);

stmt.setString(2, description);

stmt.setString(3, username);

int affectedRows = stmt.executeUpdate();

con.close();

if (affectedRows == 1) {

return successMessage;

} else {

return errorMessage;

}

}

}//class

... I took the liberty of cleaning up your code so I could follow it ...

... and I still can't see where you are setting any of the values ... but that maybe coz you haven't posted all your code.

Just try my version, with the DEBUG message in the storeData method.

Keith.

Message was edited by: corlettk

corlettka at 2007-7-8 22:42:28 > top of Java-index,Java Essentials,New To Java...
# 7
Thanks Keith. I'll give it shot.The values actually come from html form data passed on to a jsp file that uses this bean.Will post if it works out.Thanks again.
wpshopea at 2007-7-8 22:42:29 > top of Java-index,Java Essentials,New To Java...
# 8
Still didn't work...
wpshopea at 2007-7-8 22:42:29 > top of Java-index,Java Essentials,New To Java...