分享

Struts2+DAO实现用户登陆与添加

 玄范 2011-09-06
Struts2+DAO实现用户登陆与添加
2011年07月05日 星期二 上午 03:10

Struts2+DAO实现用户登陆与添加

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

登陆的Login页面:

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
   .....
     <s:form action="CheckLogin" method="post">
    <s:textfield name="user.name" label="User name" />
    <s:password name="user.password" label="Password" />
    <s:submit value="Submit" />
   </s:form>
.......</html>

用户添加页面:

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
.......
    <s:form method="post" action="AddUser">
     <s:textfield name="user.name" label="Name" />
     <s:password name="user.password" label="Password" />
     <s:textfield name="user.age" label="Age" />
     <s:submit />
    </s:form>....
   </html>

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

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
.....
    <s:property value="user.name"/>
      登陆成功!!!
......
</html>

strutst.xml 的配置:
<struts>
<include file="struts-default.xml" /><!-- 使用缺省的struts的配置文件 -->
<package name="poo" extends="struts-default">

   <action name="CheckLogin" class="actions.CheckLogin">
    <result>success.jsp</result>
    <result name="login">login.jsp</result>
   </action>


   <action name="AddUser" class="actions.AddUser">
    <result name="success">success.jsp</result>
    <result name="error">adduser.jsp</result>
   </action>
</package>
</struts>

业务逻辑处理类:

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;

@Override
public 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.Driver
database.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() {}

}


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多