这个网格函数..
$(document).ready(function () {
$("#gridTable").jqGrid({
url: '/Grid/CallingFunction',
datatype: 'xml',
colNames: ['Name', 'Empno', 'Age', 'Address', 'Contactno', 'Salary'],
colModel: [
{name: 'name', width: 200},
{name: 'empno', width: 100},
{name: 'age', width: 100},
{name: 'address', width: 100},
{name: 'contactno', width: 100},
{name: 'salary', width: 100}
],
rowNum: 20,
height: 400,
autoWidth: true,
rowList: [5, 10, 15],
pager: '#gridPager',
multiselect: true,
viewrecords: true,
caption: "Employee Details"
}).navGrid("#gridPager", {edit: false, add: false, del: 'deletetext;', refreshtext: 'Refresh ', searchtext: 'Find '});
});
</script>
</head>
<body>
<table id="gridTable" ></table>
<div id="gridPager"></div>
</body>
//这是连接类 公共类ConnectionClass {
private String name, empno, address, contactno;
private int age, salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmpno() {
return empno;
}
public void setEmpno(String empno) {
this.empno = empno;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getContactno() {
return contactno;
}
public void setContactno(String contactno) {
this.contactno = contactno;
}
public String Database() {
//StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/empdb", "root", "root");
Statement st = con.createStatement();
String s = "select * from employeegrid";
ResultSet rs = st.executeQuery(s);
while (rs.next()) {
sb.append(rs.getString(1));
sb.append(Integer.parseInt(rs.getString(2)));
sb.append(rs.getString(3));
sb.append(rs.getString(4));
sb.append(rs.getString(5));
sb.append(Integer.parseInt(rs.getString(6)));
}
} catch (ClassNotFoundException | SQLException | NumberFormatException e) {
System.out.println(e);
}
return sb.toString();
}
}
//这是调用funtcion
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml");
try (PrintWriter out = response.getWriter()) {
ConnectionClass connectionclass = new ConnectionClass();
String xml = connectionclass.Database();
response.getWriter().write(xml);
} catch (NumberFormatException e) {
System.out.println(e);
}
}