当前位置:文档之家› 图书管理系统java代码

图书管理系统java代码

项目下的包及类:数据库bookcollection表book(bookID设置为自增)表user表userbook登录注册页面:package bookDB;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class RLogin extends JFrame implements ActionListener{ //Container c;String sex;int type;// 创建登录组件JLabel jlabel0 = new JLabel("登录:");JLabel jLabel1 = new JLabel("学号(或职工号):");JLabel jLabel2 = new JLabel("密码:");JTextField userID1 = new JTextField(10);JTextField password1 = new JTextField(10);JButton btn_ok1 = new JButton(); // 创建登录确定按钮// 创建注册组件JLabel jlabel00 = new JLabel("注册:");JLabel jLabel3 = new JLabel("姓名:");JLabel jLabel4 = new JLabel("性别:");JLabel jLabel5 = new JLabel("密码:");JLabel jLabel6 = new JLabel("学号(或职工号):");JLabel jLabel7 = new JLabel("院系:");JLabel jLabel8 = new JLabel("专业:");JLabel jLabel9 = new JLabel("用户类别:");JTextField name = new JTextField(10);JTextField password2 = new JTextField(10);JTextField ID2 = new JTextField(10);JTextField department = new JTextField(10);JTextField major = new JTextField(10);JRadioButton rb1[]; // 单选按钮,用于设定性别JRadioButton rb2[];//用户类型选择JButton btn_ok2 = new JButton(); // 创建注册确定按钮// 构造函数public RLogin(){super("登录/注册:"); // 调用JFrame的构造函数String[] sex = { "男", "女" };String[] type = { "本科生", "研究生", "老师"};JPanel panel1 = new JPanel(new FlowLayout());// 使用JPanel类创建面板,该类是JComponent类的子类,后者又扩展Container//每个JPanel都是一个Container,因此,可以向JPanel添加包括面板在内的组件//this.setBackground(Color.YELLOW);this.add(panel1);this.add(panel1, BorderLayout.NORTH); // JFrame类的默认布局管理器为BorderLayout,此处将panel1设置在面板的北侧。

// 在panel1面板上添加标签panel1.add(jlabel0);panel1.add(jLabel1);panel1.add(userID1);panel1.add(jLabel2);panel1.add(password1);btn_ok1 = new JButton("确定");btn_ok1.addActionListener(this);panel1.add(btn_ok1);JPanel panel2 = new JPanel(new FlowLayout());this.add(panel2);this.add(panel2, BorderLayout.CENTER); // JFrame类的默认布局管理器为BorderLayout,此处将panel2设置在面板的南侧。

// 在panel2面板上添加组件panel2.add(jlabel00);panel2.add(jLabel3);panel2.add(name);panel2.add(jLabel4);//创建ButtonGroup的对象,用于维护单选按钮的逻辑关系ButtonGroup bg1 = new ButtonGroup();rb1 = new JRadioButton[sex.length];// 单选按钮for (int i = 0; i < 2; i++) {//rb1[i] = new JRadioButton(sex[i]);bg1.add(rb1[i]);// 管理单选按钮panel2.add(rb1[i]); // 将单选按钮添加到面}panel2.add(jLabel5);panel2.add(password2);panel2.add(jLabel6);panel2.add(ID2);panel2.add(jLabel7);panel2.add(department);panel2.add(jLabel8);panel2.add(major);panel2.add(jLabel9);//创建ButtonGroup的对象,用于维护单选按钮的逻辑关系ButtonGroup bg2 = new ButtonGroup();rb2 = new JRadioButton[type.length];// 单选按钮for (int j = 0; j < 3; j++) {//rb2[j] = new JRadioButton(type[j]);bg2.add(rb2[j]); // 管理单选按钮,只能选一个panel2.add(rb2[j]); // 将单选按钮添加到面}btn_ok2 = new JButton("确定");btn_ok2.addActionListener(this);panel2.add(btn_ok2);}public static void main(String[] args) {RLogin app = new RLogin();app.setSize(600, 200); // 设置窗口大小app.setVisible(true); // 设置窗口可见app.setDefaultCloseOperation(EXIT_ON_CLOSE);} // end main method@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stub//String sex, type;// 用户点击登录确定按钮if (e.getSource() == btn_ok1) {userbean rs = new userbean();//System.out.println(userID1.getText() + password1.getText());rs.login(userID1.getText(), password1.getText());this.dispose();}// 用户点击注册确定按钮if (e.getSource() == btn_ok2) {// 获取用户选择的性别并存入sex中for (int m = 0; m < rb1.length; m++){if (rb1[m].isSelected()){sex = rb1[m].getText();}}// 获取用户选择的类别并存入type中for (int n = 0; n < rb2.length; n++){if (rb2[n].isSelected()){type = n+1;}}userbean rs = new userbean();rs.register (name.getText(), sex, password2.getText(), ID2.getText(), department.getText(),major.getText(), type);this.dispose();}} // end actionPrform method} // end systemLogin method登录注册方法:package bookDB;import java.sql.ResultSet;import java.sql.SQLException;import javax.swing.JOptionPane;public class userbean {boolean boolLogin=false;//用户登录之后为true,没登录状态下为falseString sql;String userID;static String loginID;// 定义登录后的IDstatic int usertype;String username;String password;String sex;String department;String major;int userType;int canBorrow;ResultSet rs = null;//******************注册**********************public void register(String username,String sex,String password,String userID,String department,String major,int type){DatabaseConn2 DB = new DatabaseConn2();ername = username;erID = userID;this.sex = sex;this.department = department;this.major = major;//添加图书时,剩余馆藏量=馆藏量if(username.equals("")||userID.equals("")||sex.equals("")||department.equals("")||major. equals("")){ //提示完善图书信息JOptionPane.showMessageDialog(null,"请完善信息","错误",JOptionPane.ERROR_MESSAGE);return;}else{//int type = Integer.parseInt(userType);//转换用户类型为int型sql = "insert into user(userName,sex,password,userID,department,major,userType)"+ " values('"+username+"','"+sex+"','"+password+"','"+userID+"','"+department+"','"+major+"',"+type+")";try{DB.executeUpdate(sql); //更新语句JOptionPane.showMessageDialog(null,"注册成功您可返回登录"); //提示添加成功// bookMain main =new bookMain();RLogin login = new RLogin();login.pack();login.setVisible(true);}catch (Exception e){System.out.println(e);JOptionPane.showMessageDialog(null,"保存失败!","错误",JOptionPane.ERROR_MESSAGE);}finally {DB.close();}}}//*****************登录*****************public void login(String userId, String password1){DatabaseConn2 DB = new DatabaseConn2();erID = userId;this.password = password1;if(userID.equals("") || password.equals("")){JOptionPane.showMessageDialog(null,"请填写完整登录信息!","错误",JOptionPane.ERROR_MESSAGE);return;}else{sql = "select * from user where userID = '"+ userID +"'";rs = DB.executeQuery(sql);try {if(rs.next()){System.out.println(rs.getString("password"));if(rs.getString("password").equals(password) ){JOptionPane.showMessageDialog(null,"登录成功!");bookMain main = new bookMain();//登录成功后显示登录页面main.pack();main.setVisible(true);// HttpSession session = request.getSession(true);// session.removeAttribute(logid);// session.setAttribute(logid,userID);loginID = userID;//获取登录后的用户ID//获取用户类型usertype =Integer.parseInt(rs.getString("userType"));System.out.println(usertype+"\t"+rs.getString("userType"));}else{System.out.println("你好");}}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}主页面:package bookDB;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class RLogin extends JFrame implements ActionListener{//Container c;String sex;int type;// 创建登录组件JLabel jlabel0 = new JLabel("登录:");JLabel jLabel1 = new JLabel("学号(或职工号):");JLabel jLabel2 = new JLabel("密码:");JTextField userID1 = new JTextField(10);JTextField password1 = new JTextField(10);JButton btn_ok1 = new JButton(); // 创建登录确定按钮// 创建注册组件JLabel jlabel00 = new JLabel("注册:");JLabel jLabel3 = new JLabel("姓名:");JLabel jLabel4 = new JLabel("性别:");JLabel jLabel5 = new JLabel("密码:");JLabel jLabel6 = new JLabel("学号(或职工号):");JLabel jLabel7 = new JLabel("院系:");JLabel jLabel8 = new JLabel("专业:");JLabel jLabel9 = new JLabel("用户类别:");JTextField name = new JTextField(10);JTextField password2 = new JTextField(10);JTextField ID2 = new JTextField(10);JTextField department = new JTextField(10);JTextField major = new JTextField(10);JRadioButton rb1[]; // 单选按钮,用于设定性别JRadioButton rb2[];//用户类型选择JButton btn_ok2 = new JButton(); // 创建注册确定按钮// 构造函数public RLogin(){super("登录/注册:"); // 调用JFrame的构造函数String[] sex = { "男", "女" };String[] type = { "本科生", "研究生", "老师"};JPanel panel1 = new JPanel(new FlowLayout());// 使用JPanel类创建面板,该类是JComponent类的子类,后者又扩展Container//每个JPanel都是一个Container,因此,可以向JPanel添加包括面板在内的组件//this.setBackground(Color.YELLOW);this.add(panel1);this.add(panel1, BorderLayout.NORTH); // JFrame类的默认布局管理器为BorderLayout,此处将panel1设置在面板的北侧。

相关主题