UserDAO接口
/*
* UserDAO接口
*/
public interface UserDAO {
//插入用户
public void insert(User user);
//删除用户
public void delete(int id);
//更新用户
public void update(User user);
//查询所有用户
public List<User> getAllUsers();
//根据用户名或密码查询用户
public boolean checkUser(User user);
//根据编号查询用户
public User getUserById(int id);
}
UserDAOImpl实现类
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/*
* UserDAOImpl实现类
*/
public class UserDAOImpl implements UserDAO{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
@Override
public void insert(User user) {
// TODO Auto-generated method stub
}
@Override
public void delete(int id) {
// TODO Auto-generated method stub
}
@Override
public void update(User user) {
// TODO Auto-generated method stub
}
@Override
public List<User> getAllUsers() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean checkUser(User user) {
boolean flag=false;
String sql="select * from user where username=? and password=?";
try {
conn=DBUtil.getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
rs=pstmt.executeQuery();
if(rs.next()){
flag=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.closeAll(rs, pstmt, conn);
}
return flag;
}
@Override
public User getUserById(int id) {
// TODO Auto-generated method stub
return null;
}
}
UserService接口
/*
* UserService接口
*/
public interface UserService {
//用户注册
public void register(User user);
//用户登陆
public boolean login(User user);
}
UserServiceImpl实现类,调用DAO完成相应的功能
/*
* UserServiceImpl实现类,调用DAO完成相应的功能
*/
public class UserServiceImpl implements UserService {
//创建UserDAO实例
UserDAO ud=new UserDAOImpl();
@Override
public void register(User user) {
ud.insert(user);
}
@Override
public boolean login(User user) {
return ud.checkUser(user);
}
}
数据库工具类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
* 数据库工具类
*/
public class DBUtil {
//获取数据库连接
public static Connection getConnection(){
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭所有资源
public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
登陆窗体
/*
* 登陆窗体
*/
public class Login implements ActionListener {
//定义一个UserService实例
UserService us=new UserServiceImpl();
// 登陆窗体组件
private JFrame frmLogin = new JFrame("登陆");
private JPanel[] pnls = new JPanel[4]; // 4行面板
private JLabel lblWelcome = new JLabel("欢迎使用小型超市管理系统!");
private JLabel lblUsername = new JLabel("用户名:");
private JTextField txtUsername = new JTextField(20);
private JLabel lblPassword = new JLabel("密 码:");
private JPasswordField txtPassword = new JPasswordField(20);
private JButton btnLogin = new JButton("登陆");
private JButton btnExit = new JButton("退出");
// 初始化组件
public Login() {
// 初始化4行面板,并添加到窗体中
for (int i = 0; i < pnls.length; i++) {
pnls[i] = new JPanel();
pnls[i].setOpaque(false);// 设置面板透明
frmLogin.add(pnls[i]);// 将面板添加到组件中
}
// 初始化JLabel和文本框,并添加载面板中
lblWelcome.setFont(new Font("黑体", Font.BOLD, 20));
lblWelcome.setForeground(Color.red);
pnls[0].add(lblWelcome);
pnls[1].setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
pnls[1].add(lblUsername);
pnls[1].add(txtUsername);
pnls[2].setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
pnls[2].add(lblPassword);
pnls[2].add(txtPassword);
pnls[3].setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
pnls[3].add(btnLogin);
pnls[3].add(btnExit);
// 添加监听
btnLogin.addActionListener(this);
btnExit.addActionListener(this);
//设置窗体背景图片
ImageIcon iconBg=new ImageIcon("images/bg.jpg");
JLabel lblBg=new JLabel(iconBg);
lblBg.setSize(iconBg.getIconWidth(), iconBg.getIconHeight());
JLayeredPane layeredPane=frmLogin.getLayeredPane();
layeredPane.add(lblBg, new Integer(Integer.MIN_VALUE));
JPanel contentPane=(JPanel) frmLogin.getContentPane();
contentPane.setOpaque(false);
//初始化窗体
frmLogin.setLayout(new GridLayout(4, 1));
frmLogin.setSize(400,300);
frmLogin.setResizable(false);
frmLogin.setLocationRelativeTo(null);
frmLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmLogin.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand(); // 获取按钮上的文字
if (cmd.equals("登陆")) {
String username=txtUsername.getText();
String password=new String(txtPassword.getPassword());
User user=new User(username,password);
if(us.login(user)){
frmLogin.dispose();//隐藏登陆窗体
new JFrame().setVisible(true);
}else{
JOptionPane.showMessageDialog(frmLogin, "用户名或密码不正确!");
}
} else {
int choice=JOptionPane.showConfirmDialog(frmLogin, "确定要退出吗?", "提示",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(choice==0){ //如果选择是,则退出程序
System.exit(0);
}
}
}
public static void main(String[] args) {
new Login();
}
}
数据库设计
create table user
(
id int primary key auto_increment,
username varchar(20) not null,
password varchar(20) not null
);
insert into user values (null,‘admin‘,‘123‘);
insert into user values (null,‘tom‘,‘456‘);
时间: 2024-09-29 23:45:15