Ajax - Mysql
Ajax - Mysql
Hello all,
The idea: I want to get data from my mysql database and put them into my table.
Problem: The table is still empty and i get no response from the Server but also no error code.
I think the problem is in my get method?!
[code]
<%@ 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">
DataTables example
@import "media/css/demo_table.css";
var oTable;
$(function () {
oTable = $("#data").dataTable({
"sDom": 'T<"clear">lfrtip',
"processing": true,
"sAjaxSource": "connection",
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aoColumns": [
{ "mData": "FirstName" },
{ "mData": "LastName" },
],
});
});
FirstName
LastName
[/code]
[code]
@WebServlet("/connection")
public class connection extends HttpServlet {
private static final long serialVersionUID = 1L;
public connection() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
List deviceList = verbindung.getAllDevice();
DataTableDevice dtd = new DataTableDevice();
dtd.setAaData(deviceList);
Gson gson = new Gson();
String json = gson.toJson(dtd);
out.print(json);
}
}
[/code]
[code]
public class verbindung {
static Connection currentCon = null;
static ResultSet rs = null;
public static ArrayList getAllDevice() {
Statement stmt = null;
String searchQuery = "SELECT * FROM People_devices";
ArrayList deviceList = new ArrayList();
try {
Class.forName("com.jdbc.mysql.Driver");
currentCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/checkData?user=****&password=****");
stmt = currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
while(rs.next()) {
setAndGet device = new setAndGet();
device.setFirstName(rs.getString("FirstName"));
device.setLastName(rs.getString("LastName"));
deviceList.add(device);
}
currentCon.close();
stmt.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return deviceList;
}
}
[/code]
[code]
public class setAndGet {
private String FirstName;
private String LastName;
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
}
[/code]
[code]
public class DataTableDevice {
List AaData;
public List getAaData() {
return AaData;
}
public void setAaData(List aaData) {
AaData = aaData;
}
}
[/code]
The Imports are still there, but because of the lenght of this post i let them away.
Tank you.
The idea: I want to get data from my mysql database and put them into my table.
Problem: The table is still empty and i get no response from the Server but also no error code.
I think the problem is in my get method?!
[code]
<%@ 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">
DataTables example
@import "media/css/demo_table.css";
var oTable;
$(function () {
oTable = $("#data").dataTable({
"sDom": 'T<"clear">lfrtip',
"processing": true,
"sAjaxSource": "connection",
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aoColumns": [
{ "mData": "FirstName" },
{ "mData": "LastName" },
],
});
});
FirstName
LastName
[/code]
[code]
@WebServlet("/connection")
public class connection extends HttpServlet {
private static final long serialVersionUID = 1L;
public connection() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
List deviceList = verbindung.getAllDevice();
DataTableDevice dtd = new DataTableDevice();
dtd.setAaData(deviceList);
Gson gson = new Gson();
String json = gson.toJson(dtd);
out.print(json);
}
}
[/code]
[code]
public class verbindung {
static Connection currentCon = null;
static ResultSet rs = null;
public static ArrayList getAllDevice() {
Statement stmt = null;
String searchQuery = "SELECT * FROM People_devices";
ArrayList deviceList = new ArrayList();
try {
Class.forName("com.jdbc.mysql.Driver");
currentCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/checkData?user=****&password=****");
stmt = currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
while(rs.next()) {
setAndGet device = new setAndGet();
device.setFirstName(rs.getString("FirstName"));
device.setLastName(rs.getString("LastName"));
deviceList.add(device);
}
currentCon.close();
stmt.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return deviceList;
}
}
[/code]
[code]
public class setAndGet {
private String FirstName;
private String LastName;
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
}
[/code]
[code]
public class DataTableDevice {
List AaData;
public List getAaData() {
return AaData;
}
public void setAaData(List aaData) {
AaData = aaData;
}
}
[/code]
The Imports are still there, but because of the lenght of this post i let them away.
Tank you.
This discussion has been closed.
Replies
Allan
Allan
my 'connection' source looks so :
[code]
@WebServlet("/connection")
public class connection extends HttpServlet {
private static final long serialVersionUID = 1L;
public connection() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
List deviceList = connect.getAllDevice();
DataTableDevice dtd = new DataTableDevice();
dtd.setAaData(deviceList);
Gson gson = new Gson();
String json = gson.toJson(dtd);
out.print(json);
}
}
[/code]
And this code get the data from this code below:
[code]
public class connect {
static Connection currentCon = null;
static ResultSet rs = null;
public static ArrayList getAllDevice() {
Statement stmt = null;
String searchQuery = "SELECT * FROM People_devices";
ArrayList deviceList = new ArrayList();
try {
Class.forName("com.jdbc.mysql.Driver");
currentCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/checkData?user=****&password=****");
stmt = currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
while(rs.next()) {
setAndGet device = new setAndGet();
device.setFirstName(rs.getString("FirstName"));
device.setLastName(rs.getString("LastName"));
deviceList.add(device);
}
currentCon.close();
stmt.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return deviceList;
}
}
[/code]
The getter and setter method is post above.
Allan