views:

231

answers:

8

According to the user's input I want to select the record from the database. This is my code:

<%
String jempid=request.getParameter("empid");
out.println(jempid);
int intempid=1223;
Connection conn=null;
String url="jdbc:mysql://localhost/employees";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection(url,"root","");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from empdetails where empnum=jempid");
%>

It throws the following error

javax.servlet.ServletException: java.sql.SQLException: Unknown column 'jempid' in 'where clause

+1  A: 

That looks like too much code in a jsp page.... that said:

... empnum='"+jempid+"');" ......

And also when you are done be sure to close the db too

webclimber
Yes, @rbobby, but not really relevant to the question.
paxdiablo
A: 

Try this:

ResultSet rs=st.executeQuery(
    "select * from empdetails where empnum=" + jempid);

You need to put the value of jempid into the string, not the text "jempid".

paxdiablo
Yes, @rbobby, but not really relevant to the question at hand. We all know you shouldn't allow injection attacks but, if you wanted to cover every way in which an answer was "wrong", they'd all end up being 10,000-word essays.
paxdiablo
You could just as easily say we should have removed the unnecessary code as well such as "int intempid=1223;" :-)
paxdiablo
+3  A: 

Here is your select statement:

select * from empdetails where empnum=jempid

jempid is hardcoded instead of being used as a variable. This will never work unless you change it to the variable value entered by the customer.

alter to:

"select * from empdetails where empnum=" + CleanseUserInput(jempid)

and you're good to go.

Chris Ballance
Thanks for pointing out the sql injection risk
Chris Ballance
And this is a prime example why you shouldn't cover everything in an answer, @rbobby. What if the jempid were a string? That would require quotes. Better to answer the question at hand rather than trying to cover all bases.
paxdiablo
@Pax, good point, but devs are notoriously bad for allowing for sql injection attacks.
Chris Ballance
Still, one up on my answer (and I don't like those new-fangled high-falutin' parameterized queries :-), so +1 for you.
paxdiablo
A: 
ResultSet 
  rs=st.executeQuery("select * from empdetails where empnum=" + jempid + ";")
Learning
A: 

Thanks a lot guys.. Ooooooops! what a silly mistake..

A: 
"select * from empdetails where empnum="+jempid

But you really want to protect this against SQL injections!

x-way
+5  A: 

It's a bad idea to construct SQL using string concatenation - you're just opening yourself up to a SQL injection attack -- ESPECIALLY considering that you're getting the value of "empid" directly from the request. Yikes!

A better approach is use a parametrized query such as below:

PreparedStatement st=conn.prepareStatement("select * from empdetails where empnum=?");
st.setString(1, jempid);
ResultSet rs=st.executeQuery();

Also you should check that jempid is not null.

Marc Novakowski
Good to encompass all cases, but really the only problem here was that he forgot to make jempID a variable. This much effort might further confuse one who wrote the original code.
Chris Ballance
@Chris, right - but other developers who come here can always learn other ways of doing this.
Shivasubramanian A
If someone asks you "Hey - why can't I shoot myself in the foot?" -- do you cock the gun or take it away from them?
Marc Novakowski
Okay maybe a bad analogy, but @Shivasubramanian basically said it - if someone is reading this page either to copy/paste code or (hopefully) to learn, I'd rather have them learn the proper way to construct SQL than just concatenating strings.
Marc Novakowski
A: 

In contrast to other responses, why not use a PreparedStatement?

Basically, you'll have to code like this:

PreparedStatement st=conn.prepareStatement("select * from empdetails where empnum=?");
st.setString(1, jempId);
ResultSet rs=st.executeQuery();

The reason you should use PreparedStatements is because of SQL Injection issues. For example, using the code you posted in the question, a hacker can always type "1;delete * from empdetails" in the textbox from which you select jempid.

Thus the final query formed would be

select * from empdetails where empnum=1;delete * from empdetails

which would result in all data in empdetails getting deleted.

So always use PreparedStatements!!

Shivasubramanian A