1.登陸介面:程式名Login.java
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Container;
import java.util.*;
import java.sql.*;
class Login extends JFrame implements ActionListener
{ Container cp=null;
JFrame f=null;
JButton j1,j2;
JTextField t1;
JPasswordField t2;
JLabel jlable1,jlable2;
Color c;
JPanel jp1,jp2;
Login(){
f=new JFrame("小型圖書管理系統");
j1=new JButton("確定");
j2=new JButton("取消");
cp=f.getContentPane();
jlable1=new JLabel(" 輸入使用者名稱");
jlable2=new JLabel(" 使用者密碼");
jp1=new JPanel();
jp2=new JPanel();
t1=new JTextField(18);
t2=new JPasswordField(18);
jp1.add(jlable1);
jp1.add(t1);
jp1.add(jlable2);
jp1.add(t2);
JLabel JL=new JLabel("<html><font color=#CC00FF size='7'><i>歡迎登陸</i></font>",SwingConstants.CENTER);
cp.add(JL,"North");
jp2.add(j1);
jp2.add(j2);
cp.add(jp1,"Center");
cp.add("South",jp2);
jp1.setBackground(new Color(255,153,255));
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();
int x=screen.width; /*取得顯示器視窗的寬度*/
int y=screen.height; /*取得顯示器視窗的高度*/
//setSize(x,y); /*讓系統視窗平鋪整個顯示器視窗*/
f.setSize(300,300);
int xcenter=(x-300)/2;
int ycenter=(y-300)/2;
f.setLocation(xcenter,ycenter);/*顯示在視窗中央*/
f.setVisible(true);
//-----------------------------------------------------
j1.addActionListener(this);//註冊事件監聽器
j2.addActionListener(this);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
public void confirm()//驗證使用者和密碼是否存在
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){System.out.println("載入驅動程式失敗!");}
try{
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Book.mdb";//直接使用當前類目錄下的資料庫檔案
Connection con=DriverManager.getConnection(url);
Statement sql=con.createStatement();
String uname=t1.getText().trim();
String Mima=t2.getText().trim();
String queryMima="select * from user where 使用者名稱='"+uname+"' and 密碼='"+Mima+"'";
ResultSet rs=sql.executeQuery(queryMima);
if(rs.next())
{
new Book(uname);
f.hide();
con.close();
}else{
JOptionPane.showMessageDialog(null,"該使用者不存在","提示!",
JOptionPane.YES_NO_OPTION);
}
t1.setText("");
t2.setText("");
} catch(SQLException g)
{
System.out.println("E Code"+g.getErrorCode());
System.out.println("E M"+g.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("確定")){
confirm();
}
else if(cmd.equals("取消")){
f.dispose();
}
}
public static void main(String []arg){
Login a=new Login();
}
}
2.圖書概覽:程式名BookBrower.java
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Container;
import java.util.*;
import java.sql.*;
class BookBrower implements ActionListener
{
JFrame f;
Container cp;
JPanel jpS,jpanelWest;
JButton jbt1,jbt2;//按鈕,查詢、取消、修改
JLabel label,L; //標籤
//定義文字框
JTable table;//用來接收資料庫中返回的資訊
Object columnName[]={"圖書名","圖書號","單價","作者","出版社","入庫時間"};
Object ar[][] =new Object[80][6];
String sno;
String count="xx";
BookBrower()
{
f=new JFrame();
cp=f.getContentPane(); // 初始化皮膚、按鈕、標籤、文字框
jpS=new JPanel();
jpanelWest=new JPanel();
//------------------------------------------------
jbt1=new JButton("確定");
jbt2=new JButton("返回");
//------------------------------------------------
label=new JLabel("<html><font color=#CC00FF size='4'>圖書概覽</font>",SwingConstants.CENTER);
label.setForeground(Color.blue);
L=new JLabel("書庫現在共有圖書"+count+"本");
//------------------------------------------------
table=new JTable(ar,columnName);//ar存放表中的資料,columnname表示列名
JScrollPane scrollpane = new JScrollPane(table);
//------------------------------------------------
//佈局,新增控制元件
jpS.add(jbt1);
jpS.add(jbt2);
JPanel jpanel=new JPanel();
jpanel.add(label);
JPanel pp4=new JPanel();
JPanel jpE=new JPanel();
cp.add(jpanel,"North");
JPanel jp=new JPanel();
//jp.add(scrollpane);
JPanel p=new JPanel();//用來放兩個表
p.setLayout(new BorderLayout());
p.add(L,"North");
p.add(scrollpane);
cp.add(pp4,"West");
cp.add(p,"Center");
cp.add(jpS,"South");
cp.add(jpE,"East");
//------------------------------------------------
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();
int x=screen.width; /*取得顯示器視窗的寬度*/
int y=screen.height; /*取得顯示器視窗的高度*/
f.setSize(400,330);
int xcenter=(x-350)/2;
int ycenter=(y-330)/2;
f.setLocation(xcenter,ycenter);/*顯示在視窗中央*/
f.setVisible(true);
//-------------------------------------------------
jbt1.addActionListener(this);//註冊監聽器
jbt2.addActionListener(this);
/* f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);*/
}
//------------------------------------------------
int i=0;
public void showRecord()
{
while(i>=0)
{
ar[i][0]="";
ar[i][1]="";
ar[i][2]="";
ar[i][3]="";
ar[i][4]="";
ar[i][5]="";
i--;
}
i=0;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){System.out.println("載入驅動程式失敗!");}
try{
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Book.mdb";//直接使用當前類目錄下的資料庫檔案
Connection con=DriverManager.getConnection(url);
String s="select * from book ";
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery(s);
while(rs.next())
{
String bname=rs.getString(1);
String bno=rs.getString(2);
String price=rs.getString(3);
String writer=rs.getString(4);
String publish=rs.getString(5);
String indate=rs.getString(6);
ar[i][0]=bname;
ar[i][1]=bno;
ar[i][2]=price;
ar[i][3]=writer;
ar[i][4]=publish;
ar[i][5]=indate;
i++;
}
count=""+i+"";
L.setText("書庫現在共有圖書"+count+"本");
f.repaint();
con.close();
}catch(SQLException g)
{
System.out.println("E Code"+g.getErrorCode());
System.out.println("E M"+g.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("確定"))
{
f.hide();
}
if(cmd.equals("返回"))
f.hide();
}
public static void main(String []arg){
BookBrower a=new BookBrower();
a.showRecord();
}
}
3.圖書查詢:程式名QueryBook.java
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Container;
import java.util.*;
import java.sql.*;
class QueryBook implements ActionListener
{
JFrame f3;
Container cp;
JPanel jp1,jp2,jp3,jp4,jp,jpanelWest;
JButton jbt1,jbt2;//按鈕,確定、取消
JLabel label; //標籤:請輸入圖書號
JTextField tf,tf1,tf2,tf3,tf4,tf5,tf6; //定義文字框
JLabel label1,label2,label3,label4;
QueryBook()
{
f3=new JFrame();
cp=f3.getContentPane(); // 初始化皮膚、按鈕、標籤、文字框
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jpanelWest=new JPanel();
jp=new JPanel();
//------------------------------------------------
jbt1=new JButton("確定");
jbt2=new JButton("取消");
//------------------------------------------------
label=new JLabel("<html><font color=#CC00FF size='4'>請輸入圖書號:</font>",SwingConstants.CENTER);
label.setForeground(Color.blue);
tf=new JTextField(20);
//------------------------------------------------
tf1=new JTextField(20);
tf2=new JTextField(20);
tf3=new JTextField(20);
tf4=new JTextField(20);
tf5=new JTextField(20);
tf6=new JTextField(20);
//------------------------------------------------
//佈局,新增控制元件
JPanel jpanel=new JPanel();
jpanel.add(label);
jpanel.add(tf);
JPanel pp4=new JPanel();
JPanel jpane4=new JPanel();
cp.add(jpanel,"North");
JPanel pp2=new JPanel(new GridLayout(6,1));
JPanel pp3=new JPanel();
pp4.setLayout(new GridLayout(6,1));
pp4.add(new JLabel("圖書名",SwingConstants.CENTER));
pp2.add(tf1);
pp4.add(new JLabel("圖書號",SwingConstants.CENTER));
pp2.add(tf2);
pp4.add(new JLabel("單 價",SwingConstants.CENTER));
pp2.add(tf3);
pp4.add(new JLabel("作 者",SwingConstants.CENTER));
pp2.add(tf4);
pp4.add(new JLabel("出版社",SwingConstants.CENTER));
pp2.add(tf5);
pp4.add(new JLabel("入庫時間",SwingConstants.CENTER));
pp2.add(tf6);
pp3.add(jbt1);
pp3.add(jbt2);
cp.add(pp4,"West");
cp.add(pp2,"Center");
cp.add(pp3,"South");
cp.add(jpane4,"East");
//------------------------------------------------
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();
int x=screen.width; /*取得顯示器視窗的寬度*/
int y=screen.height; /*取得顯示器視窗的高度*/
f3.setSize(350,330);
int xcenter=(x-350)/2;
int ycenter=(y-330)/2;
f3.setLocation(xcenter,ycenter);/*顯示在視窗中央*/
f3.setVisible(true);
//-------------------------------------------------
jbt1.addActionListener(this);//註冊監聽器
jbt2.addActionListener(this);
/*f3.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);*/
}
//------------------------------------------------
public void showRecord()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){System.out.println("載入驅動程式失敗!");}
try{
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Book.mdb";//直接使用當前類目錄下的資料庫檔案
Connection con=DriverManager.getConnection(url);
Statement sql;
String ql=tf.getText().trim();
String s="select * from book where 圖書號 ='"+ql +"'";
sql=con.createStatement();
ResultSet rs=sql.executeQuery(s);
if(rs.next())
{
String bname=rs.getString(1);
String bno=rs.getString(2);
String price=rs.getString(3);
String writer=rs.getString(4);
String publish=rs.getString(5);
String indate=rs.getString(6);
tf1.setText(bname);
tf2.setText(bno);
tf3.setText(price);
tf4.setText(writer);
tf5.setText(publish);
tf6.setText(indate);
}
else
{JOptionPane.showMessageDialog(null,"您輸入的圖書號不存在,請重新輸入",
"輸入錯誤", JOptionPane.YES_NO_OPTION);
}
con.close();
}catch(SQLException g)
{
System.out.println("E Code"+g.getErrorCode());
System.out.println("E M"+g.getMessage());
}
tf1.setEditable(false);
tf2.setEditable(false);
tf3.setEditable(false);
tf4.setEditable(false);
tf5.setEditable(false);
tf6.setEditable(false);
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("確定"))
{
showRecord();
tf.setText("");
}
else if(cmd.equals("取消"))
f3.hide();
}
public static void main(String []arg){
QueryBook a=new QueryBook();
}
}
4.圖書刪除:程式名RemoveBook.java
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Container;
import java.util.*;
import java.sql.*;
class RemoveBook implements ActionListener
{
JFrame f;
Container cp;
JPanel jpS,jpanelWest;
JButton jbt1,jbt2,jbt3;//按鈕,查詢、取消、修改
JLabel label,L; //標籤:請輸入學號
JTextField tf; //定義文字框
JTable table;//用來接收資料庫中返回的資訊
Object columnName[]={"圖書名","圖書號","單價","作者","出版社","入庫時間"};
Object ar[][] =new Object[80][6];
String sno;
String count="xx";
RemoveBook()
{
f=new JFrame();
cp=f.getContentPane(); // 初始化皮膚、按鈕、標籤、文字框
jpS=new JPanel();
jpanelWest=new JPanel();
//------------------------------------------------
jbt1=new JButton("查詢");
jbt2=new JButton("取消");
jbt3=new JButton("刪除");
//------------------------------------------------
label=new JLabel("<html><font color=#CC00FF size='4'>請輸入要刪除的圖書名:</font>",SwingConstants.CENTER);
label.setForeground(Color.blue);
L=new JLabel("該種圖書共有"+count+"本");
//------------------------------------------------
table=new JTable(ar,columnName);//ar存放表中的資料,columnname表示列名
JScrollPane scrollpane = new JScrollPane(table);
//------------------------------------------------
tf=new JTextField(18);
//------------------------------------------------
//------------------------------------------------
//佈局,新增控制元件
jpS.add(jbt1);
jpS.add(jbt2);
jpS.add(jbt3);
JPanel jpanel=new JPanel();
jpanel.add(label);
jpanel.add(tf);
JPanel pp4=new JPanel();
JPanel jpE=new JPanel();
cp.add(jpanel,"North");
JPanel jp=new JPanel();
//jp.add(scrollpane);
JPanel p=new JPanel();//用來放兩個表
p.setLayout(new BorderLayout());
p.add(L,"North");
p.add(scrollpane);
cp.add(pp4,"West");
cp.add(p,"Center");
cp.add(jpS,"South");
cp.add(jpE,"East");
//------------------------------------------------
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();
int x=screen.width; /*取得顯示器視窗的寬度*/
int y=screen.height; /*取得顯示器視窗的高度*/
f.setSize(400,330);
int xcenter=(x-350)/2;
int ycenter=(y-330)/2;
f.setLocation(xcenter,ycenter);/*顯示在視窗中央*/
f.setVisible(true);
//-------------------------------------------------
jbt1.addActionListener(this);//註冊監聽器
jbt2.addActionListener(this);
jbt3.addActionListener(this);
/*f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);*/
}
//------------------------------------------------
int i=0;
public void showRecord(String ql)
{
while(i>=0)
{
ar[i][0]="";
ar[i][1]="";
ar[i][2]="";
ar[i][3]="";
ar[i][4]="";
ar[i][5]="";
i--;
}
i=0;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){System.out.println("載入驅動程式失敗!");}
try{
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Book.mdb";//直接使用當前類目錄下的資料庫檔案
Connection con=DriverManager.getConnection(url);
Statement sql;
String s="select * from book where 圖書名 ='"+ql +"'";
sql=con.createStatement();
ResultSet rs=sql.executeQuery(s);
while(rs.next())
{
String bname=rs.getString(1);
String bno=rs.getString(2);
String price=rs.getString(3);
String writer=rs.getString(4);
String publish=rs.getString(5);
String indate=rs.getString(6);
ar[i][0]=bname;
ar[i][1]=bno;
ar[i][2]=price;
ar[i][3]=writer;
ar[i][4]=publish;
ar[i][5]=indate;
i++;
}
count=""+i+"";
L.setText("該種圖書共有"+count+"本");
f.repaint();
con.close();System.out.println(ar[0][1]);
}catch(SQLException g)
{
System.out.println("E Code"+g.getErrorCode());
System.out.println("E M"+g.getMessage());
}
}
public void deleteRecord(int index)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){System.out.println("載入驅動程式失敗!");}
try{
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Book.mdb";//直接使用當前類目錄下的資料庫檔案
Connection con=DriverManager.getConnection(url);
Statement sql;
String ql=(String)(ar[index][1]);
String s="delete * from book where 圖書號 ='"+ql +"'";
sql=con.createStatement();
int del=sql.executeUpdate(s);
if(del==1)
{JOptionPane.showMessageDialog(null,"刪除成功!",
"資訊", JOptionPane.YES_NO_OPTION);
}
con.close();
f.repaint();
}catch(SQLException g)
{
System.out.println("E Code"+g.getErrorCode());
System.out.println("E M"+g.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{
String remember="";
String ql="";
String cmd=e.getActionCommand();
if(cmd.equals("查詢"))
{
ql=tf.getText().trim();
remember=ql;
showRecord(ql);
}
if(cmd.equals("刪除"))
{
int index=table.getSelectedRow();
if( index==-1)
JOptionPane.showMessageDialog(null,"請選定要刪除的表格行",
"輸入錯誤", JOptionPane.YES_NO_OPTION);
else{
deleteRecord(index);
//showRecord(remember);
}
}
if(cmd.equals("取消"))
f.hide();
}
public static void main(String []arg){
RemoveBook a=new RemoveBook();
}
}
5.圖書入庫:程式名BookIn.java
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Container;
import java.util.*;
import java.sql.*;
import javax.swing.text.JTextComponent;
//extends JFrame
class BookIn implements ActionListener
{
JFrame f3;
Container cp;
JPanel jp1,jp2,jp3,jp4,jp,jpanelWest;
JButton jbt1,jbt2;//按鈕:確定、取消、
JLabel label; //標籤
JTextField tf1,tf2,tf3,tf4,tf5,tf6; //定義文字框
JLabel label1,label2,label3,label4;
String sno;
BookIn()
{
f3=new JFrame();
cp=f3.getContentPane(); // 初始化皮膚、按鈕、標籤、文字框
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jpanelWest=new JPanel();
jp=new JPanel();
//------------------------------------------------
jbt1=new JButton("確定");
jbt2=new JButton("取消");
//------------------------------------------------
label=new JLabel("<html><font color=#CC00FF size='4'>圖書入庫</font>",SwingConstants.CENTER);
label.setForeground(Color.blue);
//------------------------------------------------
tf1=new JTextField(20);
tf2=new JTextField(20);
tf3=new JTextField(20);
tf4=new JTextField(20);
tf5=new JTextField(20);
tf6=new JTextField(20);
//------------------------------------------------
//佈局,新增控制元件
jp1.add(jbt1);
jp1.add(jbt2);
sno=tf4.getText();
jp1.add(new JLabel("您好"+sno+"歡迎登陸學生資訊系統"));
JPanel jpanel=new JPanel();
jpanel.add(label);
JPanel pp4=new JPanel();
JPanel jpane4=new JPanel();
cp.add(jpanel,"North");
JPanel pp2=new JPanel(new GridLayout(6,1));
JPanel pp3=new JPanel();
pp4.setLayout(new GridLayout(6,1));
pp4.add(new JLabel("圖書名",SwingConstants.CENTER));
pp2.add(tf1);
pp4.add(new JLabel("圖書號",SwingConstants.CENTER));
pp2.add(tf2);
pp4.add(new JLabel("單 價",SwingConstants.CENTER));
pp2.add(tf3);
pp4.add(new JLabel("作 者",SwingConstants.CENTER));
pp2.add(tf4);
pp4.add(new JLabel("出版社",SwingConstants.CENTER));
pp2.add(tf5);
pp4.add(new JLabel("入庫時間",SwingConstants.CENTER));
pp2.add(tf6);
pp3.add(jbt1);
pp3.add(jbt2);
cp.add(pp4,"West");
cp.add(pp2,"Center");
cp.add(pp3,"South");
cp.add(jpane4,"East");
//------------------------------------------------
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();
int x=screen.width; /*取得顯示器視窗的寬度*/
int y=screen.height; /*取得顯示器視窗的高度*/
f3.setSize(350,330);
int xcenter=(x-350)/2;
int ycenter=(y-330)/2;
f3.setLocation(xcenter,ycenter);/*顯示在視窗中央*/
f3.setVisible(true);
//-------------------------------------------------
jbt1.addActionListener(this);//註冊監聽器
jbt2.addActionListener(this);
/*f3.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);*/
}
//------------------------------------------------
public void insertRecord()
{
if(tf1.getText().equals("")||tf2.getText().equals("")||tf3.getText().equals("")||
tf4.getText().equals("")||tf5.getText().equals("")||tf6.getText().equals(""))
{
JOptionPane.showMessageDialog(f3,"請填寫圖書資料");
return;
}
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){System.out.println("載入驅動程式失敗!");}
try{
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Book.mdb";//直接使用當前類目錄下的資料庫檔案
Connection con=DriverManager.getConnection(url);
Statement sql;
String s="insert into book values('"+tf1.getText()+"','"+tf2.getText()+"','"+
tf3.getText()+"','"+tf4.getText()+"','"+tf5.getText()+"','"+tf6.getText()+"');";
//查詢輸入的圖書號是否在資料庫中存在
String query="select * from book where 圖書號='"+tf2.getText()+"'";
sql=con.createStatement();
ResultSet rs=sql.executeQuery(query);//返回查詢結果集
boolean moreRecords=rs.next();//判斷結果集是否有資料
if(moreRecords)
{
JOptionPane.showMessageDialog(f3,"圖書號已經被使用,請重新輸入");
con.close();
tf2.setText("");
return;
}
int insert=sql.executeUpdate(s);
if(insert==1)
{
JOptionPane.showMessageDialog(null,"圖書資訊錄入成功!");
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
tf6.setText("");
}
}catch(SQLException g)
{
System.out.println("E Code"+g.getErrorCode());
System.out.println("E M"+g.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("確定"))
{
insertRecord();
}
else if(cmd.equals("取消"))
f3.hide();
}
public static void main(String []arg){
BookIn a=new BookIn();
}
}
6.主介面:程式名Book.java
/*本類為使用者介面*/
import java.net.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;import java.util.*;
class Book extends JFrame implements ActionListener
{
JButton QueryScore=new JButton("圖書查詢");
JButton QueryXuefen=new JButton("圖書入庫");
JButton jiangfa=new JButton("圖書刪除");
JButton xuanke=new JButton("圖書概覽");
JButton gaiMima=new JButton("修改密碼");
JMenuBar mb = new JMenuBar();//選單欄
JPanel jp=new JPanel();//用來填放子模組
Container cp=getContentPane();
String username;
Book(){}
Book(String username)
{
this.username=username;
mb.add(QueryScore);
mb.add(QueryXuefen);
mb.add(jiangfa);
mb.add(xuanke);
mb.add(gaiMima);
cp.add(mb,"North");
// 設定邊框
jp.setBorder(BorderFactory.createTitledBorder(BorderFactory
.createLineBorder(Color.blue, 2),null,
TitledBorder.CENTER, TitledBorder.TOP));
jp.setLayout(new BorderLayout());
JLabel label1 = new JLabel(new ImageIcon("4.jpg"));
jp.add(label1);
/* JLabel JL=new JLabel("<html><font color=#CC00FF size='7'><i>歡迎登陸</i></font>",SwingConstants.CENTER);
jp.add(JL,"North");*/
JLabel label2 = new JLabel(new ImageIcon("2.jpg"));
//jp.add(label2,"South");
JScrollPane scrollpane=new JScrollPane(jp);
cp.add(scrollpane);
setTitle("歡迎登陸");
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();
int x=screen.width; /*取得顯示器視窗的寬度*/
int y=screen.height; /*取得顯示器視窗的高度*/
//setSize(x,y); /*讓系統視窗平鋪整個顯示器視窗*/
setSize(600,600);
int xcenter=(x-600)/2;
int ycenter=(y-600)/2;
setLocation(xcenter,ycenter);/*顯示在視窗中央*/
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//註冊臨聽器
QueryScore.addActionListener(this);
QueryXuefen.addActionListener(this);
jiangfa.addActionListener(this);
xuanke.addActionListener(this);
gaiMima.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if (cmd.equals("圖書查詢"))
{
new QueryBook();
}
if (cmd.equals("圖書入庫"))
{
new BookIn();
}
if (cmd.equals("圖書刪除"))
{
new RemoveBook();
}
if (cmd.equals("圖書概覽"))
{
new BookBrower().showRecord();
}
if (cmd.equals("修改密碼"))
{
new UpdateMima(username);
}
}
public static void main(String[]args)
{
new Book("");
}
}
7.密碼修改:程式名UpdateMima.java
import java.net.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
class UpdateMima extends JFrame implements ActionListener
{
JFrame f;
Container cp;
JPanel jp1,jp2,jp3,jp4,jp,jpanelWest;
JButton jbt1,jbt2;//按鈕,確定、取消
JLabel label; //標籤:修改密碼
JTextField name;
JPasswordField tf1,tf2,tf3; //定義文字框
JLabel label1,label2,label3,label4;
String sno;
UpdateMima(){}
UpdateMima(String username)
{
sno=username;
f=new JFrame();
cp=f.getContentPane(); // 初始化
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jpanelWest=new JPanel();
jp=new JPanel();
//------------------------------------------------
jbt1=new JButton("確定");
jbt2=new JButton("取消");
//------------------------------------------------
label=new JLabel("<html><font color=#CC00FF size='4'>修改密碼</font>",SwingConstants.CENTER);
label.setForeground(Color.blue);
label.setFont(new Font("BOLD",Font.BOLD,15));
name=new JTextField(20);
//name.setEditable(false);
//------------------------------------------------
tf1=new JPasswordField(20);
tf2=new JPasswordField(20);
tf3=new JPasswordField(20);
//------------------------------------------------
//佈局,新增控制元件
jp1.add(jbt1);
jp1.add(jbt2);
jp1.add(new JLabel("您好"+"xx"+"歡迎登陸學生資訊系統"));
JPanel jpanel=new JPanel();
jpanel.add(label);
JPanel pp4=new JPanel();
JPanel jpane4=new JPanel();
cp.add(jpanel,"North");
JPanel pp2=new JPanel(new GridLayout(6,1));
JPanel pp3=new JPanel();
pp4.setLayout(new GridLayout(6,1));
pp4.add(new JLabel("使用者名稱: ",SwingConstants.RIGHT));
pp2.add(name);
pp4.add(new JLabel("原密碼: ",SwingConstants.RIGHT));
pp2.add(tf1);
pp4.add(new JLabel(" 新密碼: ",SwingConstants.RIGHT));
pp2.add(tf2);
pp4.add(new JLabel("確認密碼: ",SwingConstants.RIGHT));
pp2.add(tf3);
pp2.add(new JLabel());
JPanel jpbutton=new JPanel();
jpbutton.add(jbt1);
jpbutton.add(jbt2);
pp2.add(jpbutton);
//pp3.add(jbt1);
//pp3.add(jbt2);
cp.add(pp4,"West");
cp.add(pp2,"Center");
//cp.add(pp3,"South");
cp.add(jpane4,"East");
//------------------------------------------------
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();
int x=screen.width; /*取得顯示器視窗的寬度*/
int y=screen.height;
f.setSize(350,330); /*取得顯示器視窗的高度*/
int xcenter=(x-350)/2;
int ycenter=(y-330)/2;
f.setLocation(xcenter,ycenter);/*顯示在視窗中央*/
//f.setTitle("修改密碼");
f.setVisible(true);
//-------------------------------------------------
jbt1.addActionListener(this);//註冊監聽器
jbt2.addActionListener(this);
/*f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);*/
}
public void updateM()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){System.out.println("載入驅動程式失敗!");}
try{
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Book.mdb";//直接使用當前類目錄下的資料庫檔案
Connection con=DriverManager.getConnection(url);
Statement sql=con.createStatement();
String uname=name.getText().trim();
String queryMima="select * from user where 使用者名稱='"+uname+"'";
ResultSet rs=sql.executeQuery(queryMima);
if(rs.next())
{
String newMima=tf2.getText().trim();
String s="update user set 密碼='"+newMima+"' where 使用者名稱 ='"+uname +"'";
sql=con.createStatement();
int updateMima=sql.executeUpdate(s);
if(updateMima==1)
{
JOptionPane.showMessageDialog(f,"密碼修改成功!");
}
con.close();
f.repaint();
}else{
JOptionPane.showMessageDialog(null,"該使用者不存在","警告!",
JOptionPane.YES_NO_OPTION);
}
name.setText("");
tf1.setText("");
tf2.setText("");
tf3.setText("");
} catch(SQLException g)
{
System.out.println("E Code"+g.getErrorCode());
System.out.println("E M"+g.getMessage());
}
}
//------------------------------------------------
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("確定"))
{
if(name.getText().equals("")||tf1.getText().equals("")||tf2.getText().equals("")||tf3.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"請填寫使用者的所有資訊","提示",
JOptionPane.YES_NO_OPTION);
return;
}
if(tf2.getText().trim().equals(tf3.getText().trim()))
updateM();
}
else if(cmd.equals("取消"))
f.hide();
}
public static void main(String[]args)
{
new UpdateMima("");
}
}