📄 addborrowinfo.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package libsystem;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.sql.ResultSet;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.Date;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.JTextField;/** * * @author admin */public class AddBorrowInfo extends JFrame{ private JPanel panel; private JLabel lborrowid=new JLabel("借阅号:"); private JLabel lbookid=new JLabel("书 号:"); private JLabel lreaderid=new JLabel("学 号:"); private JLabel lisout=new JLabel("借 出:"); private JLabel lborrowdata=new JLabel("借出日期:"); private JLabel lisreturn=new JLabel("归 还:"); private JLabel lreturndata=new JLabel("归还日期:"); private JTextField borrowid=new JTextField(); private JTextField bookid=new JTextField(); private JTextField readerid=new JTextField(); private JTextField borrowdate=new JTextField(); private JTextField returndata=new JTextField(); private JComboBox isreturn = new JComboBox(); private JComboBox isout=new JComboBox(); private JButton submit = new JButton("提交"); public AddBorrowInfo(){ super("添加借阅信息"); this.setBounds(320,160,400,350); panel =new JPanel(null); panel.setBackground(Color.black); add(panel); InitComponent(); super.setVisible(true); } private void InitComponent() { lborrowid.setForeground(Color.white); lborrowid.setBounds(30, 11, 51, 33); lbookid.setForeground(Color.white); lbookid.setBounds(30, 55, 51, 33); lreaderid.setForeground(Color.white); lreaderid.setBounds(30, 99, 51, 33); lisout.setForeground(Color.white); lisout.setBounds(30, 143, 51, 33); lborrowdata.setBounds(170, 143, 61, 33); lborrowdata.setForeground(Color.white); lisreturn.setForeground(Color.white); lisreturn.setBounds(30, 187, 61, 33); lreturndata.setForeground(Color.white); lreturndata.setBounds(170, 187, 61, 33); submit.setBounds(157, 251, 89, 22); submit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { //提交前校验信息 if(correctInfo()){ try { String SQL = "insert into Borrow (借阅号,书号,学号,借出,借出日期,归还,归还日期)" + " values ( '" + borrowid.getText() + "','" + bookid.getText() + "','" + readerid.getText() + "','" + isout.getSelectedItem() + "','" + borrowdate.getText() + "','" + isreturn.getSelectedItem() + "','" + returndata.getText() + "')"; ConnDataBase.getConnection().createStatement().executeUpdate(SQL); JOptionPane.showMessageDialog(null, "添加信息成功!"); dispose(); } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "添加信息失败", "警告",JOptionPane.WARNING_MESSAGE); return; } } } }); submit.setVisible(true); borrowid.setEditable(false); borrowid.setText( getCurrDate(2)+(int)(Math.random()*9999)); borrowid.setBounds(85, 11, 91, 25); readerid.setText(""); readerid.setBounds(85, 99, 71, 25); bookid.setText(""); bookid.setBounds(85, 55, 71, 25); isout.setBounds(85, 143, 50, 25); isout.addItem("否"); isout.addItem("是"); isout.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e) { if(e.getItem().equals("是"))borrowdate.setText(getCurrDate(1)); if(e.getItem().equals("否"))borrowdate.setText(""); } }); isout.setForeground(Color.black); isout.setBackground(Color.white); isreturn.setBounds(85, 187, 50, 25); isreturn.addItem("否"); isreturn.addItem("是"); isreturn.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e) { if(e.getItem().equals("是"))returndata.setText(getCurrDate(1)); if(e.getItem().equals("否"))returndata.setText(""); } }); isreturn.setForeground(Color.black); isreturn.setBackground(Color.white); borrowdate.setText(""); borrowdate.setBounds(232, 143, 100, 25); borrowdate.setEditable(false); returndata.setText(""); returndata.setBounds(232, 187, 100, 25); returndata.setEditable(false); panel.add(returndata); panel.add(borrowdate); panel.add(isreturn); panel.add(isout); panel.add(bookid); panel.add(borrowid); panel.add(readerid); panel.add(submit); panel.add(lreturndata); panel.add(lisreturn); panel.add(lborrowdata); panel.add(lisout); panel.add(lreaderid); panel.add(lbookid); panel.add(lborrowid); } private String getCurrDate(int type){ Date d=new Date(); SimpleDateFormat sdf; if(type==1) sdf=new SimpleDateFormat("yyyy-MM-dd"); else if(type==2)sdf=new SimpleDateFormat("MMdd"); else sdf=new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(d); } private boolean correctInfo(){ try { String msg = ""; String SQL = "select * from Reader where 学号='" + readerid.getText().trim()+"'"; ResultSet rs = null; rs = ConnDataBase.getConnection().createStatement().executeQuery(SQL); if (!rs.next()) { msg = msg + "学号不存在!\n"; } rs = null; SQL = "select * from Book where 书号='" + bookid.getText().trim()+"'"; rs = ConnDataBase.getConnection().createStatement().executeQuery(SQL); if(!rs.next())msg=msg+"书号不存在\n"; if(!msg.equals("")){JOptionPane.showMessageDialog(null, msg, "警告", JOptionPane.WARNING_MESSAGE); return false;} } catch (SQLException ex) { ex.printStackTrace(); return false; } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -