I'm a beginner and i'm trying to make a simple app to display a table from mysql using JavaScript and jquery, json object. This is not giving me any error and but i'm lost as to how to proceed. Please provide your feedback!
This is my books.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class Books {
private int id;
private String title;
private String author;
private float price;
private int qty;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public Books(int id, String title, String author, float price, int qty) {
super();
this.id = id;
this.title = title;
this.author = author;
this.price = price;
this.qty = qty;
}
public Books() {
}
public ArrayList <Books> getBooks(ArrayList<Books> bookList){
Connection conn =null;
ResultSet rset = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ebookshop", "root", "root");
PreparedStatement pst = (PreparedStatement) conn
.prepareStatement("SELECT * from books");
rset = pst.executeQuery();
while (rset.next()){
Books books = new Books();
books.setId(rset.getInt("id"));
books.setAuthor(rset.getString("author"));
books.setTitle(rset.getString("title"));
books.setPrice(rset.getFloat("price"));
books.setQty(rset.getInt("qty"));
bookList.add(books);
}
} catch (SQLException e) {
System.out.println("Could not connect to DB" + e);
} catch (ClassNotFoundException classexpt){
System.out.println("Couldn't find the class" + classexpt);
}
return bookList;
}
}
Servlet File:
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class BooksServlet
*/
@WebServlet("/BooksServlet")
public class BooksServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BooksServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
Books books = new Books();
ArrayList <Books> bookList = new ArrayList <Books>();
bookList = books.getBooks(bookList);
request.setAttribute("bookList", bookList);
JSONObject obj=new JSONObject();
JSONArray arr = new JSONArray();
for(int i = 0 ; i< bookList.size() ; i++)
{
Books b = bookList.get(i);
obj.put("id", b.getId());
obj.put("title", b.getTitle());
obj.put("author", b.getAuthor());
obj.put("price", b.getPrice());
obj.put("qty", b.getQty());
arr.add(obj.toString());
obj = new JSONObject();
}
String address = "DisplayBook.jsp";
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Jsp File:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function showBook(){
$.ajax({
type:"GET",
url:"BooksServlet.java",
dataType: "json",
data: JSON.stringify({ bookList: bookdata }),
success:function(data){
$("#content").html(data);
}
});
}
showBook();
});
</script>
<h3 align="center">Manage Book Details</h3>
<table border="1" align="center">
<tr>
<td> <input type="button" id="display" value="Display All Data" /> </td>
</tr>
</table>
<div id="content" align="center"></div>
<table>
<tr>
<td>id</td>
</tr>
</table>
</body>
</html>
JSp file is where i'm lost. I think i'm sending the data right with the list but not receiving it. Or am i sending the data wrong in the servlet? Any help would be appreciated!