Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

INSERT statement in JSP using variables

843835Dec 1 2001 — edited Dec 3 2001
People,

I am having problems inserting data from JSP into a MySQL database.
For example the following works perfectly fine:
String query = "INSERT INTO `test2` (`name`, `topic`, `message`) VALUES ('Jane Doe', 'Hi there', 'Example message')";

But, however the problems take place if I try to use variable values as parameters instead of pre-defined strings.

E.g. the following (among a 100 other ways I have tried by now) does not work):
String query = "INSERT INTO `test2` (`name`) VALUES (`" + userName +"`)";
stmt.executeUpdate(query);

This did not work either:
PreparedStatement PStmt = myConn.prepareStatement("insert into (`name`) values (?)");
PStmt.setString(1, new String("`" + userName + "`"));
PStmt.executeUpdate();

I have tried without parenthesis etc. but what I pretty much get every time is:


500 Servlet Exception
java.sql.SQLException: Column not found: Unknown column 'Jane' in 'field
list'
at org.gjt.mm.mysql.MysqlIO.sendCommand(MysqlIO.java:508)
at org.gjt.mm.mysql.MysqlIO.sqlQueryDirect(MysqlIO.java:561)
at org.gjt.mm.mysql.MysqlIO.sqlQuery(MysqlIO.java:646)
at org.gjt.mm.mysql.Connection.execSQL(Connection.java:973)
at org.gjt.mm.mysql.Connection.execSQL(Connection.java:897)
at org.gjt.mm.mysql.Statement.executeUpdate(Statement.java:230)
at org.gjt.mm.mysql.jdbc2.Statement.executeUpdate(Statement.java:99)
at sql3_jsp._jspService(/sql3.jsp:49)
at com.caucho.jsp.JavaPage.service(JavaPage.java:87)
at com.caucho.jsp.JavaPage.subservice(JavaPage.java:81)
at com.caucho.jsp.Page.service(Page.java:474)
at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:166)
at com.caucho.server.http.Invocation.service(Invocation.java:277)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:129)
at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:334)
at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:266)
at com.caucho.server.TcpConnection.run(TcpConnection.java:140)
at java.lang.Thread.run(Thread.java:484)


I do not know much about SQL or JDBC but the material I have seen makes me believe
that I am pretty much doing the right thing.
What is the right way of doing this?
I would greatly appreciate any feedback.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 31 2001
Added on Dec 1 2001
6 comments
3,622 views