1.定义一个User类(属性id,username,password),一个JDBCUtile类(实现连接数据库的方法和断开连接的方法),一个DAO类(实现对数据库的增删改查方法)
时间: 2024-05-22 22:14:06 浏览: 30
好的,这是一个比较基础的Java面向对象的问题。下面是代码示例:
User类:
public class User {
private int id;
private String username;
private String password;
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
JDBCUtile类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCUtile {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeConnection(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
DAO类:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DAO {
private Connection conn;
public DAO() {
conn = JDBCUtile.getConnection();
}
// 添加用户
public void addUser(User user) {
String sql = "insert into user(id, username, password) values(?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, user.getId());
pstmt.setString(2, user.getUsername());
pstmt.setString(3, user.getPassword());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除用户
public void deleteUser(int id) {
String sql = "delete from user where id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 修改用户
public void updateUser(User user) {
String sql = "update user set username=?, password=? where id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setInt(3, user.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询所有用户
public List<User> getAllUser() {
List<User> userList = new ArrayList<User>();
String sql = "select * from user";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
int id = rs.getInt(1);
String username = rs.getString(2);
String password = rs.getString(3);
User user = new User(id, username, password);
userList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
return userList;
}
// 根据id查询用户
public User getUserById(int id) {
User user = null;
String sql = "select * from user where id=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
String username = rs.getString(2);
String password = rs.getString(3);
user = new User(id, username, password);
}
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
public void close() {
JDBCUtile.closeConnection(conn);
}
}
希望这能帮到你!
阅读全文