没有可用数据

时间:2016-04-13 18:12:36

标签: java jquery jtable

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>jTable</display-name>
    <servlet>
        <servlet-name>DateController</servlet-name>
        <servlet-class>com.servlet.DateController</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>InfoController</servlet-name>
        <url-pattern>/InfoController</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>DateController</servlet-name>
        <url-pattern>/DateController</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>schedule.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>Controller</servlet-name>
        <servlet-class>com.servlet.Controller</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>InfoController</servlet-name>
        <servlet-class>com.servlet.InfoController</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Controller</servlet-name>
        <url-pattern>/Controller</url-pattern>
    </servlet-mapping>
</web-app>

我的jtable(jquery)显示“没有数据可用”。实际上我的表包含数据。我不知道我做错了什么 请参阅下面的代码

模型类

public class ScheduleDate {
String sapCode;
String projectName;
String country;
public String getSapCode() {
    return sapCode;
}
public void setSapCode(String sapCode) {
    this.sapCode = sapCode;
}
public String getProjectName() {
    return projectName;
}
public void setProjectName(String projectName) {
    this.projectName = projectName;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
} 
}

DAO课程:

public class DateDao {
  private Connection dbConnection;
private PreparedStatement pStmt;

public DateDao (){
     dbConnection = DBUtility.getConnection();
}
public void addSchedule(ScheduleDate sch) {

    String insertQuery = "INSERT INTO scheduledate(sap_code,project_long_name,country) VALUES (?,?,?)";
try {
    pStmt = dbConnection.prepareStatement(insertQuery);
    pStmt.setString(1, sch.getSapCode());
    pStmt.setString(2, sch.getProjectName());

    pStmt.executeUpdate();
} catch (SQLException e) {
    System.err.println(e.getMessage());
}
}

public void deleteDate(String sapcode) {
String deleteQuery = "DELETE FROM scheduledate WHERE sap_code = ?";
try {
    pStmt = dbConnection.prepareStatement(deleteQuery);
    pStmt.setString(1, sapcode);

    pStmt.executeUpdate();
} catch (SQLException e) {
    System.err.println(e.getMessage());
}
}
public void updateDate(ScheduleDate sch)  {


String updateQuery = "update scheduledate set project_long_name=?,country=? where sap_code=?";
     try{

        pStmt = dbConnection.prepareStatement(updateQuery);


    pStmt.setString(1, sch.getProjectName());
            pStmt.setString(2, sch.getCountry());
           pStmt.setString(3, sch.getSapCode());


       pStmt.executeUpdate();
        }
     catch (SQLException e) {
    System.err.println(e.getMessage());
    }
}
public List<ScheduleDate> getAllDate() {
List<ScheduleDate> in = new ArrayList<ScheduleDate>();

String query = "select sap_code,project_long_name,country from scheduledate";
try {
    Statement stmt = dbConnection.createStatement();
    ResultSet rs = stmt.executeQuery(query);

    while (rs.next()) {
        ScheduleDate i = new ScheduleDate();

        i.setSapCode(rs.getString(1));
                    i.setProjectName(rs.getString(2));
                    i.setCountry(rs.getString(3));


                    in.add(i);
    }
} catch (SQLException e) {
    System.err.println(e.getMessage());
}
return in;
}
}

我的servlet控制器

public class DateController extends HttpServlet {

private static final long serialVersionUID = 1L;
private HashMap<String, Object> JSONROOT = new HashMap<String, Object>();
private  DateDao dao ;

public  DateController(){
    dao = new DateDao();
}


protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

            String action = request.getParameter("action");
    List<ScheduleDate> scheduleList = new ArrayList<ScheduleDate>();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    response.setContentType("application/json");

    if (action != null) {
        try {
            if (action.equals("list")) {
                // Fetch Data from Student Table
                          scheduleList= dao.getAllDate();

                // Return in the format required by jTable plugin
                JSONROOT.put("Result", "OK");
                JSONROOT.put("Records",scheduleList);

                // Convert Java Object to Json
                String jsonArray = gson.toJson(JSONROOT);

                response.getWriter().print(jsonArray);

            } else if (action.equals("create") || action.equals("update")) {
                ScheduleDate sch = new ScheduleDate();
                if (request.getParameter("sapCode") != null) {

                    String sapCode = request.getParameter("sapCode");
                                            sch.setSapCode(sapCode);   
                }

                if (request.getParameter("projectName") != null) {
                    String projectName = request.getParameter("projectName");
                    sch.setProjectName(projectName);
                }
                                    if (request.getParameter("country")!= null) {
                    String country = request.getParameter("country");
                    sch.setCountry(country);
                }
                if (action.equals("create")) {
                    // Create new record
                    dao.addSchedule(sch);
                } else if (action.equals("update")) {
                    // Update existing record
                    dao.updateDate(sch);
                }

                // Return in the format required by jTable plugin
                JSONROOT.put("Result", "OK");
                JSONROOT.put("Record", sch);

                // Convert Java Object to Json
                String jsonArray = gson.toJson(JSONROOT);
                response.getWriter().print(jsonArray);
            } else if (action.equals("delete")) {
                // Delete record
                if (request.getParameter("sapCode") != null) {
                    String sapCode = request.getParameter("sapCode");

                    dao.deleteDate(sapCode);

                    // Return in the format required by jTable plugin
                    JSONROOT.put("Result", "OK");

                    // Convert Java Object to Json
                    String jsonArray = gson.toJson(JSONROOT);
                    response.getWriter().print(jsonArray);
                }
            }
        } catch (Exception ex) {
            JSONROOT.put("Result", "ERROR");
            JSONROOT.put("Message", ex.getMessage());
            String error = gson.toJson(JSONROOT);
            response.getWriter().print(error);
        }
    }
}   
}

我的jsp文件(由javasscript和html组成)。

   <script type="text/javascript">
     
  $(function() {
        $( "#tabs" ).tabs();
  });	
  $(document).ready(function(){
		$('#StudentTableContainer').jtable()({
			title : 'Schedule Date',
                      //  paging: true, //Enable paging
                        //pageSize: 10, //Set page size (default: 10)
                        //sorting: true, //Enable sorting
            //defaultSorting: 'Name ASC',
			actions : {
				listAction  :  'DateController?action=list',
				createAction : 'DateController?action=create',
				updateAction : 'DateController?action=update',
				deleteAction : 'DateController?action=delete'
			},
			fields : {
				sapCode: {
					title : 'Sap Code',
					width : '40%',
					key : true,
					list : true,
					edit : false,
					create : true
				},
				projectName : {
					title : 'Name',
					width : '50%',
                           		edit : true
				},
				country : {
					title : 'Country',
					width : '20%',
					edit : true
				}
			}
		});
             	$('#StudentTableContainer').jtable('load');    
	});
</script>
    </head>
    <body>
           <div style="width: 100%; margin-right: 0%; margin-left: 0%; text-align: center;">

		<h4>General Information</h4>
		<div id="StudentTableContainer"></div>
</div>
    </body>

0 个答案:

没有答案