select data from database in jsp

Dear sir,

can you please help me in solving this , please sir........

How to select a particular data from database .........

i will show you the codings .....please refer it

<%@page import="java.sql.*" %>

<%@page import="java.io.*" %>

<%

String s2=request.getParameter("language");

%>

<%

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:langskill");

PreparedStatement pr=con.prepareStatement("select eid from lang where language =?");

pr.setString(2,s2);

// pr.setString(1,eid);

ResultSet rs=pr.executeQuery();

if(rs.next())

{

out.println("the employees are "+eid);

}

}

catch(Exception e)

{

System.out.println(e);

}

//rs.close();

%>

not found

<a href="search.jsp"><h4>Back</h4></a>

</body>

</html>

[1534 byte] By [senthil_yogaa] at [2007-11-27 4:01:16]
# 1

The obvious things

pr.setString(2,s2); should be pr.setString(1,s2);

and you need to get the result of your query:

if (rs.next()){

String eid = rs.getString("eis");

out.println("the employees are "+eid);

}

I'll also add the standard disclaimer that SQL code does not belong in a JSP. This code should be in a servlet/bean.

evnafetsa at 2007-7-12 9:05:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Dear sir/mam,Great !!!!!!!! it worked out........i followed ur steps it worked out...........but iam able to get only one data , not able to recieve allplease give ur valuable suggestions
senthil_yogaa at 2007-7-12 9:05:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

ResultSet#next() returns the next row of the ResultSet. When putting it in an if statement, you will only retrieve the next row. When putting it in a while loop, you will retrieve all rows.

So then do:while (resultSet.next()) {

// Handle each row of resultSet.

}

BalusCa at 2007-7-12 9:05:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Dear Sir/Mam,

thank you very much it really worked out.......

i need one more help , ie

i have codings to update the databse , but it's updating one .....i am able to update only one data but not bunch of datas

i will show u the codings....

<%

String s1=(String)session.getAttribute("eid");

String s3[]=request.getParameterValues("language");

int i=0;

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:langskill");

PreparedStatement pStmt = con.prepareStatement("update lang set language= ? where eid=" + s1+"");

for ( i = 0; i < s3.length; i++)

{

pStmt.setString(1, s3[i]);

//pStmt.setString(1, s1);

pStmt.executeUpdate();

}

}

catch(Exception e)

{

System.out.println(e);

}

%>

<%

if(i>0)

{

%>

<%=(String)session.getAttribute("ename")%>

datas are updated

<%

}

else

{

%>

<%=(String)session.getAttribute("ename")%>

datas are not updated

<%

}

%>

senthil_yogaa at 2007-7-12 9:05:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
You're indeed updating one same row (with the same ID) in a loop. Each update overwrites the previous update. You need to rearrange your code logic. As long as we knows nothing about your requirements, we can't help you further.
BalusCa at 2007-7-12 9:05:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

senthil_yoga, I really think you need to read up on JDBC first. You seem really lost with it. You've had 2 posts in the past few hours both of which are the same code and show incorrect usage of the PreparedStatement and a flawed flow in the code. You're unecessarily creating new topics for the same code.

The other one:

http://forum.java.sun.com/thread.jspa?threadID=5166401

nogoodatcodinga at 2007-7-12 9:05:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...