package UserBlock; import java.util.ArrayList; import java.util.Collection; /** * DAO(Data Access Oject)接口 * Created by Richard on 2017/6/16. */ public interface BaseDAO { public boolean insert(String username,String password) throws Exception; public Collection select() throws Exception; public boolean delete(String username) throws Exception; public ArrayList up_select(String username) throws Exception; public boolean update(String username,String password) throws Exception; }
package UserBlock; import javax.xml.transform.Result; import java.sql.*; import java.util.ArrayList; import java.util.Collection; /** * Created by Richard on 2017/6/16. */ public class UserDao implements BaseDAO { private String url = "jdbc:mysql://localhost:3306/struts"; private String user = "root"; private String password = "root"; @Override public boolean insert(String username,String upassword) throws Exception { Class.forName("com.mysql.jdbc.Driver"); String sql = "insert into userinfo(username,password)Values(?,?)"; Connection conn = DriverManager.getConnection(url, user, password); PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, username); pst.setString(2, upassword); int i = pst.executeUpdate(); if (i >= 1) { return true; } else { return false; } } @Override public Collection select() throws Exception { ArrayList al = new ArrayList(); Class.forName("com.mysql.jdbc.Driver"); String sql = "select * from userinfo"; Connection conn = DriverManager.getConnection(url, user, password); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); if (rs.next()) { User Get = new User(); Get.setUsername(rs.getString("username")); Get.setPassword(rs.getString("password")); al.add(Get); } return al; } @Override public boolean delete(String username) throws Exception { Class.forName("com.mysql.jdbc.Driver"); String sql = "delete from userinfo where username=?"; Connection conn = DriverManager.getConnection(url, user, password); PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, username); int i = pst.executeUpdate(); if (i >= 1) { return true; } else { return false; } } @Override public ArrayList up_select(String username) throws Exception { ArrayList al = new ArrayList(); Class.forName("com.mysql.jdbc.Driver"); String sql = "select * from userinfo where username=?"; Connection conn = DriverManager.getConnection(url, user, password); PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1,username); ResultSet rs = pst.executeQuery(); if (rs.next()) { User Get = new User(); Get.setUsername(rs.getString("username")); Get.setPassword(rs.getString("password")); al.add(Get); } return al; } @Override public boolean update(String username,String upassword) throws Exception { Class.forName("com.mysql.jdbc.Driver"); String sql = "update userinfo set password=? where username=?"; Connection conn = DriverManager.getConnection(url, user, password); PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1,upassword); pst.setString(2,username); int i = pst.executeUpdate(); if (i >= 1) { return true; } else { return false; } } }
package UserBlock; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import java.util.ArrayList; import java.util.Map; import java.util.logging.LogManager; /** * Created by Richard on 2017/6/16. * 繼承ActionSupport實現一個Action * 登錄界面經過loginuser.action調用login()方法來處理數據 * login()方法中經過ActionContext調用Session對象,存入輸入錯誤的數據 * 經過addActionMessage()方法向頁面輸出相關信息 * 註冊頁面經過reguser.action調用reg()方法來處理數據 */ public class UserAction extends ActionSupport { private String INDEX="index"; private String LOGIN="login"; private String REG="register"; private String username; private String password; public String getCompassword() { return compassword; } public void setCompassword(String compassword) { this.compassword = compassword; } private String compassword; private UserDao user; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String login(){ try{ user=new UserDao(); ArrayList result=user.up_select(username); if(result.size()>0){ User aim= (User) result.get(0); if(aim.getPassword().equals(password)){ /*登錄成功*/ return INDEX; }else{ ActionContext applicton=ActionContext.getContext(); Map session=applicton.getSession(); int count; if(session.get("count")==null){ count=0; }else{ count= (int) session.get("count"); } if(count>=3){ addActionMessage("錯誤次數過多"); count=0; session.put("count",count); return LOGIN; }else{ count++; addActionMessage("您輸入的用戶名或密碼錯誤"+count); session.put("count",count); return LOGIN; } } }else{ addActionMessage("該用戶不存在,已經跳轉到註冊頁面"); return REG; } }catch (Exception e){ addActionError(e.getMessage()); System.out.println(e.getMessage()); e.printStackTrace(); return LOGIN; } } public String reg(){ try{ user=new UserDao(); ArrayList result=user.up_select(username); if(result.size()>0) { addActionMessage("該用戶已經存在"); return REG; } else{ if(user.insert(username,password)){ return INDEX; }else{ addActionMessage("發生未知錯誤,請重試!"); return REG; } } }catch (Exception e){ addActionError(e.getMessage()); return REG; } } }
Git項目源代碼,持續更新java