struts2 dao连接mysql_Struts2+DAO实现用户登陆与添加

本文详细介绍了如何使用Struts2框架进行用户管理,包括创建用户表,设计登录和添加用户页面,以及业务逻辑处理类的实现,涉及数据库连接、CRUD操作和异常处理。重点展示了UserService类中的 addUser和checkLog方法,以及UserDaoImp接口和其实现类的操作细节。
摘要由CSDN通过智能技术生成

表很简单 create table struser( name varchar(20) , password varchar(20) , age int);

登陆的Login页面:

   .....    

               .......

用户添加页面:

.......   

                        ....  

添加用户/登陆成功的显示页面:

.....          登陆成功!!!......

strutst.xml 的配置:

    success.jsp    login.jsp  

    success.jsp    adduser.jsp  

业务逻辑处理类:

public class UserService {private UserInterface userDao = new UserDaoImp();

public boolean addUser(User user) throws Exception {   // TODO Auto-generated constructor stub   boolean boo = false;   Connection connection = null;   try {    connection = DataBaseConnection.getConnection();    userDao.setConnection(connection);    userDao.addUser(user);    boo = true;   } catch (Exception e) {    e.printStackTrace();    DataBaseConnection.rollback();    throw e;   } finally {    DataBaseConnection.releaseConnection(connection);

}   return boo;}

public boolean checkLog(String name, String password) throws Exception {   User user = null;   boolean boo = false;   Connection connection = null;   try {    connection = DataBaseConnection.getConnection();    userDao.setConnection(connection);

user = userDao.getUser(name);       if (user != null) {     if (user.getPassword().equals(password)) {      boo = true;     } else {      boo = false;     }    } else {     boo = false;    }   } catch (Exception e) {    e.printStackTrace();    DataBaseConnection.rollback();    throw e;   } finally {    DataBaseConnection.releaseConnection(connection);   }   return boo;}}Action的实现

package actions;

public class AddUser extends ActionSupport {User user;boolean boo = false;

@Overridepublic String execute() throws Exception {   // TODO Auto-generated method stub   UserService us = new UserService();   boo = us.addUser(user);   System.out.println(user.getName());   if (boo == true)    return SUCCESS;   else    return ERROR;

}

public User getUser() {   return user;}

public void setUser(User user) {   this.user = user;}}

public class CheckLogin extends ActionSupport {User user;

boolean boo = false;

public User getUser() {   return user;}

public void setUser(User user) {   this.user = user;}

public String execute() throws Exception {   UserService us = new UserService();   CheckLogin cl = new CheckLogin();       boo = us.checkLog(user.getName(), user.getPassword());   if (boo == true) {    return SUCCESS;   } else {    return LOGIN;   }

}}相关接口与实现类

public interface UserInterface {       public void addUser(User user)throws Exception;       public void updateUser(User user)throws Exception;       public void delUser(String name)throws Exception;       public User getUser(String name)throws Exception;       public Connection getConnection()throws Exception;       public void setConnection(Connection connection)throws Exception;}

