
Hi, can somebody help me?
I have a jsp page. in that i want to get data from the database where username is matched. i tried it. but can't get the output. here is the code. please help me. ALL DATABASE CONNECTIONS ARE OK. THE ERROR IS SHOWN IN "WHERE". IT SAYS INCORRECT SQL SYNTAX
<html>
.....
<body>
<%
//table name is "register" have Name and Username filed.
String uname=request.getParameter("username");
String sql;
sql="SELECT * FROM register WHERE Username="+uname+"";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
%>
NAME: <%=rs.getString(1)%>
USERNAME: <%=rs.getString(2)%>
<%
}
%>
</body>
</html>

hi friend,
I think in your sql query you missed the single quote ( ' ) after Username. This is required because you are searching for a text and text should be written within the single quote such as in your example your query should be written as follows :
SELECT * FROM register WHERE Username='"+uname+"'"
you may try the following code :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*;" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Delete Data</title>
</head>
<body>
<form action="#">
<table>
<tr>
<td>Enter Username</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Register"/></td>
</tr>
</table>
</form>
<%! String driverName = "com.mysql.jdbc.Driver";%>
<%! String url = "jdbc:mysql://localhost:3306/record";%>
<%! String user = "root";%>
<%! String psw = "root";%>
<%
String uname = request.getParameter("username");
if(uname != null)
{
Connection con = null;
PreparedStatement ps = null;
try
{
Class.forName(driverName);
con = DriverManager.getConnection(url,user,psw);
String sql = "SELECT * FROM register WHERE Username='"+uname+"'";
ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
%>
Name = <%=rs.getString("Name") %>
<%
}
}
catch(SQLException sqe)
{
out.println(sqe);
}
}
%>
</body>
</html>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.