图书管管理系统15周

这里写目录标题

    • 一,创建数据库
    • 二,1
    • 二,2
    • 二,3
    • 二,(3)
    • 三,向别人的数据库写入数据

在这里插入图片描述

一,创建数据库

/*
Navicat MySQL Data Transfer

Source Server : localhost18
Source Server Version : 50716
Source Host : localhost:3306
Source Database : 2020java

Target Server Type : MYSQL
Target Server Version : 50716
File Encoding : 65001

Date: 2020-12-06 23:38:49
*/

SET FOREIGN_KEY_CHECKS=0;


– Table structure for user


DROP TABLE IF EXISTS user;
CREATE TABLE user (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) DEFAULT NULL,
pwd varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;


– Records of user


INSERT INTO user VALUES (‘1’, ‘aaa’, ‘qqq’);
INSERT INTO user VALUES (‘2’, ‘bbb’, ‘bbb’);
INSERT INTO user VALUES (‘3’, ‘ccc’, ‘ccc’);
INSERT INTO user VALUES (‘4’, ‘qqq’, ‘qqq’);
INSERT INTO user VALUES (‘5’, ‘aaa’, ‘qqq’);

二,1

package framedb;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class DBDemo1 {public static void main(String[] args) throws ClassNotFoundException, SQLException{Class.forName("com.mysql.jdbc.Driver");  //数据库驱动//String url="jdbc:mysql://172.28.22.100:3306/2017jsj1?characterEncoding=utf-8";     //2017jsj1数据库名String url="jdbc:mysql://127.0.0.1:3306/2020java?     characterEncoding=utf-8";     //2020java数据库名String username="root";String password="123456";Connection conn=DriverManager.getConnection(url, username, password);Statement sta=conn.createStatement();//判定是否有此用户String sql="select * from user where name='qqq'";ResultSet rs=null;rs=sta.executeQuery(sql);int count=0;while(rs.next()){for(int i=0;i<rs.getMetaData().getColumnCount();i++){System.out.print(rs.getObject(i+1)+" ");}System.out.println();count++;}if(count==0){System.out.println("此用户不存在");}//判定用户名、密码是否一致String sql2="select * from user where name='aaa' and pwd='qqq'";int count2=0;rs=sta.executeQuery(sql2);while(rs.next()){count2++;}if(count2==0){System.out.println("用户名、密码输入错误");}System.out.println("查询结果一共有"+count2+"行记录");if(rs!=null){rs.close();}if(sta!=null){sta.close();}if(conn!=null){conn.close();}}
}

二,2

package framedb;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;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.JTextField;public class Login extends JFrame implements ActionListener{//按钮JButton btnLogin,btnRegister,btnCancel;//创建中间容器JPanel pnlSouth,pnlNorth,pnlCenter,pnlCenter1,pnlCenter2;//标签JLabel lbl1,JLabelNum,JLabelPwd;//用户名文本框JTextField tfNum;//密码文本框JPasswordField tfPwd;//创建窗口Login(String title){super(title);//northpnlNorth = new JPanel();lbl1 = new JLabel("欢迎进入图书管理系统!");pnlNorth.add(lbl1);this.add(pnlNorth,BorderLayout.NORTH);//centerpnlCenter=new JPanel();pnlCenter1=new JPanel();pnlCenter2=new JPanel();pnlCenter.setLayout(new BorderLayout());JLabelNum=new JLabel("学   号:");tfNum=new JTextField(15);pnlCenter1.add(JLabelNum);pnlCenter1.add(tfNum);pnlCenter.add(pnlCenter1,BorderLayout.NORTH);JLabelPwd=new JLabel("密    码:");tfPwd=new JPasswordField(15);pnlCenter2.add(JLabelPwd);pnlCenter2.add(tfPwd);pnlCenter.add(pnlCenter2,BorderLayout.SOUTH);this.add(pnlCenter,BorderLayout.CENTER);//southpnlSouth = new JPanel();//生成按钮btnLogin = new JButton("登录");btnLogin.addActionListener(this);btnRegister = new JButton("注册");btnRegister.addActionListener(this);btnCancel = new JButton("取消");btnCancel.addActionListener(this);//将三个按钮放在一个中间容器中pnlSouth.add(btnLogin);pnlSouth.add(btnRegister);pnlSouth.add(btnCancel);//将按钮添加到图形界面//this.add(btnLogin);//this.add(btnRegister);//this.add(btnCancel);this.add(pnlSouth,BorderLayout.SOUTH);this.setSize(400, 180);//GUIUtil.toCenter(this);//使窗口居中this.setVisible(true);//可视化this.setResizable(false);//关闭放大窗口this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置错误关闭操作}@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==btnLogin){String name=this.tfNum.getText().trim();String pwd=new String(this.tfPwd.getPassword());//String pwd2=new String(this.tfPwd.getPassword());System.out.println("输入的用户名,密码分别是:");System.out.println("name="+name);System.out.println("pwd="+pwd);//System.out.println("pwd2="+pwd2);try {Class.forName("com.mysql.jdbc.Driver");String url="jdbc:mysql://localhost:3306/2020java?characterEncoding=utf-8";String username="root";String password="123456";Connection conn=DriverManager.getConnection(url, username, password);Statement sta=conn.createStatement();//判定是否有此用户String sql="select * from user where name='"+name+"'";ResultSet rs=null;rs=sta.executeQuery(sql);int count=0;while(rs.next()){for(int i=0;i<rs.getMetaData().getColumnCount();i++){System.out.print(rs.getObject(i+1)+" ");}System.out.println();count++;}if(count==0){JOptionPane.showMessageDialog(this,name+":此用户不存在");}else{//如果用户存在,判定用户名,密码是否正确//JOptionPane.showMessageDialog(this,name+":此用户存在");String sql2="select * from user where name='"+name+"' and pwd='"+pwd+"'";rs=sta.executeQuery(sql2);int count2=0;System.out.println("---------------------");while(rs.next()){for(int i=0;i<rs.getMetaData().getColumnCount();i++){System.out.print(rs.getObject(i+1)+" ");}System.out.println();count2++;}if(count2==0){JOptionPane.showMessageDialog(this,name+":此用户密码不正确");}else{JOptionPane.showMessageDialog(this,name+":此用户登录成功");}}} catch (ClassNotFoundException ex) {Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);} catch (SQLException ex) {Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);}finally{}// JOptionPane.showMessageDialog(this,name+":此用户不存在");//System.out.println("此用户不存在");//JOptionPane.showMessageDialog(this,name+": "+pwd+":用户名、密码输入错误");}else if(e.getSource()==btnRegister){dispose();//关闭登录页面,跳到注册页面new Register("用户注册");}else {JOptionPane.showMessageDialog(this,"谢谢使用,欢迎下次再次使用本系统!");System.exit(0);}}public static void main(String[] args){Login login=new Login("图书管理系统登录界面");}
}

二,3

package framedb;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
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;public class Register extends JFrame implements ActionListener{//按钮private JButton btnRegister,btnReset,btnCancel;//标签private JLabel Num,jLabelNum,Name,jLabelName,UserName,jLabelUserName,jLabelPwd,jLabelSurePwd,jLabelSex,jLabelAge,jLabelClass;//学号,姓名,用户名,密码,确认密码,性别,年龄,所属班级//用户名文本框private JTextField jtNum,jtName,jtUserName,jtAge;//密码文本框private JPasswordField jtPwd,jtSurePwd;//性别选项private JRadioButton man,woman;//班级下拉菜单private JComboBox jtClass;//下拉框private JComboBox jcb;//创建窗口public Register(String string){this.setTitle("用户注册");//Container cp = this.getContentPane();//cp.setLayout(new BorderLayout());this.setLayout(new FlowLayout());UserName = new JLabel("      用户注册     ",JLabel.CENTER);UserName.setFont(new Font("楷体",1,36));this.add(UserName);//设置字体大小Font font = new Font("楷体",1,20);jLabelNum = new JLabel("学    号:");jLabelNum.setFont(font);jtNum = new JTextField(20);jLabelName = new JLabel("姓    名:");jLabelName.setFont(font);jtName = new JTextField(20);jLabelUserName = new JLabel("用 户 名:");jLabelUserName.setFont(font);jtUserName = new JTextField(20);jLabelPwd = new JLabel("密    码:");jLabelPwd.setFont(font);jtPwd = new JPasswordField(20);jLabelSurePwd = new JLabel("确认密码:");jLabelSurePwd.setFont(font);jtSurePwd = new JPasswordField(20);jLabelSex = new JLabel("性    别");jLabelSex.setFont(font);man = new JRadioButton("男");man.setSelected(true);//默认选男性man.setFont(font);woman = new JRadioButton("女");woman.setFont(font);ButtonGroup group = new ButtonGroup();group.add(man);group.add(woman);jLabelAge = new JLabel("年龄:");jLabelAge.setFont(font);jtAge = new JTextField(6);jLabelClass = new JLabel("所属班级:");jLabelClass.setFont(font);String[] str = {"计算机科学与技术一班","计算机科学与技术二班","物联网工程班","网络工程班"};jtClass = new JComboBox(str);jtClass.setFont(new Font("楷体",1,18));//按钮btnRegister = new JButton("注册");btnRegister.setFont(new Font("楷体",1,24));btnRegister.addActionListener(this);btnReset = new JButton("重置");btnReset.setFont(new Font("楷体",1,24));btnReset.addActionListener(this);btnCancel = new JButton("取消");btnCancel.setFont(new Font("楷体",1,24));btnCancel.addActionListener(this);JPanel pnrSouth = new JPanel();//将按钮加到一个专门放按钮的容器中pnrSouth.add(btnRegister);pnrSouth.add(btnReset);pnrSouth.add(btnCancel);this.add(jLabelNum);this.add(jtNum);this.add(jLabelName);this.add(jtName);this.add(jLabelUserName);this.add(jtUserName);this.add(jLabelPwd);this.add(jtPwd);this.add(jLabelSurePwd);this.add(jtSurePwd);this.add(jLabelSex);this.add(man);this.add(woman);this.add(jLabelAge);this.add(jtAge);this.add(jLabelClass);this.add(jtClass);this.add(pnrSouth,BorderLayout.SOUTH);//将放按钮的容器加到主容器中this.setSize(400,380);this.setVisible(true);//可视化this.setResizable(false);//关闭放大窗口this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置错误关闭操作}@Overridepublic void actionPerformed(ActionEvent e) {this.dispose();new Login("用户登录!");//return;JButton jb = (JButton) e.getSource();/*********************重置按钮***********************/if(jb == btnReset){//readInfo();jtNum.setText("");jtName.setText("");jtUserName.setText("");jtPwd.setText("");jtSurePwd.setText("");man.setSelected(true);jtAge.setText("");jtClass.setSelectedIndex(0);//选中第一个班级(计算机科学与技术一班)/********************注册按钮************************/}else if(jb == btnRegister){String num = jtNum.getText().trim();String name = jtName.getText().trim();String username = jtUserName.getText().trim();String pwd = new String(jtPwd.getPassword());String surepwd = new String(jtSurePwd.getPassword());String sex = man.isSelected()?"男":"女";String age = jtAge.getText().trim();String clas = (String)jtClass.getSelectedItem();if(num.equals("")||name.equals("")||username.equals("")||pwd.equals("")||surepwd.equals("")||age.equals("")){//判断资料是否填写完整JOptionPane.showMessageDialog(null, "请完整填写所有的信息!","提示",JOptionPane.WARNING_MESSAGE);return;}if(!pwd.equals(surepwd)){//判断两次输入密码是否相同JOptionPane.showMessageDialog(this, "两次输入的密码不同,请您重新输入!");return;}this.dispose();new Login("用户登录!");return;
//            FileOpe.getInfoByAccount(num);
//            if(User.num != null){//判断用户是否已经注册过
//                JOptionPane.showMessageDialog(this, "该用户已经注册,请您直接登录或者重新注册!");
//                this.dispose();
//                new Login("用户登录!");
//                return;
//            }
//
//            FileOpe.updateCustomer(num, name, username, pwd, sex, age, clas);
//            JOptionPane.showMessageDialog(this, "恭喜您,注册成功!");
//            this.dispose();
//            new Login("用户登录!");}else if(jb == btnCancel){this.dispose();new Login("用户登录!");}}
}

不能向数据库写入,有待完善


二,(3)

package framedb;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
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;public class Register extends JFrame implements ActionListener{//按钮private JButton btnRegister,btnReset,btnCancel;//标签private JLabel Num,jLabelNum,Name,jLabelName,UserName,jLabelUserName,jLabelPwd,jLabelSurePwd,jLabelSex,jLabelAge,jLabelClass;//学号,姓名,用户名,密码,确认密码,性别,年龄,所属班级//用户名文本框private JTextField jtNum,jtName,jtUserName,jtAge;//密码文本框private JPasswordField jtPwd,jtSurePwd;//性别选项private JRadioButton man,woman;//班级下拉菜单private JComboBox jtClass;//下拉框private JComboBox jcb;//创建窗口public Register(String string){this.setTitle("用户注册");//Container cp = this.getContentPane();//cp.setLayout(new BorderLayout());this.setLayout(new FlowLayout());UserName = new JLabel("      用户注册     ",JLabel.CENTER);UserName.setFont(new Font("楷体",1,36));this.add(UserName);//设置字体大小Font font = new Font("楷体",1,20);jLabelNum = new JLabel("学    号:");jLabelNum.setFont(font);jtNum = new JTextField(20);jLabelName = new JLabel("姓    名:");jLabelName.setFont(font);jtName = new JTextField(20);jLabelUserName = new JLabel("用 户 名:");jLabelUserName.setFont(font);jtUserName = new JTextField(20);jLabelPwd = new JLabel("密    码:");jLabelPwd.setFont(font);jtPwd = new JPasswordField(20);jLabelSurePwd = new JLabel("确认密码:");jLabelSurePwd.setFont(font);jtSurePwd = new JPasswordField(20);jLabelSex = new JLabel("性    别");jLabelSex.setFont(font);man = new JRadioButton("男");man.setSelected(true);//默认选男性man.setFont(font);woman = new JRadioButton("女");woman.setFont(font);ButtonGroup group = new ButtonGroup();group.add(man);group.add(woman);jLabelAge = new JLabel("年龄:");jLabelAge.setFont(font);jtAge = new JTextField(6);jLabelClass = new JLabel("所属班级:");jLabelClass.setFont(font);String[] str = {"计算机科学与技术一班","计算机科学与技术二班","物联网工程班","网络工程班"};jtClass = new JComboBox(str);jtClass.setFont(new Font("楷体",1,18));//按钮btnRegister = new JButton("注册");btnRegister.setFont(new Font("楷体",1,24));btnRegister.addActionListener(this);btnReset = new JButton("重置");btnReset.setFont(new Font("楷体",1,24));btnReset.addActionListener(this);btnCancel = new JButton("取消");btnCancel.setFont(new Font("楷体",1,24));btnCancel.addActionListener(this);JPanel pnrSouth = new JPanel();//将按钮加到一个专门放按钮的容器中pnrSouth.add(btnRegister);pnrSouth.add(btnReset);pnrSouth.add(btnCancel);this.add(jLabelNum);this.add(jtNum);this.add(jLabelName);this.add(jtName);this.add(jLabelUserName);this.add(jtUserName);this.add(jLabelPwd);this.add(jtPwd);this.add(jLabelSurePwd);this.add(jtSurePwd);this.add(jLabelSex);this.add(man);this.add(woman);this.add(jLabelAge);this.add(jtAge);this.add(jLabelClass);this.add(jtClass);this.add(pnrSouth,BorderLayout.SOUTH);//将放按钮的容器加到主容器中this.setSize(400,380);this.setVisible(true);//可视化this.setResizable(false);//关闭放大窗口this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置错误关闭操作}@Overridepublic void actionPerformed(ActionEvent e) {this.dispose();new Login("用户登录!");//return;JButton jb = (JButton) e.getSource();/*********************重置按钮***********************/if(jb == btnReset){//readInfo();jtNum.setText("");jtName.setText("");jtUserName.setText("");jtPwd.setText("");jtSurePwd.setText("");man.setSelected(true);jtAge.setText("");jtClass.setSelectedIndex(0);//选中第一个班级(计算机科学与技术一班)/********************注册按钮************************/}else if(jb == btnRegister){String num = jtNum.getText().trim();String name = jtName.getText().trim();String username = jtUserName.getText().trim();String pwd = new String(jtPwd.getPassword());String surepwd = new String(jtSurePwd.getPassword());String sex = man.isSelected()?"男":"女";String age = jtAge.getText().trim();String clas = (String)jtClass.getSelectedItem();if(num.equals("")||name.equals("")||username.equals("")||pwd.equals("")||surepwd.equals("")||age.equals("")){//判断资料是否填写完整JOptionPane.showMessageDialog(null, "请完整填写所有的信息!","提示",JOptionPane.WARNING_MESSAGE);return;}if(!pwd.equals(surepwd)){//判断两次输入密码是否相同JOptionPane.showMessageDialog(this, "两次输入的密码不同,请您重新输入!");return;}try {Class.forName("com.mysql.jdbc.Driver");String url="jdbc:mysql://localhost:3306/grh?characterEncoding=utf-8";String dbusername="root";String dbpassword="123456";Connection conn=DriverManager.getConnection(url, dbusername, dbpassword);//判定是否有此用户String sql="insert into user (name,pwd) values(?,?)";  //完善插入数据库方法PreparedStatement m = conn.prepareStatement(sql);m.setString(1, num);m.setString(2, pwd);m.executeUpdate();conn.close();} catch (ClassNotFoundException ex) {Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);} catch (SQLException ex) {Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);}finally{}this.dispose();new Login("用户登录!");return;
//            FileOpe.getInfoByAccount(num);
//            if(User.num != null){//判断用户是否已经注册过
//                JOptionPane.showMessageDialog(this, "该用户已经注册,请您直接登录或者重新注册!");
//                this.dispose();
//                new Login("用户登录!");
//                return;
//            }
//
//            FileOpe.updateCustomer(num, name, username, pwd, sex, age, clas);
//            JOptionPane.showMessageDialog(this, "恭喜您,注册成功!");
//            this.dispose();
//            new Login("用户登录!");}else if(jb == btnCancel){this.dispose();new Login("用户登录!");}}
}

三,向别人的数据库写入数据

package framedb;import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Read implements Runnable {DataInputStream in;public void setDataInputStream(DataInputStream in) {this.in = in;}public void run() {double result=0;try {String answer=in.readUTF();System.out.println("接收到的answer字符串为:"+answer);System.out.println("下面对程序进行处理:");int sum = 0;
//           String value = "192.168.128.33";// 注意要加\\,要不出不来answer=answer.substring(12);System.out.println("截取完学号之后为:"+answer);String[] names = answer.split("\\,");for (int i = 0; i < names.length; i++) {sum += Integer.parseInt(names[i]);System.out.println(names[i]);}System.out.println("sum=" + sum);double c = 0.0;c = (double) sum / names.length;System.out.println("c=" + c);//写入数据库(如需写入需要打开writeToDB(answer,sum,c)这一行的注释)writeToDB2(answer, sum, c);} catch (IOException ex) {Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);}}String dbusername = "root";String dbpassword = "123456";//P407参数化写数据库
public void writeToDB2(String answer,int sum, double c){Connection conn=null;Statement sta=null;try {Class.forName("com.mysql.jdbc.Driver");String url = "jdbc:mysql://172.28.22.100:3306/wts18jk2?characterEncoding=utf-8";conn = DriverManager.getConnection(url, dbusername, dbpassword);String sql="insert into user (name,pwd) values (?,?) ";PreparedStatement pst=conn.prepareStatement(sql);pst.setString(1, answer);pst.setString(2, "java我的最爱,sum="+sum+" c="+c);int count=pst.executeUpdate();System.out.println("受影响的行数为:count="+count);pst.close();} catch (ClassNotFoundException ex) {Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);} catch (SQLException ex) {Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);}finally{if(sta!=null){try {sta.close();} catch (SQLException ex) {Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);}}if(conn!=null){try {conn.close();} catch (SQLException ex) {Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);}}}
}//P405常规写数据库public void writeToDB(String answer,int sum, double c){Connection conn=null;Statement sta=null;try {Class.forName("com.mysql.jdbc.Driver");String url = "jdbc:mysql://172.28.22.100:3306/wts18jk2?characterEncoding=utf-8";conn = DriverManager.getConnection(url, dbusername, dbpassword);sta=conn.createStatement();String a = " " + sum;String b=" "+answer;
//                String sql="insert into user201705050138 (name,pwd) values (111,111ad)";String sql="insert into user (name,pwd) values('"+answer+"','"+a+"') ";int row=sta.executeUpdate(sql);System.out.println(row);} catch (ClassNotFoundException ex) {Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);} catch (SQLException ex) {Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);}finally{if(sta!=null){try {sta.close();} catch (SQLException ex) {Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);}}if(conn!=null){try {conn.close();} catch (SQLException ex) {Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);}}}}public static void main(String[] args){Read read=new Read();read.writeToDB2("201602080223guoguo", 99, 9);}
}