public class UserDaoImp implements UserInterface {private Connection connection = null;

public void addUser(User user) throws Exception {   // TODO Auto-generated method stub   PreparedStatement ps = null;   try {    ps = connection      .prepareStatement("insert into struser values(?,?,?)");    ps.setString(1, user.getName());    ps.setString(2, user.getPassword());    ps.setInt(3, user.getAge());//    System.out.println("************");    ps.executeUpdate();   } catch (SQLException sqle) {    sqle.printStackTrace();    throw sqle;   } finally {    closeStatement(ps);   }}

public void delUser(String name) throws Exception {   // TODO Auto-generated method stub   PreparedStatement ps = null;   try {    ps = connection      .prepareStatement("delete from struser where name=?)");    ps.setString(1, name);    System.out.println(name);    ps.executeUpdate();   } catch (SQLException sqle) {    sqle.printStackTrace();    throw sqle;   } finally {    closeStatement(ps);   }}

public Connection getConnection() throws Exception {   // TODO Auto-generated method stub   return connection;}

public User getUser(String name) throws Exception {   // TODO Auto-generated method stub   PreparedStatement ps = null;   ResultSet rs = null;   User user = null;      try {    ps = connection      .prepareStatement("select * from struser where name=?");    ps.setString(1, name);    rs = ps.executeQuery();    if (rs.next()) {     user = new User();     user.setName(rs.getString("name"));     user.setPassword(rs.getString("password"));     user.setAge(rs.getInt("age"));    }   } catch (SQLException sqle) {    sqle.printStackTrace();    throw sqle;   } finally {    closeResultSet(rs);    closeStatement(ps);    System.out.println("%%%"+name);   }   return user;}

public void setConnection(Connection connection) throws Exception {   // TODO Auto-generated method stub   this.connection = connection;}

public void updateUser(User user) throws Exception {   // TODO Auto-generated method stub   PreparedStatement ps = null;   try {    ps = connection      .prepareStatement("update struser set password=?,age=? where name=?)");    ps.setString(1, user.getPassword());    ps.setInt(2, user.getAge());    ps.setString(3, user.getName());    ps.executeUpdate();   } catch (SQLException sqle) {    sqle.printStackTrace();    throw sqle;   } finally {    closeStatement(ps);   }}

public UserDaoImp() {   connection = DataBaseConnection.getConnection();}

public static void closeStatement(Statement st) {   if (st != null) {    try {     st.close();     st = null;    } catch (SQLException sqle) {     sqle.printStackTrace();    }   }

}

public static void closeResultSet(ResultSet rs) {   if (rs != null) {    try {     rs.close();     rs = null;    } catch (SQLException sqle) {     sqle.printStackTrace();    }   }}}

数据库连接类

public class DataBaseConnection {

// private static String driver="com.mysql.jdbc.Driver";// private static String// url="jdbc:mysql://127.0.0.1/test?user=root&password=root&useUnicode=true&characterEncoding=gbk";// private static String user="root";// private static String pwd="root";private static String driver;private static String url;private static String user;private static String pwd;private final static String fileName = "database";private static ThreadLocal connection = new ThreadLocal();

static {   config();}

private static void config() {   // 读取配置文件   PropertyResourceBundle resourceBundle = (PropertyResourceBundle) PropertyResourceBundle     .getBundle(fileName);   // 将系统设置赋值给类变量   Enumeration enu = resourceBundle.getKeys();   while (enu.hasMoreElements()) {    String propertyName = enu.nextElement().toString();    if (propertyName.equals("database.url"))     url = resourceBundle.getString("database.url");    if (propertyName.equals("database.driver"))     driver = resourceBundle.getString("database.driver");    if (propertyName.equals("database.username"))     user = resourceBundle.getString("database.username");    if (propertyName.equals("database.password"))     pwd = resourceBundle.getString("database.password");   }}

public static Connection getConnection() {   Connection con = (Connection) connection.get();

try {    Class.forName(driver);    con = java.sql.DriverManager.getConnection(url, user, pwd);   } catch (Exception e) {    e.printStackTrace();   }   return con;}

public static void commit() {   Connection con = (Connection) connection.get();   try {    con.commit();   } catch (SQLException e) {    e.printStackTrace();   }}

public static void rollback() {   Connection con = (Connection) connection.get();   try {    con.rollback();   } catch (SQLException e) {    e.printStackTrace();   }}

public synchronized static void releaseConnection(Connection connection) {   try {    if (connection != null && !connection.isClosed())     connection.close();   } catch (SQLException e) {    e.printStackTrace();   }   connection = null;}

public static void main(String args[]) {   DataBaseConnection dbc = new DataBaseConnection();   System.out.println(driver);   System.out.println(url);   System.out.println(user);   System.out.println(pwd);   dbc.getConnection();}}数据库的连接配置文件

database.driver=com.mysql.jdbc.Driverdatabase.url=jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=utf-8

POJO类

package poo;

import java.io.Serializable;

public class User implements Serializable {public String name;public String password;public int age;

set/get method;

public User() {}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值