?? bookreturnupdate.java
字號:
package bookmanager;
import javax.swing.*;
import java.awt.event.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.sql.*;
public class BookReturnUpdate extends JFrame implements ActionListener
{
public BookReturnUpdate()
{
setTitle("書籍還入信息修改");
setSize(200,200);
gl=new GridLayout(4,2,20,20);
gl2=new GridLayout(1,4,20,20);
JPanel panel=new JPanel();
JPanel panel2=new JPanel();
panel.setLayout(gl);
panel2.setLayout(gl2);
namelbl=new JLabel("借閱者編號(輸入借閱者編號按確定即可調出相關信息)");
nametex=new JTextField("");
booknamelbl=new JLabel("書編號(輸入書編號按確定即可調出相關信息)");
booknametex=new JTextField("");
datelbl=new JLabel("還書日期");
datetex=new JTextField("");
detaillbl=new JLabel("備注");
detailtex=new JTextField("");
cleanbtn=new JButton("清除");
surebtn=new JButton("確定");
editbtn=new JButton("更新");
cancelbtn=new JButton("退出");
panel.add(namelbl);
panel.add(nametex);
panel.add(booknamelbl);
panel.add(booknametex);
panel.add(datelbl);
panel.add(datetex);
panel.add(detaillbl);
panel.add(detailtex);
panel2.add(cleanbtn);
panel2.add(surebtn);
panel2.add(editbtn);
panel2.add(cancelbtn);
cleanbtn.addActionListener(this);
surebtn.addActionListener(this);
editbtn.addActionListener(this);
cancelbtn.addActionListener(this);
getContentPane().add(panel,BorderLayout.CENTER);
getContentPane().add(panel2,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
Object source=e.getSource();
if(source==cleanbtn)
{
nametex.setText("");
booknametex.setText("");
detailtex.setText("");
datetex.setText("");
}
if(source==cancelbtn)
{
this.dispose();
}
if(source==surebtn)
{
String bookid=booknametex.getText().trim();
String userid=nametex.getText().trim();
if((bookid.equals(""))&&(userid.equals("")))
{
JOptionPane.showMessageDialog(null,"請輸入書編號或者用戶編號");
return;
}
String sql;
if(bookid.equals(""))
{
Long intuserid=new Long(userid);
sql=("select * from borrow where userid="+intuserid);
rs=db.getResult(sql);
boolean isexist=false;
try
{
//把游標指向結果集的第一行,如果沒有數據則返回false
isexist=rs.first();
}
catch(SQLException w)
{
System.out.println(w);
}
//判斷結果是否正確
if(!isexist)
{
JOptionPane.showMessageDialog(null,"沒有相關資料");
nametex.setText("");
return;
}
try
{
namelbl.setText("用戶名(修改請輸入用戶編號)");
booknamelbl.setText("書名(修改請輸入書編號)");
borrowid=Long.valueOf(rs.getString(1));
booknametex.setText(rs.getString(3));
nametex.setText(rs.getString(2));
detailtex.setText(rs.getString(6));
datetex.setText(rs.getString(5));
}
catch(SQLException e2)
{
System.out.println(e2);
}
}
if(userid.equals(""))
{
Long intbookid=new Long(bookid);
sql=("select * from borrow where bookid="+intbookid);
rs=db.getResult(sql);
boolean isexist=false;
try
{
//把游標指向結果集的第一行,如果沒有數據則返回false
isexist=rs.first();
}
catch(SQLException w)
{
System.out.println(w);
}
//判斷結果是否正確
if(!isexist)
{
JOptionPane.showMessageDialog(null,"沒有相關資料");
nametex.setText("");
return;
}
try
{
namelbl.setText("用戶名(修改請輸入用戶編號)");
booknamelbl.setText("書名(修改請輸入書編號)");
booknametex.setText(rs.getString(3));
nametex.setText(rs.getString(2));
detailtex.setText(rs.getString(5));
datetex.setText(rs.getString(4));
}
catch(SQLException e2)
{
System.out.println(e2);
}
}
}
if(source==editbtn)
{
String bookid=booknametex.getText().trim();
Long intbookid=new Long(bookid);
String userid=nametex.getText().trim();
Long intuserid=new Long(userid);
String borrowdate=datetex.getText().trim();
String detail=detailtex.getText().trim();
if((bookid.equals(""))||(userid.equals(""))||(borrowdate.equals("")))
{
JOptionPane.showMessageDialog(null,"前三項不能為空");
return;
}
String sql=("select * from user where id="+intuserid);
rs=db.getResult(sql);
boolean isexist=false;
try
{
//把游標指向結果集的第一行,如果沒有數據則返回false
isexist=rs.first();
}
catch(SQLException w)
{
System.out.println(w);
}
//判斷結果是否正確
if(!isexist)
{
JOptionPane.showMessageDialog(null,"用戶名不存在");
nametex.setText("");
return;
}
try
{
userid=rs.getString(2);
}
catch(SQLException e2)
{
System.out.println(e2);
}
sql=("select * from book where id="+intbookid);
rs=db.getResult(sql);
isexist=false;
try
{
//把游標指向結果集的第一行,如果沒有數據則返回false
isexist=rs.first();
}
catch(SQLException w2)
{
System.out.println(w2);
}
//判斷結果是否正確
if(!isexist)
{
JOptionPane.showMessageDialog(null,"書不存在");
booknametex.setText("");
return;
}
try
{
bookid=rs.getString(2);
}
catch(SQLException e3)
{
System.out.println(e);
}
namelbl.setText("用戶名");
booknamelbl.setText("書名");
nametex.setText(userid);
booknametex.setText(bookid);
int confirm=JOptionPane.showConfirmDialog(null,"確認信息","修改確認",JOptionPane.YES_NO_OPTION);
if(confirm==JOptionPane.YES_OPTION)
{
sql=("update borrow set studentname='"+userid+"',bookname='"+bookid+"',returndate='"+borrowdate+"',comment='"+detail+" ',bookid="+intbookid+",userid="+intuserid+" where id="+borrowid);
System.out.println(sql);
boolean success=db.executeSql(sql);
if(!success)
{
JOptionPane.showMessageDialog(null,"不成功,請重試");
}
else
{
JOptionPane.showMessageDialog(null,"成功");
this.dispose();
}
}
else
{
this.dispose();
return;
}
}
}
private JLabel namelbl;
private JLabel booknamelbl;
private JLabel datelbl;
private JLabel detaillbl;
private JTextField nametex;
private JTextField booknametex;
private JTextField datetex;
private JTextField detailtex;
private JButton cleanbtn;
private JButton surebtn;
private JButton editbtn;
private JButton cancelbtn;
private GridLayout gl;
private GridLayout gl2;
private DBManager db=new DBManager();
private ResultSet rs;
//借閱表的ID
private Long borrowid=new Long(0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -