sampurna sampurna - 4 months ago 12
MySQL Question

How to required tuple from database using JSP

I need to retrieve as user specifies in previous page So I written code in this may But I got that

Unable to connect to databasejava.sql.SQLException: you have an error in your SQL syntax;check the manual that corresponds to your MYSQL server version for right syntax to use near '==t)'

My code

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!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>Insert title here</title>
</head>
<body>
<%
try
{
String t=request.getParameter("val");
String connectionURL = "jdbc:mysql://localhost/tables";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "");
Statement st = connection.createStatement();
String query="select * from details where sno==t)";
ResultSet rs = st.executeQuery(query);
if(!connection.isClosed())
{
while(rs.next())
{
out.println(rs.getString(1));
out.println(rs.getString(2));
out.println(rs.getString(3));
out.println("<br>");
}
}
}
connection.close();
}
catch(Exception ex)
{
out.println("Unable to connect to database"+ex);
}
}
}
%>
</body>
</html>

Answer

Comparison operator in MySQL is = not ==.

code snippet:

int param = Integer.parseInt(t);
String query="select * from details where sno = "+param;
System.out.println("query="+query);    
ResultSet rs = st.executeQuery(query);

In fact you should specify like below to comply with the datatypes:

out.println(rs.getInt("sno")); 
out.println(rs.getInt("Id")); 
out.println(rs.getString("name"));
out.println(rs.getInt("mobile"));