JSP+Servlet实现⽤户增删改查的简单步骤要求
⽤户的登录,注册,增删改查,还有详情页⾯
以及相关的表单验证
步骤:
数据库
建库
CREATE DATABASE<dbname>
建表
CREATE TABLE<table name>
添加数据
INSERT INTO<tbname>(列1,列2,列3..)VALUES('值1','值2','值3') –-字符串是要⽤单引号括起来,数字值不⽤
项⽬结构
bean(model)
根据数据库表字段名创建bean
要⽤私有的,例如:
private Integer id;
private String userName;
private String pwd;
Util(⼯具类)
1.静态代码段
private static String driver ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url ="jdbc:sqlserver://127.0.0.1:1433;databaseName=mydb";
private static String user ="sa";
private static String pwd ="1";
2.获取链接
public static Connection getConn(){
Connection conn = null;
try{
Class.forName(driver);
try{
conn = Connection(url, user, pwd);
}catch(SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
java和jsp}
}catch(ClassNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
3.资源关闭
public static void close(Connection conn, PreparedStatement ps, ResultSet rs){ try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
4.更新操作
更新操作包括增、删、改。
返回的是受影响的⾏数,所以定义⼀个int类型。
public static int update(String sql, Object[] objs){
int count =0;
Connection conn =getConn();
PreparedStatement ps = null;
try{
ps = conn.prepareStatement(sql);
for(int i =0; i < objs.length; i++){
ps.setObject(i +1, objs[i]);
}
count = ps.executeUpdate();
}catch(SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
}
dao(业务层)
1.⽤户的登录
//定义⼀个boolean类型的⽅法,⽤来判断登录是否成功
public static boolean login(String userName, String pwd){
boolean flag =false;
Connection conn = Conn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql ="select * from users where user_name= ? and pwd =?"; try{
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
rs = ps.executeQuery();
()){
flag =true;
}else{
flag =false;
}
}catch(SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
}
2.通过id获取⽤户
//定义⼀个user类型的⽅法
public static User getUserByID(Integer id){
Connection conn = Conn();
String sql ="select * from users where id = ?";
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try{
ps = conn.prepareStatement(sql);
//给 ? 赋值
ps.setInt(1, id);
rs = ps.executeQuery();
()){
//实例化⼀个user对象
user =new User();
//通过user.set把rs.get获取的内容写⼊到user对象
user.Int("id"));
user.String("user_name"));
user.String("pwd"));
user.String("sex"));
user.Int("age"));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
//关闭资源
DBUtil.close(conn, ps, rs);
}
return user;
}
3.查询全部⽤户
//定义⼀个ArrayList数组获取全部⽤户
public static ArrayList<User>getUsers(){
Connection conn = Conn();
String sql ="select * from users";
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList<User> users =new ArrayList<User>();
try{
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
()){
User user =new User();
user.Int("id"));
user.String("user_name"));
user.String("pwd"));
user.String("sex"));
user.Int("age"));
users.add(user);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DBUtil.close(conn, ps, rs);
}
return users;
}
action
登录(login)
LoginServlet
1.设置编码
request.setCharacterEncoding("utf-8");
2.获取登录页⾯的⽤户名和密码
String userName = Parameter("userName");
String pwd = Parameter("pwd");
3.调⽤dao层的login⽅法User user=UserDao.login(⽤户名,密码) boolean flag = UserDao.login(userName, pwd);
4.判断并且重定向到列表页⾯
if(flag){
response.sendRedirect("cdzList");
}
login.jsp页⾯
<form action="login"method="post">
⽤户名:
<input type="text"name="userName"/>
<br />
密码:
<input type="text"name="pwd"/>
<br />
<input type="submit"value="登录"/>
</form>
注册(reg)
RegServlet
1.设置编码
request.setCharacterEncoding("utf-8");
2.获取参数
String id = Parameter("id");
String userName = Parameter("userName");
String pwd = Parameter("pwd");
3.定义sql和数组
String sql ="insert into cdz values(?,?)";
Object[] objs ={ userName, pwd };
4.调⽤util的更新操作 int count =DbUtil.update
int count = DbUtil.update(sql, objs);
5.重定向到列表页⾯
if(count >0){
/
/重定向到列表页⾯
response.sendRedirect("cdzList");
}else{
response.sendRedirect("reg.jsp");
}
reg.jsp
<form action="reg"method="post"onsubmit="return reg()">
⽤户名:
<input type="text"name="userName"onblur="checkUserName()"/> <br />
密码:
<input type="text"name="pwd"onblur="checkPwd"/>
<br />
确认密码:
<input type="text"name="surePwd"/>
<br />
<input type="submit"value="注册"/>
</form>
判断
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论