81,094
社区成员
发帖
与我相关
我的任务
分享
<SCRIPT type="text/javascript">
$(document).ready(function()
{
$("#gridTable").jqGrid({
url:'../src/db/ViewData'
datatype:'json',
autowidth : true,
height:'auto',
colNames:['设备ID','设备域名', '设备名称', 'IP地址', '端口号','更新时间','状态'],
colModel:[
{name:'devId',index:'devId', width:80, editable:true},
{name:'devDomainName',index:'devDomainName', width:120,editable:true},
{name:'devName',index:'devName', width:120},
{name:'ipAddress',index:'ipAddress', width:80, align:"right"},
{name:'port',index:'port', width:80,align:"right"},
{name:'updateTime',index:'updateTime', width:120,sorttype:'date', editrules:{date:true},formatter:'date', datefmt:'d/m/Y'},
{name:'state',index:'state', width:80, sortable:false}
],
jsonReader:{
root: "rows", // json中代表实际模型数据的入口
page: "page", // json中代表当前页码的数据
total: "total", // json中代表页码总数的数据
records: "records", // json中代表数据行总数的数据
repeatitems:false
},
width:2,
sortname:'devId',
sortorder:'asc',
viewrecords:true,
rowNum:10,
rowList:[10,20,30],
pager:"#pager",
cellEdit:false,
caption: "设备管理",
multiselect: true ,
sunGrid:true
}).navGrid('#pager',{edit:true,add:true,del:true,search:true,refresh:true,view:true});
var mydata = [
{devId:'ID0000801940411152210651',devDomainName:'weilan',devName:'dg',ipAddress:'119.39.97.85',port:'3000',updateTime:'2011-02-27 07:47:52.0',state:'1'},
{devId:'ID0000801940411152210651',devDomainName:'weilan',devName:'dg',ipAddress:'119.39.97.85',port:'3000',updateTime:'2011-02-27 07:47:52.0',state:'1'}
];
for(var i=0;i<=mydata.length;i++)
$("#gridTable").jqGrid('addRowData',i+1,mydata[i]);
});
</script>
<table id="gridTable" ></table>
<div id="pager" class="scroll" style="text-algin:center;"></div>
package db;
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class ViewData extends HttpServlet {
/**
* @param args
*/
public void getData (HttpServletRequest request, HttpServletResponse response)throws Exception
//public void getData() //方面调试json,省去参数。
{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/pcwebdb";
String user="root";
String password="root";
String sql=null;
Connection conn=null;
Statement statement=null;
ResultSet rs=null;
try
{
conn = DriverManager.getConnection(url,user,password);
statement = conn.createStatement();
sql = "select * from tab_dev";
rs=statement.executeQuery(sql);
Class.forName(driver);
if(!conn.isClosed())
{
System.out.println("Succeeded connecting to the Database!");
}
String dev_id=null;
String dev_memo=null;
String dev_name=null;
String dev_OuterIP=null;
String dev_OuterPort=null;
String dev_Time_UTC=null;
String dev_active=null;
while(rs.next())
{
dev_id=rs.getString("dev_id");
dev_memo=rs.getString("dev_memo");
dev_name=rs.getString("dev_name");
dev_OuterIP=rs.getString("dev_OuterIP");
dev_OuterPort=rs.getString("dev_OuterPort");
dev_Time_UTC=rs.getString("dev_Time_UTC");
dev_active=rs.getString("dev_active");
//String page = request.getParameter("page");
//String rows = request.getParameter("rows");
//int totalRecord = 80;
String page="1";
String rows="2";
page=request.getParameter("page");
rows=request.getParameter("rows");
int totalRecord=1648;
int totalPage = totalRecord%Integer.parseInt(rows) == 0 ?
totalRecord/Integer.parseInt(rows) : totalRecord/Integer.parseInt(rows)+1;
try {
int index = (Integer.parseInt(page)-1)*Integer.parseInt(rows);
int pageSize = Integer.parseInt(rows);
String json = "{total: "+totalPage+", page: "+page+", records: "+totalRecord+", rows: [";
for (int i = index; i < pageSize + index && i<totalRecord; i++) {
json += "{devId:'"+dev_id+"',devDomainName:'"+dev_memo+"',devName:'"+dev_name+"',ipAddress:'"+dev_OuterIP+"',port:'"+dev_OuterPort+"',updateTime:'"+dev_Time_UTC+"',state:'"+dev_active+"'}";
if (i != pageSize + index - 1 && i != totalRecord - 1) {
json += ",";
}
}
json += "]}";
System.out.println(json);
response.getWriter().write(json);
} catch (Exception ex)
{
System.out.print("Excep in ViewData when invoke method getData"+ex.getMessage());
}
}
rs.close();
conn.close();
}
catch(ClassNotFoundException e)
{
System.out.println("Sorry,can't find the Driver");
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request,response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/pcwebdb";
String user="root";
String password="root";
String sql=null;
Connection conn=null;
Statement statement=null;
ResultSet rs=null;
try
{
conn = DriverManager.getConnection(url,user,password);
statement = conn.createStatement();
sql = "select * from tab_dev";
rs=statement.executeQuery(sql);
Class.forName(driver);
if(!conn.isClosed())
{
System.out.println("Succeeded connecting to the Database!");
}
String dev_id=null;
String dev_memo=null;
String dev_name=null;
String dev_OuterIP=null;
String dev_OuterPort=null;
String dev_Time_UTC=null;
String dev_active=null;
while(rs.next())
{
dev_id=rs.getString("dev_id");
dev_memo=rs.getString("dev_memo");
dev_name=rs.getString("dev_name");
dev_OuterIP=rs.getString("dev_OuterIP");
dev_OuterPort=rs.getString("dev_OuterPort");
dev_Time_UTC=rs.getString("dev_Time_UTC");
dev_active=rs.getString("dev_active");
String page="1";
String rows="2";
page=request.getParameter("page");// 取得当前页数,注意这是jqgrid自身的参数
rows=request.getParameter("rows");// 取得每页显示行数,,注意这是jqgrid自身的参数
int totalRecord=1648;// 总记录数(应根据数据库取得,在此只是模拟)
int totalPage = totalRecord%Integer.parseInt(rows) == 0 ?
totalRecord/Integer.parseInt(rows) : totalRecord/Integer.parseInt(rows)+1; // 计算总页数
try {
int index = (Integer.parseInt(page)-1)*Integer.parseInt(rows); //开始记录数
int pageSize = Integer.parseInt(rows);
String json = "{total: "+totalPage+", page: "+page+", records: "+totalRecord+", rows: [";
for (int i = index; i < pageSize + index && i<totalRecord; i++) {
json += "{devId:'"+dev_id+"',devDomainName:'"+dev_memo+"',devName:'"+dev_name+"',ipAddress:'"+dev_OuterIP+"',port:'"+dev_OuterPort+"',updateTime:'"+dev_Time_UTC+"',state:'"+dev_active+"'}";
if (i != pageSize + index - 1 && i != totalRecord - 1) {
json += ",";
}
} //for 循环
json += "]}";
System.out.println(json);
response.getWriter().write(json); // 将JSON数据返回页面
}//try结束
catch (Exception ex)
{
System.out.println("Exception In getWriter:"+ex.getMessage());
}
}//while循环
}//try结束
catch(Exception ex)
{
System.out.println("Exception In Do Post:"+ex.getMessage());
}
}//doPost结束
public static void main(String[] args) {
// TODO Auto-generated method stub
//ViewData test=new ViewData();
//test.getData();
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<head>
<base href="<%=basePath%>">
</head>
<style>
html, body {
margin: 0;
padding: 0;
font-size: 100%;
}
.ui-jqgrid tr.jqgrow td {
white-space: normal !important;
height:auto;
vertical-align:text-top;
padding-top:10px;
}
</style>
<SCRIPT type="text/javascript">
$(document).ready(function()
{
$("#gridTable").jqGrid({
url:'/DNSWebs/servlet/ViewDataServlet',
mtype:"POST",
datatype:'json',
autowidth : true,
height:'auto',
colNames:['设备ID','设备域名', 'IP地址', '端口号','更新时间','状态'],
colModel:[
{name:'devId',index:'devId', width:120, editable:true},
{name:'devDomainName',index:'devDomainName', width:120,editable:true},
{name:'ipAddress',index:'ipAddress', width:80, align:"right"},
{name:'port',index:'port', width:80,align:"right"},
{name:'updateTime',index:'updateTime', width:120,sorttype:'date', editrules:{date:true},formatter:'date', datefmt:'d/m/Y'},
{name:'state',index:'state', width:80, sortable:false}
],
jsonReader:{
root: "rows", // json中代表实际模型数据的入口
page: "page", // json中代表当前页码的数据
total: "total", // json中代表页码总数的数据
records: "records", // json中代表数据行总数的数据
repeatitems:false
},
width:2,
sortname:'devId',
sortorder:'asc',
viewrecords:true,
rowNum:10,
rowList:[10,20,30],
pager:"#pager",
cellEdit:false,
caption: "设备管理",
multiselect: true ,
sunGrid:true
}).navGrid('#pager',{edit:true,add:true,del:true,search:true,refresh:true,view:true});
var mydata = [
{devId:'ID0000801940411152210651',devDomainName:'test1',ipAddress:'119.39.97.85',port:'3000',updateTime:'2011-02-27 07:47:52.0',state:'1'},
{devId:'ID0000801940410882100613',devDomainName:'test2',ipAddress:'58.45.131.233',port:'3000',updateTime:'2011-02-26 04:46:24.0',state:'1'},
{devId:'ID0000801940411232310669',devDomainName:'test3',ipAddress:'123.120.26.43',port:'3000',updateTime:'2011-02-23 04:35:26.0',state:'1'},
];
for(var i=0;i<=mydata.length;i++)
$("#gridTable").jqGrid('addRowData',i+1,mydata[i]);
});
</script>
<table id="gridTable" ></table>
<div id="pager" class="scroll" style="text-algin:center;"></div>
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ViewDataServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ViewDataServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Test");
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/pcwebdb";
String user="root";
String password="root";
String sql=null;
Connection conn=null;
Statement statement=null;
ResultSet rs=null;
try
{
conn = DriverManager.getConnection(url,user,password);
statement = conn.createStatement();
sql = "select * from tab_dev";
rs=statement.executeQuery(sql);
Class.forName(driver);
if(!conn.isClosed())
{
System.out.println("Succeeded connecting to the Database!");
}
String dev_id=null;
String dev_memo=null;
String dev_OuterIP=null;
String dev_OuterPort=null;
String dev_Time_UTC=null;
String dev_active=null;
while(rs.next())
{
dev_id=rs.getString("dev_id");
dev_memo=rs.getString("dev_memo");
dev_OuterIP=rs.getString("dev_OuterIP");
dev_OuterPort=rs.getString("dev_OuterPort");
dev_Time_UTC=rs.getString("dev_Time_UTC");
dev_active=rs.getString("dev_active");
String page="1";
String rows="1";
page=request.getParameter("page");
rows=request.getParameter("rows");
int totalRecord=1648;
int totalPage = totalRecord%Integer.parseInt(rows) == 0 ?
totalRecord/Integer.parseInt(rows) : totalRecord/Integer.parseInt(rows)+1;
try {
int index = (Integer.parseInt(page)-1)*Integer.parseInt(rows);
int pageSize = Integer.parseInt(rows);
String json = "{total: "+totalPage+", page: "+page+", records: "+totalRecord+", rows: [";
for (int i = index; i < pageSize + index && i<totalRecord; i++) {
json += "{devId:'"+dev_id+"',devDomainName:'"+dev_memo+"',ipAddress:'"+dev_OuterIP+"',port:'"+dev_OuterPort+"',updateTime:'"+dev_Time_UTC+"',state:'"+dev_active+"'}";
if (i != pageSize + index - 1 && i != totalRecord - 1) {
json += ",";
}
}
json += "]}";
System.out.println(json);
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
} catch (Exception ex)
{
System.out.print("Excep in ViewData when invoke method getData"+ex.getMessage());
}
}
rs.close();
conn.close();
}
catch(ClassNotFoundException e)
{
System.out.println("Sorry,can't find the Driver");
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
System.out.print("ViewDataServlet");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
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_2_5.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ViewDataServlet</servlet-name>
<servlet-class>ViewDataServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ViewDataServlet</servlet-name>
<url-pattern>/servlet/ViewDataServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>