UserDAO接口
public interface UserDAO {
public void insert(User user);
public void delete(int id);
public void update(User user);
public List 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;
public class UserDAOImpl implements UserDAO{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
@Override
public void insert(User user) {
}
@Override
public void delete(int id) {
}
@Override
public void update(User user) {
}
@Override
public List getAllUsers() {
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) {
return null;
}
}
UserService接口
public interface UserService {
public void register(User user);
public boolean login(User user);
}
UserServiceImpl实现类,调用DAO完成相应的功能
public class UserServiceImpl implements UserService {
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 us=new UserServiceImpl();
private JFrame frmLogin = new JFrame("登陆");
private JPanel[] pnls = new JPanel[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() {
for (int i = 0; i < pnls.length; i++) {
pnls[i] = new JPanel();
pnls[i].setOpaque(false);
frmLogin.add(pnls[i]);
}
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');