I tried to retrieve a data from database. I used jdbc to connect to the database. I have a text field and a submit button. When I type the text in the textfield and click submit, it shows a blank page and there is no error in the console . I didn't get the data from the database.my database name is new and table name is list. Any solution for this?
<!DOCTYPE html>
<html>
<head>
<title>D3</title>
<script>
function getData() {
{
xmlHttp = GetXmlHttpObject();
var id = document.getElementById("input").value;
var url = "main.jsp";
url = url + "?input=" + id;
xmlHttp.onreadystatechange = stateChanged;
xmlHttp.open("GET", url, true);
}
xmlHttp.send(null);
function stateChanged() {
if (xmlHttp.readyState === 4 ||
xmlHttp.readyState === "complete") {
var showdata = xmlHttp.responseText;
document.getElementById("input").value
= showdata;
}
}
}
</script>
</head>
<body>
<div id="type">
<form>
<table>
<tr>
<td>
<input type="text" name="input">
</td>
</tr>
<tr>
<td>
<input type="submit"
name="submit" onclick="getdata();" </td>
</tr>
</table>
</form>
</div>
</body>
</html>
below is the main.jsp file used to get data from the database based on the input form
main.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.*"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.beans.Statement"%>
<%
String ch = request.getParameter("type").toString();
System.out.println(ch);
String data ="";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/new","root","admin321");
PreparedStatement st=con.prepareStatement
("select * from list where input in name=?");
ResultSet rs=st.executeQuery();
while(rs.next())
{
data = rs.getString("name") + ": " +
rs.getString("child name");
}
out.println(data);
System.out.println(data);
}
catch(Exception e) {
System.out.println(e);
}
%>
updated script:
function getData(){
{
xmlHttp=GetXmlHttpObject();
var id=document.getElementById("input").value;
var url="main.jsp";
// java.net.URLEncoder.encode(url, "UTF-8");
// url=URLEncoder.encode(url,"UTF-8");
url=url+"?input="+id;
xmlHttp.onreadystatechange=stateChanged ;
xmlHttp.open("GET",url,true);
}
xmlHttp.send(null);
function stateChanged(){
if(xmlHttp.readyState===4 || xmlHttp.readyState==="complete"){
var showdata = xmlHttp.responseText;
document.getElementById("input").value=showdata;
}
}
return false;
}