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>
# 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.
# 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.
}
# 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
<%
}
%>
# 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