?? bookborrow.java
字號:
package bookmanager;
import javax.swing.*;
import java.awt.event.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.sql.*;
public class BookBorrow extends JFrame implements ActionListener
{
public BookBorrow()
{
setTitle("書籍出借");
setSize(200,200);
gl=new GridLayout(4,2,20,20);
gl2=new GridLayout(1,3,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("確定");
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(cancelbtn);
cleanbtn.addActionListener(this);
surebtn.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();
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=("insert into borrow (studentname,bookname,borrowdate,returndate,comment,bookid,userid) values('"+userid+"','"+bookid+"','"+borrowdate+"',' ','"+detail+" "+"',"+intbookid+","+intuserid+")");
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 cancelbtn;
private GridLayout gl;
private GridLayout gl2;
private DBManager db=new DBManager();
private ResultSet rs;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -