dwr动态添加表格小应用


//实体类

package com.xh.entity;

public class User {
private Integer id;
private String name;
private String password;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}


}




//数据访问类


package com.xh.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.xh.entity.User;

public class UserService {
// 创建一个静态连接
public static Connection conn;
static {
resetConn();
}

public static Connection getConn() {
return conn;
}

// 设置连接
public static void resetConn() {
String username = "root";
String password = "root";
String driverClassName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
try {
Class.forName(driverClassName);
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

// 验证用户登陆
public String Login(String name, String password) throws SQLException {
String sql = "select * from users where name=? and password=?";
// 获取驱动
Connection conn = UserService.getConn();
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setString(1, name);
pstm.setString(2, password);
ResultSet rs = pstm.executeQuery();
User u = null;
if (rs.next()) {
return "用户和密码正确";
} else {
return "您的输入有错误";
}

}

public List<User> getAllUser() {
List<User> list = new ArrayList();
;
String sql = "select * from users";
PreparedStatement pstm;
try {
pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
while (rs.next()) {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
u.setPassword(rs.getString("password"));
list.add(u);
}
} catch (SQLException e) {
e.printStackTrace();
}

return list;
}

public int delUser(Integer id) {
String sql = "delete users where id=" + id;
int result = 0;
try {
PreparedStatement pstm = conn.prepareStatement(sql);
result = pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}

public int updateUser(User user) {
String sql = "update users set name=? ,password=? where id=?";
int result = 0;
try {
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setString(1, user.getName());
pstm.setString(2, user.getPassword());
pstm.setInt(3, user.getId());
result = pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}

public int addUser(User user) {
   
String sql = "insert into users values(?,?)";
int result = 0;
try {
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setString(1, user.getName());
pstm.setString(2, user.getPassword());
result = pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}

}




///页面


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户列表</title>
<style type="text/css">
.button {
font-family: "tahoma", "宋体"; /*www.52css.com*/
font-size: 9pt;
color: #003399;
border: 1px #003399 solid;
color: #006699;
/*border-bottom: #93bee2 1px solid;
border-left: #93bee2 1px solid;
border-right: #93bee2 1px solid;
border-top: #93bee2 1px solid;*/
background-image: url(../images/bluebuttonbg.gif);
background-color: #e8f4ff;
cursor: hand;
font-style: normal;
width: 60px;
height: 22px;
}
</style>
<script type='text/javascript' src='/dwrlist/dwr/interface/UserJs.js'></script>
<script type='text/javascript' src='/dwrlist/dwr/engine.js'></script>
<script type='text/javascript' src='/dwrlist/dwr/util.js'></script>
<script type="text/javascript">
//封装对象
function findAllUser() {
UserJs.getAllUser( function(result) {
DWRUtil.removeAllRows("users");
dwr.util.addRows("users", result, tables, {
escapeHtml :false
});

});

}
var tables = [
function(value) {
return value.id;
},
function(value) {
return value.name;
},
function(value) {
return value.password;
},
function(value) {
return "<button id='btnedit' class='button' onclick='del("
+ value.id + ")'>删除</button>";
},
function(value) {
return "<button id='"
+ value.id
+ "' class='button' onclick='updateUser(this)'>修改</button>";
} ];
//修改用户
function updateUser(obj) {
alert(obj.id);
}
//删除用户
function del(id) {
alert(id);
}
</script>


</head>
<body>
<input type="button" value="查询所有" id="btnall" onclick="findAllUser()">
<table>
<tr>
<td>id</td>
<td>名字</td>
<td>密码</td>
<td>操作</td>
<td>修改</td>
</tr>
<tbody id="users"></tbody>
</table>

</body>
</html>

下面上源代码    和数据库脚本 
小小的例子  希望对大家学习DWR  有帮助



你可能感兴趣的:(java,sql,mysql,jdbc,DWR)