By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,806 Members | 1,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,806 IT Pros & Developers. It's quick & easy.

retriving data on jsp page from sql databse

P: 1
Sir,
I want to fetch data from a table in sql server database on a jsp page...i make text to be searched input from user through an html page and on jsp page i am getting that data by getParameter() method and passing that variable in sql like query whose syntax is "select * from tablename where columnname like 'f%'" where f is the text received by getparameter()..

my code is:
//search.html

<body>
<form action="" searchbox.jsp" method="post">
<input type="text" name="searchitem" value=" " size="30" maxlength="30">
<input type="submit" name="search" value="search">
</form>
</body>



//searchbox.jsp
<%@ page import = "java.util.*;" %>

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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>

<%! Connection con;
PreparedStatement pstmt;
Statement stmt;
ResultSet rs;
%>

</head>
<body>
<% String str;
str=request.getParameter("searchitem");

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


con=DriverManager.getConnection("Jdbc:Odbc:ds_name ");

Statement st=con.createStatement();
String st1="'";
String st2="%";
String str2=st1.concat(str);
String str3=str2.concat(st2);
String str4=str3.concat(st1);


rs=st.executeQuery("select * from items where itemname like '" +str4+ "'");
int count=0;
if(rs.next())
{

String s=rs.getString(1);


count++;
out.println(s);
if(count>0)
{
out.println("success");
}
else
{
out.println("fail");
}
}


}
catch(Exception ex)
{
}

%>
</body>
</html>
Jul 22 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.