borrowinfoupdate.java
来自「图书馆系统」· Java 代码 · 共 388 行
JAVA
388 行
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Point;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.sql.*;
import javax.swing.JComboBox;
public class borrowInfoUpdate extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JLabel jLabel3 = null;
private JLabel jLabel4 = null;
private JTextField jTextborrowdate = null;
private JTextField jTextcomment = null;
private JButton jButtonClear = null;
private JButton jButtonOK = null;
private JButton jButtonupdate = null;
private JButton jButtonexit = null;
ResultSet rs;
private DBManager db=new DBManager();
private JComboBox jComboBoxstudentname = null;
private JComboBox jComboBoxbookname = null;
public borrowInfoUpdate() throws HeadlessException {
// TODO 自动生成构造函数存根
super();
initialize();
}
public borrowInfoUpdate(GraphicsConfiguration arg0) {
super(arg0);
// TODO 自动生成构造函数存根
initialize();
}
public borrowInfoUpdate(String arg0) throws HeadlessException {
super(arg0);
// TODO 自动生成构造函数存根
initialize();
}
public borrowInfoUpdate(String arg0, GraphicsConfiguration arg1) {
super(arg0, arg1);
// TODO 自动生成构造函数存根
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(398, 297);
this.setContentPane(getJContentPane());
this.setTitle("借阅信息修改");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent e) {
System.out.println("windowOpened()"); // TODO Auto-generated Event stub windowOpened()
String sql="select Studentname from BookBorrow",SQL="select Bookname from BookBorrow";
rs=db.getResult(sql);
boolean isexist=false;
try{
isexist=rs.first();
}
catch(SQLException ex){
}
if(!isexist){
JOptionPane.showMessageDialog(null, "没有用户,请确认", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
else{
try{
rs.beforeFirst();
//循环遍历rs中的数据,将其加到jcomboboxbookname中
while(rs.next()){
jComboBoxstudentname.addItem(rs.getString("Studentname"));
}
}
catch(SQLException ex){
}
}
rs=db.getResult(SQL);
isexist=false;
try{
isexist=rs.first();
}
catch(SQLException ex){
}
if(!isexist){
JOptionPane.showMessageDialog(null, "没有用户,请确认", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
else{
try{
rs.beforeFirst();
//循环遍历rs中的数据,将其加到jcomboboxbookname中
while(rs.next()){
jComboBoxbookname.addItem(rs.getString("Bookname"));
}
}
catch(SQLException ex){
}
}
}
});
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel4 = new JLabel();
jLabel4.setText("备注");
jLabel4.setSize(new Dimension(38, 20));
jLabel4.setLocation(new Point(35, 140));
jLabel3 = new JLabel();
jLabel3.setText("借阅日期");
jLabel3.setSize(new Dimension(72, 20));
jLabel3.setLocation(new Point(35, 110));
jLabel2 = new JLabel();
jLabel2.setText("书名");
jLabel2.setLocation(new Point(35, 80));
jLabel2.setSize(new Dimension(38, 20));
jLabel1 = new JLabel();
jLabel1.setText("借阅者姓名");
jLabel1.setLocation(new Point(35, 50));
jLabel1.setSize(new Dimension(79, 20));
jLabel = new JLabel();
jLabel.setText("输入借阅者信息或书名,将调出相关信息");
jLabel.setSize(new Dimension(279, 20));
jLabel.setLocation(new Point(23, 14));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
jContentPane.add(jLabel2, null);
jContentPane.add(jLabel3, null);
jContentPane.add(jLabel4, null);
jContentPane.add(getJTextborrowdate(), null);
jContentPane.add(getJTextcomment(), null);
jContentPane.add(getJButtonClear(), null);
jContentPane.add(getJButtonOK(), null);
jContentPane.add(getJButtonupdate(), null);
jContentPane.add(getJButtonexit(), null);
jContentPane.add(getJComboBoxstudentname(), null);
jContentPane.add(getJComboBoxbookname(), null);
}
return jContentPane;
}
/**
* This method initializes jTextborrowdate
*
* @return javax.swing.JTextField
*/
private JTextField getJTextborrowdate() {
if (jTextborrowdate == null) {
jTextborrowdate = new JTextField();
jTextborrowdate.setLocation(new Point(150, 110));
jTextborrowdate.setSize(new Dimension(121, 20));
}
return jTextborrowdate;
}
/**
* This method initializes jTextcomment
*
* @return javax.swing.JTextField
*/
private JTextField getJTextcomment() {
if (jTextcomment == null) {
jTextcomment = new JTextField();
jTextcomment.setLocation(new Point(150, 140));
jTextcomment.setSize(new Dimension(121, 20));
}
return jTextcomment;
}
/**
* This method initializes jButtonClear
*
* @return javax.swing.JButton
*/
private JButton getJButtonClear() {
if (jButtonClear == null) {
jButtonClear = new JButton();
jButtonClear.setLocation(new Point(30, 183));
jButtonClear.setText("清空");
jButtonClear.setSize(new Dimension(60, 20));
jButtonClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
jComboBoxstudentname.setSelectedItem("");
jComboBoxbookname.setSelectedItem("");
jTextborrowdate.setText("");
jTextcomment.setText("");
}
});
}
return jButtonClear;
}
/**
* This method initializes jButtonOK
*
* @return javax.swing.JButton
*/
private JButton getJButtonOK() {
if (jButtonOK == null) {
jButtonOK = new JButton();
jButtonOK.setLocation(new Point(100, 183));
jButtonOK.setText("确定");
jButtonOK.setSize(new Dimension(60, 20));
jButtonOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
if(jComboBoxstudentname.getSelectedItem().toString().trim().length()==0 && jComboBoxbookname.getSelectedItem().toString().trim().length()==0){
JOptionPane.showMessageDialog(null, "至少输入一个查询条件", "提示", JOptionPane.INFORMATION_MESSAGE);
return;//buttonexit();
}
boolean isfirst=true;
String sql="select * from BookBorrow";
if(jComboBoxstudentname.getSelectedItem().toString().trim().length()==0){}
else{
sql=sql+" where Studentname='"+jComboBoxstudentname.getSelectedItem().toString().trim()+"'";
isfirst=false;
}
if(jComboBoxbookname.getSelectedItem().toString().trim().length()==0){}
else{
if(isfirst){
sql=sql+" where Bookname='"+jComboBoxbookname.getSelectedItem().toString().trim()+"'";
}
else{
sql=sql+" and Bookname='"+jComboBoxbookname.getSelectedItem().toString().trim()+"'";
}
}
rs=db.getResult(sql);
try{
//判断返回是否为空,不为空则返回第一条记录
if(!rs.equals(null)){
rs.first();
jComboBoxstudentname.setSelectedItem(rs.getString("Studentname"));
jComboBoxbookname.setSelectedItem(rs.getString("Bookname"));
jTextborrowdate.setText(rs.getString("Borrowdate"));
jTextcomment.setText(rs.getString("Comment"));
}
else{
JOptionPane.showMessageDialog(null, "无此记录1", "错误", JOptionPane.ERROR_MESSAGE);
}
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null, "无此记录", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
}
return jButtonOK;
}
/**
* This method initializes jButtonupdate
*
* @return javax.swing.JButton
*/
private JButton getJButtonupdate() {
if (jButtonupdate == null) {
jButtonupdate = new JButton();
jButtonupdate.setLocation(new Point(170, 183));
jButtonupdate.setText("更新");
jButtonupdate.setSize(new Dimension(60, 20));
jButtonupdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
String strSQL;
strSQL="update BookBorrow set";
strSQL=strSQL+" Borrowdate='"+jTextborrowdate.getText().trim()+"',";
strSQL=strSQL+" Comment='"+jTextcomment.getText().trim()+"'";
strSQL=strSQL+" where Studentname='"+jComboBoxstudentname.getSelectedItem().toString().trim()+"'";
strSQL=strSQL+" and Bookname='"+jComboBoxbookname.getSelectedItem().toString().trim()+"'";
System.out.println(strSQL);
//数据库执行更新语句
if(db.executeSql(strSQL)){
JOptionPane.showMessageDialog(null, "更新成功", "成功", JOptionPane.INFORMATION_MESSAGE);
buttonexit();
}
else{
JOptionPane.showMessageDialog(null, "更新失败,请重新操作", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
}
return jButtonupdate;
}
/**
* This method initializes jButtonexit
*
* @return javax.swing.JButton
*/
public void buttonexit(){
this.dispose();
}
private JButton getJButtonexit() {
if (jButtonexit == null) {
jButtonexit = new JButton();
jButtonexit.setLocation(new Point(240, 183));
jButtonexit.setText("退出");
jButtonexit.setSize(new Dimension(60, 20));
jButtonexit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
buttonexit();
}
});
}
return jButtonexit;
}
/**
* This method initializes jComboBoxstudentname
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBoxstudentname() {
if (jComboBoxstudentname == null) {
jComboBoxstudentname = new JComboBox();
jComboBoxstudentname.setLocation(new Point(150, 50));
jComboBoxstudentname.setEditable(true);
jComboBoxstudentname.setSize(new Dimension(121, 20));
}
return jComboBoxstudentname;
}
/**
* This method initializes jComboBoxbookname
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBoxbookname() {
if (jComboBoxbookname == null) {
jComboBoxbookname = new JComboBox();
jComboBoxbookname.setLocation(new Point(150, 80));
jComboBoxbookname.setEditable(true);
jComboBoxbookname.setSize(new Dimension(121, 20));
}
return jComboBoxbookname;
}
} // @jve:decl-index=0:visual-constraint="10,10"
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?