3,model类跟数据库进行对应
65 package com.myivtec.model;
66
67 public class User {
68
69 private int id;
70 private String username;
71 private String password;
72
76 private int isAdmin = 2;
77 private String email;
78 public int getId() {
79 return id;
80 }
81 public void setId(int id) {
82 this.id = id;
83 }
84 public String getUsername() {
85 return username;
86 }
87 public void setUsername(String username) {
88 this.username = username;
89 }
90 public String getPassword() {
91 return password;
92 }
93 public void setPassword(String password) {
94 this.password = password;
95 }
96 public String getEmail() {
97 return email;
98 }
99 public void setEmail(String email) {
100 this.email = email;
101 }
102 public int getIsAdmin() {
103 return isAdmin;
104 }
105 public void setIsAdmin(int isAdmin) {
106 this.isAdmin = isAdmin;
107 }
108
109
110 }
在数据库中对应的表的结构
111 mysql> desc user
112 -> ;
113 +----------+--------------+------+-----+---------+----------------+
114 | Field | Type | Null | Key | Default | Extra |
115 +----------+--------------+------+-----+---------+----------------+
116 | id | int(11) | NO | PRI | NULL | auto_increment |
117 | username | varchar(100) | YES | | NULL | |
118 | password | varchar(100) | YES | | NULL | |
119 | isAdmin | int(11) | YES | | NULL | |
120 | email | varchar(100) | YES | | NULL | |
121 +----------+--------------+------+-----+---------+----------------+
122 5 rows in set (0.01 sec)
4,DAO层
123 package com.myivtec.dao;
124
125 import java.sql.Connection;
126 import java.sql.PreparedStatement;
127 import java.sql.ResultSet;
128
129 import com.myivtec.form.CommonForm;
130 import com.myivtec.model.User;
131 import com.myivtec.util.JdbcUtil;
132
133 public class UserDao {
134
139 public boolean isExit(String username){
140 boolean res = false;
141 Connection con = null;
142 ResultSet rs = null;
143 PreparedStatement ps = null;
144 try {
145
148 con = JdbcUtil.getConnection();
149
152 String sql = "select * from user where username = ?" ;
153 ps = con.prepareStatement(sql);
154 //这里讲参数替换条
155 ps.setString(1, username);
156 //执行查询,把结果存放在结果集对象rs中
157 rs = ps.executeQuery();
158 //结果集中有数据的话 将res设为true
159 while(rs.next()){
160 res = true;
161 }
162 } catch (Exception e) {
163 // TODO Auto-generated catch block
164 e.printStackTrace();
165 }finally{
166 //释放连接资源
167 JdbcUtil.free(rs, ps, con);
168 }
169 return res;
170 }
171
177 public User login(String username,String password){
178 User user = null;
179 Connection con = null;
180 ResultSet rs = null;
181 PreparedStatement ps = null;
182 try {
183 //得到数据库的一个连接
184 con = JdbcUtil.getConnection();
185 //要执行的SQL语句
186 String sql = "select username,password,isAdmin from user where username=? and password= ?";
187 //创建一个PrepareStatement对象用来准备要执行的SQL语句
188 ps = con.prepareStatement(sql);
189 //把参数设进去
190 //1代表第一个问号
191 //2代表第二个问号
192 ps.setString(1, username);
193 ps.setString(2, password);
194 //执行查询语句,并将结果存放在结果集rs中
195 rs = ps.executeQuery();
196 while(rs.next()){
197 user = new User();
198 //根据结果集,将结果构造一个对象
199 //根据列明得到要查询列的值
200 user.setIsAdmin(rs.getInt("isAdmin"));
201 user.setUsername(rs.getString("username"));
202 user.setPassword(rs.getString("password"));
203 }
204 } catch (Exception e) {
205 // TODO Auto-generated catch block
206 e.printStackTrace();
207 }finally{
208 //释放连接
209 JdbcUtil.free(rs, ps, con);
210 }
211 return user;
212 }
213 }
本文出自 “Kenan_ITBlog” 博客,请务必保留此出处http://soukenan.blog.51cto.com/5130995/1054362