这里写目录标题

  • 一,创建数据库
  • 二,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("用户登录!");}}
}

图书管管理系统15周相关推荐

  1. (附源码)php+mysql+基于django的图书商城管理系统 毕业设计110938

    目 录 摘要 1 1 绪论 1 1.1 研究背景 1 1.2国内外研究现状 1 1.3论文结构与章节安排 1 2 图书销售管理系统系统分析 3 2.1 可行性分析 3 2.1.1 技术可行性分析 3 ...

  2. java图书馆库存管理系统_书店图书库存管理系统.doc

    毕 业 设 计 报 告课题: 书店图书库存管理系统系部: 软件工程系班级: 软件 092 班学号: 2009005338学生:指导教师:装订交卷日期:2012 年 4 月 10 日 毕业设计任务书一. ...

  3. 大一c语言图书管理系统查询,大一C语言课程设计图书信息管理系统.doc

    高级语言程序设计 课程设计 评语: 学 院 班 级 姓 名 学 号 成 绩 指导老师 年 月 日 一.目的 1. 进一步掌握和利用C语言进行程设计的能力: 2.? 进一步理解和运用结构化程设计的思想和 ...

  4. 数据库课程设计:图书信息管理系统(Java+MySQL)(附程序)

    期末数据库课程设计做了个图书信息管理系统,由于老师给的选题给得早,所以我在开学后的几周就开学搞了,删删改改整了好多,在此整理分享一下: 项目简介: 随着社会的发展,人们对知识的需求也在不断增长.书籍作 ...

  5. MySQL_简易图书数据库管理系统_峰峰博客_峰峰吃芒果

    简易图书数据库管理系统 简易图书管理数据库(假定数据库名为:BooksDB)包含4个关系(即:数据表).关系名.属性及说明分别如下述各表所示. 读者类别表:ReaderType 序号 字段名 数据类型 ...

  6. java毕业设计图书借阅管理系统mybatis+源码+调试部署+系统+数据库+lw

    java毕业设计图书借阅管理系统mybatis+源码+调试部署+系统+数据库+lw java毕业设计图书借阅管理系统mybatis+源码+调试部署+系统+数据库+lw 本源码技术栈: 项目架构:B/S ...

  7. 2021-07-08图书借阅管理系统

    图书借阅管理系统 背景资料: 1)每种图书都有书名.书号(ISBN).作者(译者).出版社.定价和数量等: 2)借书证记录有借阅者的基本信息,如姓名.所在单位.身份(教师和学生)等: 3)凭借书证借书 ...

  8. 基于云服务器 B/S模式 JavaWeb RFID 图书借阅管理系统

    RFID图书借阅管理系统:点击此处访问系统 前排提示: 源码.文档(开题/结题报告/演示视频)包含于上述链接. 本文是一篇初学者写的 "软件文档". 全部仔细阅读完毕需要10min ...

  9. Java课程设计-图书借阅管理系统

    摘要 图书管理工作是每个学校必须面对的工作,如何利用较为先进的技术开发高效.安全.各平台间相互数据共享的信息化平台,也就是设计出稳定.全面.有效的图书借阅管理系统,这就显得尤为重要.文中介绍了图书借阅 ...

最新文章

  1. C语言//注释使下一行代码失效
  2. Jquery的Split二次分割
  3. python的序列化和反序列化
  4. C# ASP 面试题 2017
  5. img内联块元素的操作
  6. java前端开发_Java前端开发学习什么内容
  7. nacos替代config-server和Euraka
  8. R语言之 as.formula()
  9. BPM管理系统解决方案
  10. Jshop小程序商城
  11. OSPF报文与LSA
  12. Galaxian 小蜜蜂
  13. LevOJ P1685飞跃悬崖(着色问题)
  14. NYOJ 562 盒子游戏
  15. 数学分析笔记10:函数项级数
  16. 解决IE禁止第三方Cookie
  17. iOS 相册,图片裁剪工具(附demo)
  18. 《给中国学生的第三封信——成功、自信、快乐》
  19. PC端微信.dat文件解码
  20. 【机器学习】模型评估与选择(实战)

热门文章

  1. 【问题描述】在温度刻画的不同体系中,摄氏度以1标准大气压下水的结冰点为0度,沸点为100度。华氏度以1标准大气压下水的结冰点为32度,沸点为212度。
  2. 戴尔服务器远程管理卡端口修改,dell服务器远程管理卡的配置和应用(10页)-原创力文档...
  3. 哪些细节可以助力卖家提升销量?
  4. Linux下像vhd一样的东西,如何在Linux中附加VHDx或VHD文件?
  5. xgboost处理二分类问题原理
  6. IDEA快捷键-重构
  7. 信息管理导论 | 信息资源系统管理
  8. 国产性能最稳定的NFC读卡器芯片FSV9522完美替代NXP MFRC522 性能稳定 性价比高 不缺货 KK级出货 可提供软硬件DEMO 助力企业快速开发产品
  9. 方法的调用(java)
  10. FPGA入门(三)扇入扇出,逻辑延迟和线延迟,设计主频