JSP Java Code to Search record in SQL database using Apache server


searchstud.jsp

<%@page session="true"import="java.io.*"%>
<html>
<body bgcolor="#d0d0d0">
<br></br>
<br></br>
<br></br>
<h2><center>enter stuent details</center></h2>
<form name="searchstud"action="searchstudinfo.jsp"method="post">
<table border="2"align=center>
<tr><td>
<b>enter the id u want to search:</b>
<input type="text"name="id"/>
<br/>
<br/>
</td></tr>
</table>
<center>
<input type="submit"value="submit"/>
<input type="reset"value="reset"/>
</form>
</body>
</html>

searchstudinfo.jsp

<%@page session="true"import="java.io.*"%>
<html>
<body bgcolor="#d0d0d0">
<br></br>
<br></br>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<table>
<tr>
<th>name</th>
<th>id</th>
<th>age</th>
<th>branch</th>
</tr>
<%
Connection con=null;
Statement stmt=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:student1","sa","sa");
stmt=con.createStatement();
String s2=request.getParameter("id");
%>
<%

ResultSet res=stmt.executeQuery("select * from studentinfo where id1='"+s2+"'");
while(res.next())
{
%>
<tr>
<td><%=res.getString("name1")%></td>
<td><%=res.getString("id1")%></td>
<td><%=res.getString("age")%></td>
<td><%=res.getString("branch")%></td>
</tr>
<%
}
%>
</table>
</body>
</html>


OUTPUT:

1 comments:

Computer Gyan said...

i want to search records either by entering id or either by entering name.
then what will be the command

Post a Comment

If You Are Asking Some Question On This Comment Then Click On Subscribe by email Link