WEB服务器的注册和登录类代码

数据库连接类:

View Code
 1 public class DBConnection {
 2 
 3     private static final String DRIVER = "com.mysql.jdbc.Driver";
 4     private static final String URL = "jdbc:mysql://localhost:3306/state_grid?characterEncoding=UTF-8";
 5     private static final String USERNAME = "root";
 6     private static final String PASSWORD = "";
 7     
 8     private Connection conn;
 9     public DBConnection(){
10         try {
11             Class.forName(DRIVER);
12             conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
13         } catch (ClassNotFoundException e) {
14             // TODO Auto-generated catch block
15             e.printStackTrace();
16         } catch (SQLException e) {
17             // TODO Auto-generated catch block
18             e.printStackTrace();
19         }
20         
21     }
22     
23     public Connection GetConn(){
24         return conn;
25     }
26     
27     public void Close(Statement st, ResultSet rs)
28     {
29         if(st!=null)
30             try {
31                 st.close();
32             } catch (SQLException e) {
33                 // TODO Auto-generated catch block
34                 e.printStackTrace();
35             }
36         if(rs!=null)
37             try {
38                 rs.close();
39             } catch (SQLException e) {
40                 // TODO Auto-generated catch block
41                 e.printStackTrace();
42             }
43         if(GetConn()!=null)
44             try {
45                 GetConn().close();
46             } catch (SQLException e) {
47                 // TODO Auto-generated catch block
48                 e.printStackTrace();
49             }
50     }
51     
52 }

数据库操作类:

View Code
  1 public class DBTool {
  2 
  3     /*
  4      * 注册
  5      */
  6     public boolean Register(String name, String pwd, String email, String comp) {
  7         if (name == null || pwd == null || email == null || comp == null) {
  8             System.out.println("注册的部分内容为空");
  9             return false;
 10         } else {
 11             DBConnection conn = new DBConnection();
 12             PreparedStatement st = null;
 13             ResultSet rs = null;
 14             String insertSql = null;
 15             try {
 16                 insertSql = "insert into userinfo(username, password, email, company) values(?,?,?,?)";
 17                 st = conn.GetConn().prepareStatement(insertSql);
 18                 st.setString(1, name);
 19                 st.setString(2, pwd);
 20                 st.setString(3, email);
 21                 st.setString(4, comp);
 22                 if (st.executeUpdate() <= 0) {
 23                     System.out.println("插入数据失败");
 24                     return false;
 25                 } else {
 26                     System.out.println("插入数据成功");
 27                     return true;
 28                 }
 29             } catch (SQLException e) {
 30                 // TODO Auto-generated catch block
 31                 e.printStackTrace();
 32                 return false;
 33             } finally {
 34                 conn.Close(st, rs);
 35             }
 36         }
 37     }
 38 
 39     /*
 40      * 登录
 41      */
 42     public boolean Login(String name, String pwd) {
 43         if (name == null || pwd == null) {
 44             System.out.println("登录的用户名或密码为空");
 45             return false;
 46         } else {
 47             DBConnection conn = new DBConnection();
 48             PreparedStatement st = null;
 49             ResultSet rs = null;
 50             String querySql = "select count(*) from userinfo where username=? and password=?";
 51             try {
 52                 st = conn.GetConn().prepareStatement(querySql);
 53                 st.setString(1, name);
 54                 st.setString(2, pwd);
 55                 rs = st.executeQuery();
 56                 if (rs.next()) {
 57                     int counts = rs.getInt(1);
 58                     if (counts == 0) {
 59                         System.out.println("此用户不存在,不能登录");
 60                         return false;
 61                     } else {
 62                         System.out.println("允许登录");
 63                         if (RecordLoginTime(name, conn, st))
 64                             return true;
 65                         else
 66                             return false;
 67                     }
 68                 } else {
 69                     System.out.println("rs.next==null");
 70                     return false;
 71                 }
 72             } catch (SQLException e) {
 73                 // TODO Auto-generated catch block
 74                 e.printStackTrace();
 75                 return false;
 76             } finally {
 77                 conn.Close(st, rs);
 78             }
 79         }
 80     }
 81 
 82     /*
 83      * 检查用户名是否存在 true:存在
 84      */
 85     public boolean CheckUser(String name) {
 86         if (name == null) {
 87             System.out.println("用户名不为空才能检查是否存在");
 88             return false;
 89         } else {
 90             DBConnection conn = new DBConnection();
 91             PreparedStatement st = null;
 92             ResultSet rs = null;
 93             String querySql = "select count(*) from userinfo where username=?";
 94             try {
 95                 st = conn.GetConn().prepareStatement(querySql);
 96                 st.setString(1, name);
 97                 rs = st.executeQuery();
 98                 if (rs.next()) {
 99                     int counts = rs.getInt(1);
100                     if (counts == 0) {
101                         System.out.println("此用户名没有人注册过");
102                         return false;
103                     } else {
104                         System.out.println("用户名已经存在了");
105                         return true;
106                     }
107                 } else {
108                     System.out.println("rs.next==null");
109                     return true;
110                 }
111 
112             } catch (SQLException e) {
113                 // TODO Auto-generated catch block
114                 e.printStackTrace();
115                 return true;
116             } finally {
117                 conn.Close(st, rs);
118             }
119         }
120     }
121 
122     /*
123      * 获取当前的登录时间
124      */
125     private boolean RecordLoginTime(String name, DBConnection conn,
126             PreparedStatement st) throws SQLException {
127         String updateTimeSql = "update userinfo set last_time=? where username='"
128                 + name + "'";
129         st = conn.GetConn().prepareStatement(updateTimeSql);
130         st.setString(1, GetCurTime());
131         if (st.executeUpdate() <= 0) {
132             System.out.println("更新登录时间失败");
133             return false;
134         } else {
135             System.out.println("更新登录时间完成");
136             return true;
137         }
138     }
139 
140     /*
141      * 当前时间
142      */
143     private String GetCurTime() {
144         SimpleDateFormat sDateFormat = new SimpleDateFormat(
145                 "yyyy-MM-dd   hh:mm:ss");
146         return sDateFormat.format(new java.util.Date());
147     }
148 
149 }

在此标记下,以防再写。

(应该将方法设计成static的,这里就不再改了)

posted @ 2012-12-04 18:26  Marble  阅读(377)  评论(0编辑  收藏