📄 usermatainframe.java
字号:
1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); } } { jPanel5 = new JPanel(); jPanel2.add(jPanel5, BorderLayout.SOUTH); { jLabel1 = new JLabel(); jPanel5.add(jLabel1); jLabel1.setText("当前位置:"); } { posText = new JTextField(3); jPanel5.add(posText); } { sumLabel = new JLabel(); jPanel5.add(sumLabel); sumLabel.setText("/"); } } } { jPanel3 = new JPanel(); GridLayout jPanel3Layout = new GridLayout(2, 5); jPanel3Layout.setColumns(5); jPanel3Layout.setRows(2); jPanel1.add(jPanel3, BorderLayout.SOUTH); jPanel3.setLayout(jPanel3Layout); { firstButton = new JButton(); jPanel3.add(firstButton); firstButton.setText("第一"); firstButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { firstPressed(); //TODO add your code for firstButton.actionPerformed } }); } { preButton = new JButton(); jPanel3.add(preButton); preButton.setText("上一"); preButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { prePressed(); //TODO add your code for preButton.actionPerformed } }); } { nextButton = new JButton(); jPanel3.add(nextButton); nextButton.setText("下一"); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { nextPressed(); //TODO add your code for nextButton.actionPerformed } }); } { lastButton = new JButton(); jPanel3.add(lastButton); lastButton.setText("最后"); lastButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { lastPressed(); //TODO add your code for lastButton.actionPerformed } }); } { skimButton = new JButton(); jPanel3.add(skimButton); skimButton.setText("浏览"); skimButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { skimPressed(); //TODO add your code for skimButton.actionPerformed } }); } { addButton = new JButton(); jPanel3.add(addButton); addButton.setText("添加"); addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { addPressed(); //TODO add your code for addButton.actionPerformed } }); } { modifyButton = new JButton(); jPanel3.add(modifyButton); modifyButton.setText("修改"); modifyButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { modifyPressed(); //TODO add your code for modifyButton.actionPerformed } }); } { deleteButton = new JButton(); jPanel3.add(deleteButton); deleteButton.setText("删除"); deleteButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { delPressed(); //TODO add your code for deleteButton.actionPerformed } }); } { refreshButton = new JButton(); jPanel3.add(refreshButton); refreshButton.setText("刷新"); refreshButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { refreshPressed(); //TODO add your code for refreshButton.actionPerformed } }); } { closeButton = new JButton(); jPanel3.add(closeButton); closeButton.setText("关闭"); closeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { closePressed(); //TODO add your code for closeButton.actionPerformed } }); } } } load(); setValue(); } catch (Exception e) { e.printStackTrace(); } } void load( ){ try{ Connection conn = bridge.getConnection( ); manage.setStatement( conn ); sqlCommand ="select count(*)as'count' from buser"; System.out.println(sqlCommand); MySQL ms=new MySQL("sa","",null,sqlCommand); rs=ms.search(); //rs = manage.executeQuery( sqlCommand ); rs.next(); rowCount=rs.getInt(1); System.out.println(rowCount); MySQL ms2=new MySQL("sa","","buser",null); rs = ms2.search(); rs.next(); } catch(Exception ex) { JOptionPane.showMessageDialog(null,ex.toString(),"错误",JOptionPane.WARNING_MESSAGE); } } void setValue(){ //填充数据 try{ noText.setText( rs.getString(1) ); nameText.setText( rs.getString(2) ); sexText.setSelectedItem( rs.getString(3) ); kindText.setSelectedItem( rs.getString(4) ); deptText.setText( rs.getString(5) ); classText.setText( rs.getString(6) ); borrowedText.setText( rs.getString(7) ); inHandText.setText( rs.getString(8) ); posText.setText( String.valueOf( rs.getRow() ) ); sumLabel.setText( "/"+rowCount ); } catch(Exception ex) { JOptionPane.showMessageDialog(null,"wu xin xi le","错误",JOptionPane.WARNING_MESSAGE); } } private void firstPressed( ){ try{ rs.first(); setValue(); } catch(SQLException ex) { JOptionPane.showMessageDialog(null,ex.toString(),"错误",JOptionPane.WARNING_MESSAGE); ex.printStackTrace(); } } private void prePressed( ){ try{ rs.previous(); setValue(); } catch(SQLException ex) { JOptionPane.showMessageDialog(null,ex.toString(),"错误",JOptionPane.WARNING_MESSAGE); ex.printStackTrace(); } } private void nextPressed( ){ try{ rs.next(); setValue(); } catch(SQLException ex) { JOptionPane.showMessageDialog(null,ex.toString(),"错误",JOptionPane.WARNING_MESSAGE); ex.printStackTrace(); } } private void lastPressed( ){ try{ rs.last(); setValue(); } catch(Exception ex) { JOptionPane.showMessageDialog(null,ex.toString(),"错误",JOptionPane.ERROR_MESSAGE); ex.printStackTrace(); } } private void skimPressed( ){ try{ ResultSet r,temp; sqlCommand = "select * from user where user_id =\'"+noText.getText()+"\'"; r = manage.executeQuery( sqlCommand ); r.next(); load(); temp = rs; rs = r; setValue(); rs = temp; } catch(Exception ex) { JOptionPane.showMessageDialog(null,ex.toString(),"错误",JOptionPane.ERROR_MESSAGE); ex.printStackTrace(); } } private void addPressed( ){ try{ ResultSet r; sqlCommand = "select * from user where user_id =\'"+noText.getText()+"\'"; r = manage.executeQuery( sqlCommand ); if( !r.next() ){ sqlCommand = "Insert into user(user_id,user_name,sex,专业,班级,学制,sum,inhand)"+ "values(\'"+noText.getText()+"\',\'"+nameText.getText()+"\',\'"+(String)sexText.getSelectedItem()+ "\',\'"+deptText.getText()+"\',\'"+classText.getText()+"\',\'"+(String)kindText.getSelectedItem()+"\',\'"+borrowedText.getText()+"\',\'"+inHandText.getText()+"\')"; manage.executeInsert( sqlCommand ); JOptionPane.showMessageDialog(null,"记录添加成功!","恭喜",JOptionPane.INFORMATION_MESSAGE); } else JOptionPane.showMessageDialog(null,"记录已存在!","错误",JOptionPane.ERROR_MESSAGE); load(); setValue(); } catch(SQLException ex) { JOptionPane.showMessageDialog(null,ex.toString(),"错误",JOptionPane.ERROR_MESSAGE); ex.printStackTrace(); } } private void modifyPressed( ){ try{ ResultSet r; sqlCommand = "select * from user where user_id =\'"+noText.getText()+"\'"; r = manage.executeQuery( sqlCommand ); if( !r.next() ){ JOptionPane.showMessageDialog(null,"记录不存在!","错误",JOptionPane.ERROR_MESSAGE); } else{ sqlCommand = "update user set user_name =\'"+nameText.getText()+"\',sex =\'"+(String)sexText.getSelectedItem()+ "\',专业=\'"+deptText.getText()+"\',班级=\'"+classText.getText()+"\',学制 = \'"+(String)kindText.getSelectedItem()+ "\',sum = \'"+borrowedText.getText()+"\',inhand = \'"+inHandText.getText() +"\' where user_id=\'"+noText.getText()+"\'"; manage.executeUpdate( sqlCommand ); JOptionPane.showMessageDialog(null,"记录修改成功!","恭喜",JOptionPane.INFORMATION_MESSAGE); load(); } } catch(SQLException ex) { JOptionPane.showMessageDialog(null,ex.toString(),"错误",JOptionPane.WARNING_MESSAGE); ex.printStackTrace(); } } private void delPressed( ){ try{ Object[] options = { "确定", "放弃" }; JOptionPane pane = new JOptionPane("确定删除?", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); //JOptionPane.showOptionDialog(null, ); pane.setVisible(true); if( !pane.getValue().equals("确定")){ sqlCommand = "delete from user where user_id =\'"+noText.getText()+"\'"; manage.executeDelete( sqlCommand ); JOptionPane.showMessageDialog(null,"记录删除成功!","恭喜",JOptionPane.INFORMATION_MESSAGE); load(); setValue(); } } catch(Exception ex) { JOptionPane.showMessageDialog(null,ex.toString(),"错误",JOptionPane.WARNING_MESSAGE); ex.printStackTrace(); } } private void refreshPressed( ){ load(); setValue(); } private void closePressed( ){ this.setVisible(false); } /** * Auto-generated method for setting the popup menu for a component */ private void setComponentPopupMenu( final java.awt.Component parent, final javax.swing.JPopupMenu menu) { parent.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { if (e.isPopupTrigger()) menu.show(parent, e.getX(), e.getY()); } public void mouseReleased(java.awt.event.MouseEvent e) { if (e.isPopupTrigger()) menu.show(parent, e.getX(), e.getY()); } }); } public static void main(String []args){ //JFrame frame=new JFrame(); JInternalFrame frame1=new UserMatainFrame(); //frame.add(frame1); //frame.pack(); //frame.show(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -