I would like to know how to retrieve values from database without reloading the page. I only know a little of javascript and I am using data source jndi
for my database. I am currently following the MVC2 model.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import = "java.sql.*" %>
<%@page import = "javax.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=UTF-8">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(){
$( "button" ).click(function() {
$("#users").change(function(){ //A function to execute each time the event is triggered.
var value = $(this).val(); //allows you to pass an array of element values
$.get("index.jsp",{q:value},function(data){
$("#javaquery").html(data);
});
});
});
});
</script>
</head>
<body>
<div id="users">
<button value="1">1</button>
<button value="2">2</button>
</div>
<br />
<div id="javaquery"><b>Name will be displayed here</b></div>
<%
String name = "";
String q = request.getParameter("q");
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.0.14:1433/demolangako", "demolangme", "demolangme");
Statement smt = con.createStatement(); //Create Statement to interact
ResultSet r = smt.executeQuery("select * from users where(id='" + q + "');");
while (r.next()) {
name = r.getString("name");
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
Name:<%out.print(name);%>
</body>
</html>
Currently I have this code, but it doesn't work with my buttons. Any help is appreciated.