sql query? What is wrong with this?

High everyone. I was trying to solve problem with jsp-servlet combination. I nailed the problem down to the last point, but surprisingly I got an error message which is completely out of any reason.

Here is the problem. Im using tomcat 5.5 and MySql DB.

I've got a servlet with a method that returns ID of the member according to his name. Here is the relevant code:publicstaticint getMemberID (String name){

int id = 5;

try{

Class.forName("com.mysql.jdbc.Driver").newInstance();

.

.

ResultSet rs = st.executeQuery("select MemberID from member where MemberName =" + name);

if (rs!=null && rs.next()){

id = rs.getInt("MemberID");

st.close();

conn.close();

}

}

I call this method from the JSP:

<% int a = ThreadMembers.getMemberID("Merlin");.....

Now what happens...I got the SQL exception message:

Unknown column 'Merlin' in 'where' clause !?!?!

Of course I checked my database and tried the same query manually, everything is OK. There are only two records in my member table.

And, just in case, I entered manually the next query:

select MemberID from member where Merlin = 'Merlin':)

and of course got the same error message.

I would really like to hear if anybody had similar problem, or is able to explain what's going on here.

Thank you

[1853 byte] By [xAxisa] at [2007-11-27 5:38:56]
# 1

Your SQL statement is wrong, you should enclose text in ' '

ResultSet rs = st.executeQuery("select MemberID from member where MemberName = '"+name+"'");

But you are vulnerable to SQL injections with that method, you should consider using PreparedStatement instead.

kaderuda at 2007-7-12 15:13:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

At least this has nothing to do with JSP/JSTL. It may reach JDBC http://forum.java.sun.com/forum.jspa?forumID=48 , but it is still just an ordinary SQL problem. Starting reading some SQL tutorials will help you to gain more SQL knowledge. Here are some useful links in one page: http://www.google.com/search?q=sql+tutorial

BalusCa at 2007-7-12 15:13:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...