在Dao包里面实现增删查改方法。
1、.建立SQL数据库脚本,建表tb1。:mysql
2、建立Entity包中的User.java
package Entity;
public class User {
private String userID;
private String userName;
private String password;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
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;
}
}
3、建立Dao包中的UserDAO.java
public static boolean add(User u) {
String sql = "INSERT INTO User(userID,userName) " + "VALUES(?,?)";
Object[] params = new Object[] { u.getUserID(), u.getUserName() };
DBHelper.ExecSql(sql, params);
return true;
}
public static boolean delete(User u) {
String sql = "DELETE FROM User WHERE userID=?";
Object[] params = new Object[] { u.getUserID() };
DBHelper.ExecSql(sql, params);
return true;
}
public static boolean update(User u) {
String sql = "update User set userName=? where userID=?";
Object[] params = new Object[] { u.getUserName(), u.getUserID() };
DBHelper.ExecSql(sql, params);
return true;
}
public static List<User> selAll() {
List<User> users = new LinkedList<User>();
String sql = "select * from tb1";
ResultSet rs = DBHelper.getResultSet(sql);
try {
users = tools.RestoList(rs, Class.forName("beans.tb1"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return users;
}