I have this table in MySQL:
CREATE TABLE CanteenMan.Foods (
id TINYINT PRIMARY KEY AUTO_INCREMENT,
type ENUM("soup","main","desert") DEFAULT "soup",
name VARCHAR(30) NOT NULL,
price FLOAT NULL DEFAULT NULL
)ENGINE = INNODB;
So if i want to insert new food, I should do this:
INSERT INTO Foods(id,type,name,price)
VALUES(NULL,"soup","somename",3.3);
And here is my problem now...I am using JDBC to add new foods in the table:
public void addFood(Meal f) throws ClassNotFoundException, SQLException {
Class.forName (dbid.driver);
Connection c = (Connection) DriverManager.getConnection(dbid.url,dbid.user,dbid.pass);
PreparedStatement ps = (PreparedStatement) c.prepareStatement("INSERT INTO foods(id,type,name,price"
+ "VALUES(NULL,?,?,?)");
ps.setString(1,f.getType()); // here throws SQLException
ps.setString(2, f.getName());
ps.setFloat(3,f.getPrice());
int rowsInserted = ps.executeUpdate();
System.out.println("Rows inserted:"+rowsInserted);
}
But this throws SQL Exception because MySQL wants here
+ "VALUES(NULL,?,?,?)");
to surround the values with "".Is it any way to make this query to be accepted from the MySQL server ?
Mealclass, especially what the return value ofgetType()is.