package com.booksystem; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Random; import java.util.Vector; import static javax.swing.WindowConstants.EXIT_ON_CLOSE; public class BookSystem { static Connection conn = null; static PreparedStatement ps = null; static ResultSet rs = null; static String user = null;//用户名 static String pwd = null;//密码 static String userAcc = null;//账号 static String useremail = null;//邮箱 /** * 随机账号生成 */ public static void Account(){ Random rand = new Random(); int a=rand.nextInt(100000000); // int a=12345678; String s = ""; String acc= String.valueOf(a); try { conn=DBUtil.getConn(); String sql = "select * from user where useracc=?"; ps=conn.prepareStatement(sql); ps.setString(1,acc); rs=ps.executeQuery(); if (rs.next()) { Account(); }else{ //注册成功 JFrame jf = new JFrame(); jf.setResizable(false); jf.setLayout(null); jf.setVisible(false); jf.setSize(400,300); jf.setLocationRelativeTo(null); //账号 JLabel accLabel = new JLabel("你的账号"); accLabel.setBounds(80,40,80,30); accLabel.setFont(new Font("微软雅黑",0,14)); jf.add(accLabel); JTextField accArea= new JTextField(); accArea.setBounds(150,40,160,30); accArea.setText(acc); userAcc=acc; accArea.setEditable(false); jf.add(accArea); //密码 JLabel pwdLabel = new JLabel("密码"); pwdLabel.setBounds(110,80,80,30); pwdLabel.setFont(new Font("微软雅黑",0,14)); jf.add(pwdLabel); JTextField pwdArea= new JTextField(); pwdArea.setBounds(150,80,160,30); pwdArea.setText(pwd); pwdArea.setEditable(false); jf.add(pwdArea); //邮箱 JLabel email = new JLabel("邮箱"); email.setBounds(110,120,80,30); email.setFont(new Font("微软雅黑",0,14)); jf.add(email); JTextField emailArea= new JTextField(); emailArea.setBounds(150,120,160,30); emailArea.setText(useremail); emailArea.setEditable(false); jf.add(emailArea); //确定按钮 JButton confirm = new JButton("注册成功"); confirm.setBounds(150,200,100,30); confirm.setFont(new Font("微软雅黑",0,14)); jf.add(confirm); //确定按钮监听 confirm.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { conn=DBUtil.getConn(); String sql ="INSERT into user VALUES(null,?,?,?,?,'用户')"; ps=conn.prepareStatement(sql); ps.setString(1,user); ps.setString(2,userAcc); ps.setString(3,pwd); ps.setString(4,useremail); int r=ps.executeUpdate(); jf.setVisible(false); } catch (SQLException ex) { ex.printStackTrace(); }finally{ DBUtil.close(rs,ps,conn); } jf.setVisible(false); mainInterface(); } }); jf.setVisible(true); jf.setDefaultCloseOperation(EXIT_ON_CLOSE); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.close(rs,ps,conn); } } /** * 主界面 */ public static void mainInterface(){ JFrame jf = new JFrame("图书管理系统"); jf.setSize(800,500); jf.setLayout(null); jf.setLocationRelativeTo(null); //显示书籍 JButton listBook = new JButton("显示书籍"); listBook.setBounds(20,10,100,40); listBook.setFont(new Font("微软雅黑",0,16)); jf.add(listBook); //列名 Vector<Object> colname = new Vector<>(); colname.add("书籍名称"); colname.add("书籍数量"); //数据源 Vector<Object> rowdate = new Vector<>(); //表格模型 DefaultTableModel dt = new DefaultTableModel(rowdate,colname); //新建表格 JTable jt = new JTable(dt); //显示书籍监听 listBook.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { conn=DBUtil.getConn(); String sql="select * from book "; ps=conn.prepareStatement(sql); rs=ps.executeQuery(); while (rs.next()) { Vector<Object> row = new Vector<>(); row.add(rs.getString(2)); row.add(rs.getString(3)); rowdate.add(row); } dt.setDataVector(rowdate,colname); jt.setModel(dt); } catch (SQLException ex) { ex.printStackTrace(); }finally{ DBUtil.close(rs,ps,conn); } } }); JScrollPane jsp = new JScrollPane(jt); jsp.setBounds(30,60,740,400); jf.add(jsp); jf.setVisible(true); jf.setResizable(false); jf.setDefaultCloseOperation(EXIT_ON_CLOSE); } /** * 登录界面 */ public static void login(){ //创建窗口 JFrame jf= new JFrame(); //窗口布局 jf.setLayout(null); //窗口大小 jf.setSize(500,360); //设置窗口居中 jf.setLocationRelativeTo(null); //创建标签 JLabel accLabel = new JLabel("账号:"); //标签字体大小 accLabel.setFont(new Font("微软雅黑",0,14)); //绝对布局位置 accLabel.setBounds(110,60,80,15); //加入jf容器 jf.add(accLabel); //创建输入框 JTextField accArea = new JTextField(); //输入框位置 accArea.setBounds(160,55,200,30); //加入jf容器 jf.add(accArea); //密码标签 JLabel pwdLabel = new JLabel("密码:"); pwdLabel.setBounds(110,120,80,15); pwdLabel.setFont(new Font("微软雅黑",0,15)); jf.add(pwdLabel); JPasswordField pwdArea = new JPasswordField(); pwdArea.setBounds(160,110,200,30); jf.add(pwdArea); //登录按钮 JButton login = new JButton("登录"); login.setBounds(100,220,80,30); login.setFont(new Font("微软雅黑",0,15)); jf.add(login); //监听事件 login.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { String acc = accArea.getText(); String pwd = pwdArea.getText(); conn=DBUtil.getConn(); String sql="select * from user where useracc=? and userpwd=?"; ps=conn.prepareStatement(sql); ps.setString(1,acc); ps.setString(2,pwd); rs=ps.executeQuery(); int flag=0; if (rs.next()){ flag=1; mainInterface(); jf.setVisible(false); } if (flag==0){ JFrame prompt = new JFrame("错误!"); prompt.setSize(200,80); prompt.setLocationRelativeTo(null); JTextField jt = new JTextField(); //文本框只读 jt.setEditable(false); jt.setText("账号或密码有误,请重新输入!"); prompt.add(jt); prompt.setVisible(true); prompt.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } } catch (SQLException ex) { ex.printStackTrace(); }finally{ DBUtil.close(rs,ps,conn); } } }); //注册按钮 JButton register = new JButton("注册"); register.setBounds(300,220,80,30); register.setFont(new Font("微软雅黑",0,15)); jf.add(register); //注册按钮监听 register.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jf.setVisible(false); register(); } }); //窗口是否可见 jf.setVisible(true); //窗口不能改变大小 jf.setResizable(false); //窗口默认关闭方式 jf.setDefaultCloseOperation(EXIT_ON_CLOSE); } /** * 注册界面 */ public static void register(){ JFrame jf = new JFrame("注册"); jf.setLayout(null); jf.setSize(600,420); jf.setLocationRelativeTo(null); jf.setResizable(false); //用户文本 JLabel username= new JLabel("用户名"); username.setBounds(140,60,60,30); username.setFont(new Font("微软雅黑",0,16)); jf.add(username); //用户名输入框 JTextField userinfo = new JTextField(); userinfo.setBounds(220,60,200,30); jf.add(userinfo); //密码文本 JLabel userPwd1 = new JLabel("密码"); userPwd1.setBounds(145,120,60,30); userPwd1.setFont(new Font("微软雅黑",0,16)); jf.add(userPwd1); //密码输入框 JTextField userinfoPwd1 = new JTextField(); userinfoPwd1.setBounds(220,120,200,30); jf.add(userinfoPwd1); //密码文本 输入框2 JLabel userPwd2 = new JLabel("确认密码"); userPwd2.setBounds(135,180,80,30); userPwd2.setFont(new Font("微软雅黑",0,16)); jf.add(userPwd2); JTextField userinfoPwd2 = new JTextField(); userinfoPwd2.setBounds(220,180,200,30); jf.add(userinfoPwd2); //邮箱 JLabel email = new JLabel("邮箱"); email.setBounds(145,240,200,30); email.setFont(new Font("微软雅黑",0,16)); jf.add(email); JTextField emailinput = new JTextField(); emailinput.setBounds(220,240,200,30); jf.add(emailinput); //确认注册按钮 JButton determine = new JButton("确定注册"); determine.setBounds(120,310,110,30); determine.setFont(new Font("微软雅黑",0,16)); jf.add(determine); //确认注册监听 determine.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String pwd1 = userinfoPwd1.getText(); String pwd2 = userinfoPwd2.getText(); String email = emailinput.getText(); user=userinfo.getText(); pwd=pwd1; useremail=email; JFrame Tips = new JFrame(); Tips.setLayout(null); Tips.setSize(250,100); Tips.setLocationRelativeTo(null); JTextField jt = new JTextField(); jt.setSize(250,60); jt.setFont(new Font("微软雅黑",0,14)); Tips.add(jt); Tips.setVisible(false); Tips.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); if (pwd1.equals(pwd2)){ if (pwd1.length()>=6){ if (email.contains("@")){ //随机账号生成 Account(); jf.setVisible(false); }else { Tips.setVisible(true); jt.setText("邮箱格式不正确"); } }else { Tips.setVisible(true); jt.setText("密码必须大于六位"); } }else { Tips.setVisible(true); jt.setText("密码不一致,请重新输入"); } } }); //取消注册 JButton cancel = new JButton("取消"); cancel.setBounds(360,310,80,30); cancel.setFont(new Font("微软雅黑",0,16)); jf.add(cancel); //取消注册按钮监听 cancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jf.setVisible(false); login(); } }); jf.setVisible(true); jf.setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { login(); // mainInterface(); // register(); // Account(); } }