三,(3)1

package framedb;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
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;public class Register extends JFrame implements ActionListener{//按钮private JButton btnRegister,btnReset,btnCancel;//标签private JLabel Num,jLabelNum,Name,jLabelName,UserName,jLabelUserName,jLabelPwd,jLabelSurePwd,jLabelSex,jLabelAge,jLabelClass;//学号,姓名,用户名,密码,确认密码,性别,年龄,所属班级//用户名文本框private JTextField jtNum,jtName,jtUserName,jtAge;//密码文本框private JPasswordField jtPwd,jtSurePwd;//性别选项private JRadioButton man,woman;//班级下拉菜单private JComboBox jtClass;//下拉框private JComboBox jcb;//创建窗口public Register(String string){this.setTitle("用户注册");//Container cp = this.getContentPane();//cp.setLayout(new BorderLayout());this.setLayout(new FlowLayout());UserName = new JLabel("      用户注册     ",JLabel.CENTER);UserName.setFont(new Font("楷体",1,36));this.add(UserName);//设置字体大小Font font = new Font("楷体",1,20);jLabelNum = new JLabel("学    号:");jLabelNum.setFont(font);jtNum = new JTextField(20);jLabelName = new JLabel("姓    名:");jLabelName.setFont(font);jtName = new JTextField(20);jLabelUserName = new JLabel("用 户 名:");jLabelUserName.setFont(font);jtUserName = new JTextField(20);jLabelPwd = new JLabel("密    码:");jLabelPwd.setFont(font);jtPwd = new JPasswordField(20);jLabelSurePwd = new JLabel("确认密码:");jLabelSurePwd.setFont(font);jtSurePwd = new JPasswordField(20);jLabelSex = new JLabel("性    别");jLabelSex.setFont(font);man = new JRadioButton("男");man.setSelected(true);//默认选男性man.setFont(font);woman = new JRadioButton("女");woman.setFont(font);ButtonGroup group = new ButtonGroup();group.add(man);group.add(woman);jLabelAge = new JLabel("年龄:");jLabelAge.setFont(font);jtAge = new JTextField(6);jLabelClass = new JLabel("所属班级:");jLabelClass.setFont(font);String[] str = {"计算机科学与技术一班","计算机科学与技术二班","物联网工程班","网络工程班"};jtClass = new JComboBox(str);jtClass.setFont(new Font("楷体",1,18));//按钮btnRegister = new JButton("注册");btnRegister.setFont(new Font("楷体",1,24));btnRegister.addActionListener(this);btnReset = new JButton("重置");btnReset.setFont(new Font("楷体",1,24));btnReset.addActionListener(this);btnCancel = new JButton("取消");btnCancel.setFont(new Font("楷体",1,24));btnCancel.addActionListener(this);JPanel pnrSouth = new JPanel();//将按钮加到一个专门放按钮的容器中pnrSouth.add(btnRegister);pnrSouth.add(btnReset);pnrSouth.add(btnCancel);this.add(jLabelNum);this.add(jtNum);this.add(jLabelName);this.add(jtName);this.add(jLabelUserName);this.add(jtUserName);this.add(jLabelPwd);this.add(jtPwd);this.add(jLabelSurePwd);this.add(jtSurePwd);this.add(jLabelSex);this.add(man);this.add(woman);this.add(jLabelAge);this.add(jtAge);this.add(jLabelClass);this.add(jtClass);this.add(pnrSouth,BorderLayout.SOUTH);//将放按钮的容器加到主容器中this.setSize(400,380);this.setVisible(true);//可视化this.setResizable(false);//关闭放大窗口this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置错误关闭操作}@Overridepublic void actionPerformed(ActionEvent e) {this.dispose();new Login("用户登录!");//return;JButton jb = (JButton) e.getSource();/*********************重置按钮***********************/if(jb == btnReset){//readInfo();jtNum.setText("");jtName.setText("");jtUserName.setText("");jtPwd.setText("");jtSurePwd.setText("");man.setSelected(true);jtAge.setText("");jtClass.setSelectedIndex(0);//选中第一个班级(计算机科学与技术一班)/********************注册按钮************************/}else if(jb == btnRegister){String num = jtNum.getText().trim();String name = jtName.getText().trim();String username = jtUserName.getText().trim();String pwd = new String(jtPwd.getPassword());String surepwd = new String(jtSurePwd.getPassword());String sex = man.isSelected()?"男":"女";String age = jtAge.getText().trim();String clas = (String)jtClass.getSelectedItem();if(num.equals("")||name.equals("")||username.equals("")||pwd.equals("")||surepwd.equals("")||age.equals("")){//判断资料是否填写完整JOptionPane.showMessageDialog(null, "请完整填写所有的信息!","提示",JOptionPane.WARNING_MESSAGE);return;}if(!pwd.equals(surepwd)){//判断两次输入密码是否相同JOptionPane.showMessageDialog(this, "两次输入的密码不同,请您重新输入!");return;}try {Class.forName("com.mysql.jdbc.Driver");String url="jdbc:mysql://192.168.137.86:3306/chenke?characterEncoding=utf-8";String dbusername="root";String dbpassword="123456";Connection conn=DriverManager.getConnection(url, dbusername, dbpassword);//判定是否有此用户String sql="insert into user (id,name,username,pwd,sex,age,class) values(?,?,?,?,?,?,?)";  //完善插入数据库PreparedStatement m = conn.prepareStatement(sql);m.setString(1, num);   //m.setString(2, name);m.setString(3, username);m.setString(4, pwd);m.setString(5, sex);m.setString(6, age);m.setString(7, clas);m.executeUpdate();  //执行conn.close();} catch (ClassNotFoundException ex) {Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);} catch (SQLException ex) {Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);}finally{}this.dispose();new Login("用户登录!");return;
//            FileOpe.getInfoByAccount(num);
//            if(User.num != null){//判断用户是否已经注册过
//                JOptionPane.showMessageDialog(this, "该用户已经注册,请您直接登录或者重新注册!");
//                this.dispose();
//                new Login("用户登录!");
//                return;
//            }
//
//            FileOpe.updateCustomer(num, name, username, pwd, sex, age, clas);
//            JOptionPane.showMessageDialog(this, "恭喜您,注册成功!");
//            this.dispose();
//            new Login("用户登录!");}else if(jb == btnCancel){this.dispose();new Login("用户登录!");}}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部