Ajax - Mysql

Ajax - Mysql

onetwoonetwo Posts: 9Questions: 0Answers: 0
edited April 2014 in General
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.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    As required in the forum rules, please link to a test case showing the issue: http://datatables.net/forums/discussion/12899/post-test-cases-when-asking-for-help-please-read

    Allan
  • onetwoonetwo Posts: 9Questions: 0Answers: 0
    http://live.datatables.net/jiyavil/2/edit
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Thank you, but there is no `"sAjaxSource": "connection"` source on this server. I can't see what data your script is returning.

    Allan
  • onetwoonetwo Posts: 9Questions: 0Answers: 0
    Hi @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.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Thanks, but I'm not a C# (assuming that is what it is) developer. If you are having an issue with the server-side script I would suggest you ask in a forum for that language.

    Allan
This discussion has been closed.