连接数据库,写了一个登录注册界面

//首先将连接数据库的语句抽出,作为一个工具类,使用的时候直接可以进行调用
package com.kd.clqx.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtils {
	public static String driverName="com.mysql.jdbc.Driver";
	public static String url = "jdbc:mysql://localhost:3306/clqx1?characterEncoding=utf8";
	public static String user = "root";
	public static String password = "root";
	
	static {
		try {
			Class.forName(DBUtils.driverName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	
	public static Connection getConn() {
		try {
			return DriverManager.getConnection(DBUtils.url, DBUtils.user, DBUtils.password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static void  close(Connection conn,Statement st,ResultSet resultSet) {
		if (null!=resultSet) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (null!=st) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (null!=conn) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
//定义商家和客户,我做的是一个简单的电商系统
package com.kd.clqx.dao;

import java.util.List;

import com.kd.clqx.domain.User;

public interface UserDao {
	public int add(User user);
	
	public User findByAccount(User user);
	
	public List<User> findAll();
}
package com.kd.clqx.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.kd.clqx.domain.SysUser;

public interface SysUserDao {
	public int add(SysUser sysUser);

	public SysUser findByAccount(SysUser sysUser);

	public List<SysUser> findAll();
	
	public int update(ArrayList list);
	
	public ResultSet select();
		
	
}
//创建具体类去实现商家和用户的接口
package com.kd.clqx.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.kd.clqx.dao.UserDao;
import com.kd.clqx.domain.User;
import com.kd.clqx.util.DBUtils;

public class UserDaoImpl implements UserDao{
    /*
     * 添加用户(non-Javadoc)
     * @see com.kd.clqx.dao.UserDao#add(com.kd.clqx.domain.User)
     */
	@Override
	public int add(User user) {
		Connection conn=DBUtils.getConn();
		Statement st=null;
		try {
			st=conn.createStatement();
			String u_account=user.getU_account();
			String u_password=user.getU_password();
			String u_name=user.getU_name();
			String sql="insert into user (u_account,u_password,u_name) values('"+u_account+"','"+u_password+"','"+u_name+"')";
			int row=st.executeUpdate(sql);
			return row;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBUtils.close(conn, st, null);
		}
		
		return -1;
	}
    /*
     * 通过账号查找用户(non-Javadoc)
     * @see com.kd.clqx.dao.UserDao#findByAccount(com.kd.clqx.domain.User)
     */
	@Override
	public User findByAccount(User user) {
		Connection conn=DBUtils.getConn();
		Statement st=null;
		ResultSet set=null;
		User user2=null;
		try {
			st=conn.createStatement();
			String sql="select * from user where u_account='"+user.getU_account()+"'";
			set=st.executeQuery(sql);
			if(set.next()) {
				int u_id=set.getInt("u_id");
				String u_account=set.getString("u_account");
				String u_password=set.getString("u_password");
				String u_name=set.getString("u_name");
				user2=new User(u_id, u_account, u_password, u_name);
			}
			return user2;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public List<User> findAll() {
		Connection conn=DBUtils.getConn();
		Statement st=null;
		ResultSet set=null;
		try {
			st=conn.createStatement();
			String sql="select * from user";
			set=st.executeQuery(sql);
			ArrayList<User> list=new ArrayList<>();
			while (set.next()) {
				int u_id=set.getInt("u_id");
				String u_account=set.getString("u_account");
				String u_password=set.getString("u_password");
				String u_name=set.getString("u_name");
				User user=new User(u_id, u_account, u_password, u_name);
				list.add(user);
				}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBUtils.close(conn, st, set);
		}
		return null;
	}

}
package com.kd.clqx.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;

import com.kd.clqx.dao.SysUserDao;
import com.kd.clqx.domain.SysUser;
import com.kd.clqx.util.DBUtils;
import com.kd.clqx.view.LoginView;

public class SysUserDaoImpl implements SysUserDao {

	@Override
	public int add(SysUser sysUser) {
		Connection conn = DBUtils.getConn();
		Statement st = null;
		try {
			st = conn.createStatement();
			String s_account = sysUser.getS_account();
			String s_password = sysUser.getS_password();
			String s_name = sysUser.getS_name();
			String sql = "insert into sysuser(s_account,s_password,s_name) values ('" + s_account + "','" + s_password
					+ "','" + s_name + "')";
			int row = st.executeUpdate(sql);
			return row;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(conn, st, null);
		}
		return -1;
	}

	@Override
	public SysUser findByAccount(SysUser sysUser) {
		Connection conn = DBUtils.getConn();
		Statement st = null;
		ResultSet set = null;
		SysUser sysUser2 = null;
		try {
			st = conn.createStatement();
			String sql = "select * from sysuser where s_account='" + sysUser.getS_account() + "'";
			set = st.executeQuery(sql);

			if (set.next()) {
				int s_id = set.getInt("s_id");
				String s_account = set.getString("s_account");
				String s_password = set.getString("s_password");
				String s_name = set.getString("s_name");
				sysUser2 = new SysUser(s_id, s_account, s_password, s_name);
			}
			return sysUser2;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(conn, st, set);
		}
		return null;
	}

	@Override
	public List<SysUser> findAll() {
		Connection conn = DBUtils.getConn();
		Statement st = null;
		ResultSet set = null;
		SysUser sysUser = new SysUser();

		try {
			st = conn.createStatement();
			String sql = "select * from sysuser";
			set = st.executeQuery(sql);
			ArrayList<SysUser> list = new ArrayList<>();
			while (set.next()) {
				int s_id = set.getInt("s_id");
				String s_account = set.getString("s_account");
				String s_password = set.getString("s_password");
				String s_name = set.getString("s_name");
				sysUser = new SysUser(s_id, s_account, s_password, s_name);
				list.add(sysUser);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(conn, st, set);
		}
		return null;
	}

	@Override
	public int update(ArrayList list) {
		Connection conn = DBUtils.getConn();
		Statement st = null;

		try {
			st = conn.createStatement();
			String name = (String) list.get(0);
			String sex = (String) list.get(1);
			int age = Integer.valueOf(list.get(2).toString());
			String national = (String) list.get(3);
			String phone = (String) list.get(4);
			String emails = (String) list.get(5);
			String address = (String) list.get(6);
			String sqll = "delete from store";
			String sql = "insert into store(name,sex,age,national,phone,emails,address,s_account) values ('" + name
					+ "' , '" + sex + "','" + age + "','" + national + "','" + phone + "','" + emails + "','" + address
					+ "','" + LoginView.account + "')";
			st.executeUpdate(sqll);
			int row = st.executeUpdate(sql);
			return row;
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return -1;
	}

	@Override
	public ResultSet select() {
		Connection conn = DBUtils.getConn();
		Statement st = null;
		ResultSet set = null;

		try {
			st = conn.createStatement();
			String sql = "select * from store where s_account='" + LoginView.account + "'";
			set = st.executeQuery(sql);

			return set;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
//开始写登录和注册界面,我使用的是window builder工具,非常方便快捷创建JFrame界面
package com.kd.clqx.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.kd.clqx.dao.SysUserDao;
import com.kd.clqx.dao.impl.SysUserDaoImpl;
import com.kd.clqx.service.SysUserService;
import com.kd.clqx.service.UserService;
import com.kd.clqx.service.impl.SysUserServiceImpl;
import com.kd.clqx.service.impl.UserServiceImpl;

public class LoginView extends JFrame {
	public static String account = null;
	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
	SysUserDao sys = new SysUserDaoImpl();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginView frame = new LoginView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginView() {
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension dimension = kit.getScreenSize();
		setIconImage(Toolkit.getDefaultToolkit().getImage("images\\777.jpg"));
		setType(Type.POPUP);
		setTitle("潮流前线");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds((int) (dimension.getWidth() - 744) / 2, (int) (dimension.getHeight() - 407) / 2, 744, 407);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("用户名:");
		lblNewLabel.setForeground(Color.BLACK);
		lblNewLabel.setFont(new Font("宋体", Font.BOLD, 14));
		lblNewLabel.setBounds(207, 179, 54, 31);
		contentPane.add(lblNewLabel);

		JLabel label = new JLabel("密码:");
		label.setFont(new Font("宋体", Font.BOLD, 14));
		label.setBounds(207, 231, 54, 31);
		contentPane.add(label);

		textField = new JTextField();
		textField.setBounds(347, 179, 217, 31);

		contentPane.add(textField);
		textField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBounds(347, 231, 217, 31);
		contentPane.add(passwordField);

		JRadioButton rdbtnNewRadioButton = new JRadioButton("商户");
		rdbtnNewRadioButton.setBounds(282, 294, 54, 23);
		rdbtnNewRadioButton.setContentAreaFilled(false);
		contentPane.add(rdbtnNewRadioButton);

		JRadioButton radioButton = new JRadioButton("用户");
		radioButton.setFont(new Font("宋体", Font.PLAIN, 13));
		radioButton.setBounds(427, 294, 64, 23);
		radioButton.setContentAreaFilled(false);
		contentPane.add(radioButton);

		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(radioButton);
		buttonGroup.add(rdbtnNewRadioButton);

		JButton btnNewButton = new JButton("登录");
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 13));
		btnNewButton.setContentAreaFilled(false);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if (radioButton.isSelected()) {
					String u_account = textField.getText();
					String u_password = passwordField.getText();

					UserService userService = new UserServiceImpl();
					boolean b = userService.login(u_account, u_password);
					if (b) {
						JOptionPane.showMessageDialog(LoginView.this, "登录成功!");
						GoodsListView goodsListView=new GoodsListView();
						goodsListView.setVisible(true);
						LoginView.this.dispose();
					} else {
						JOptionPane.showMessageDialog(LoginView.this, "登录失败!");
					}
				}
				else if (rdbtnNewRadioButton.isSelected()) {
					String s_account = textField.getText();
					String s_password = passwordField.getText();
					SysUserService service = new SysUserServiceImpl();
					boolean b = service.login(s_account, s_password);
					account = s_account;
					if (b) {
						JOptionPane.showMessageDialog(null, "登录成功!");
						SysUserView sysUserView = new SysUserView();
						sysUserView.setVisible(true);
						LoginView.this.dispose();
					} else {
						JOptionPane.showMessageDialog(null, "登录失败!");
					}
				} else {
					JOptionPane.showMessageDialog(null, "登录失败,请检查无误后登录!!");
				}
			}

		});
		btnNewButton.setBounds(243, 335, 64, 23);
		contentPane.add(btnNewButton);
		JButton button = new JButton("重置");
		button.setFont(new Font("宋体", Font.PLAIN, 13));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField.setText("");
				passwordField.setText("");
				textField.requestFocus();
			}
		});
		button.setContentAreaFilled(false);
		button.setBounds(347, 335, 64, 23);
		contentPane.add(button);

		JButton button_1 = new JButton("注册");
		button_1.setFont(new Font("宋体", Font.PLAIN, 13));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RegisterView registerView = new RegisterView();
				registerView.setVisible(true);
				setVisible(false);
			}
		});
		button_1.setBounds(453, 335, 64, 23);
		contentPane.add(button_1);
		ImageIcon icon = new ImageIcon("E:\\test\\66.png");
		JLabel label_1 = new JLabel(icon);
		this.getLayeredPane().add(label_1, new Integer(Integer.MIN_VALUE));
		label_1.setBounds(0, 0, 728, 375);
		button_1.setContentAreaFilled(false);
		contentPane.add(label_1);

	}
}
//注册界面
package com.kd.clqx.view;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.kd.clqx.service.SysUserService;
import com.kd.clqx.service.UserService;
import com.kd.clqx.service.impl.SysUserServiceImpl;
import com.kd.clqx.service.impl.UserServiceImpl;

public class RegisterView extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	/**
	 * Create the frame.
	 */
	public RegisterView() {
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension dimension = kit.getScreenSize();
		setTitle("潮流前线注册");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds((int) (dimension.getWidth() - 658) / 2, (int) (dimension.getHeight() - 450) / 2, 658, 450);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("用户名:");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 14));
		lblNewLabel.setBounds(175, 120, 68, 15);
		contentPane.add(lblNewLabel);

		JLabel label = new JLabel("密  码:");
		label.setFont(new Font("宋体", Font.PLAIN, 14));
		label.setBounds(175, 179, 68, 15);
		contentPane.add(label);

		JLabel label_1 = new JLabel("姓  名:");
		label_1.setFont(new Font("宋体", Font.PLAIN, 14));
		label_1.setBounds(175, 237, 68, 15);
		contentPane.add(label_1);

		textField = new JTextField();
		textField.setBounds(297, 113, 187, 29);
		contentPane.add(textField);
		textField.setColumns(10);

		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(297, 172, 187, 29);
		contentPane.add(textField_1);

		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(297, 230, 187, 29);
		contentPane.add(textField_2);

		JRadioButton rdbtnNewRadioButton = new JRadioButton("商户");
		rdbtnNewRadioButton.setBounds(214, 292, 54, 23);
		rdbtnNewRadioButton.setContentAreaFilled(false);
		contentPane.add(rdbtnNewRadioButton);

		JRadioButton radioButton = new JRadioButton("客户");
		radioButton.setBounds(391, 292, 54, 23);
		radioButton.setContentAreaFilled(false);
		contentPane.add(radioButton);

		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(radioButton);
		buttonGroup.add(rdbtnNewRadioButton);

		JButton btnNewButton = new JButton("确认注册");
		btnNewButton.setContentAreaFilled(false);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (radioButton.isSelected()) {
					// 用户注册
					String u_account = textField.getText();
					String u_password = textField_1.getText();
					String u_name = textField_2.getText();
					UserService userService = new UserServiceImpl();
					if (!(u_account.equals("")) && !(u_password.equals("")) && !(u_name.equals(""))) {
						boolean b = userService.register(u_account, u_password, u_name);
						if (b) {
							JOptionPane.showConfirmDialog(RegisterView.this, "注册成功!");
							RegisterView.this.dispose();
							LoginView loginView = new LoginView();
							loginView.setVisible(true);
						} else {
							JOptionPane.showConfirmDialog(RegisterView.this, "注册失败!");
						}
					} else {
						JOptionPane.showMessageDialog(null, "格式不规范,请认真填写!!");
					}

				} else if (rdbtnNewRadioButton.isSelected()) {
					// 商户注册
					String s_account = textField.getText();
					String s_password = textField_1.getText();
					String s_name = textField_2.getText();
					if (!(s_account.equals("")) && !(s_password.equals("")) && !(s_name.equals(""))) {
						SysUserService sysUserService = new SysUserServiceImpl();
						boolean b = sysUserService.register(s_account, s_password, s_name);
						if (b) {
							JOptionPane.showConfirmDialog(RegisterView.this, "注册成功!");
							RegisterView.this.dispose();
							LoginView loginView = new LoginView();
							loginView.setVisible(true);

						} else {
							JOptionPane.showConfirmDialog(RegisterView.this, "注册失败!");
						}
					} else {
						JOptionPane.showMessageDialog(null, "格式不规范,请认真填写!!");
					}

				}
				else {
					JOptionPane.showMessageDialog(null, "格式不规范,请认真填写!!");
				}
			}
		});
		btnNewButton.setBounds(175, 347, 93, 23);
		contentPane.add(btnNewButton);

		JButton button = new JButton("返回");
		button.setContentAreaFilled(false);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RegisterView.this.dispose();
				LoginView loginView = new LoginView();
				loginView.setVisible(true);
			}
		});
		button.setBounds(391, 347, 93, 23);
		contentPane.add(button);
		ImageIcon icon = new ImageIcon("images\\2.jpg");
		JLabel label_2 = new JLabel(icon);
		label_2.setFont(new Font("宋体", Font.PLAIN, 13));
		this.getLayeredPane().add(label_2, new Integer(Integer.MIN_VALUE));
		label_2.setBounds(0, 0, 642, 411);
		contentPane.add(label_2);

	}

}
//中间有很多的跳转界面,那都是为写其他界面预留的,整个系统的代码后期我会逐渐上传

猜你喜欢

转载自blog.csdn.net/Yang_xinqiao/article/details/81951813