上传者: shenzhouty
|
上传时间: 2021-12-16 17:30:05
|
文件大小: 16KB
|
文件类型: -
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.sql.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Java{
public static void main(String args[]){
One one=new One();
}
}
class One extends JFrame implements ActionListener{
JLabel jlabel1,jlabel2;
JButton jbutton1,jbutton2;
JTextField jtextfield1;
JPanel jpanel1;
JPasswordField passwordText;
One(){
jpanel1=new JPanel();
jlabel1=new JLabel("用户名:");
jlabel2=new JLabel("密 码:");
jbutton1=new JButton("登录");
jbutton2=new JButton("取消");
Font f=new Font("楷体_GB2312",Font.BOLD,22);
jlabel1.setFont(f);
jlabel2.setFont(f);
jtextfield1=new JTextField(10);
passwordText=new JPasswordField(10);
passwordText.setEchoChar('*');
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
setLayout(new FlowLayout());
jpanel1.add(jbutton1);
jpanel1.add(jbutton2);
add(jlabel1);
add(jtextfield1);
add(jlabel2);
add(passwordText);
add(jpanel1);
setBounds(100,100,200,200);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jbutton1){
String str1,str2;
str1=jtextfield1.getText().trim();
str2=passwordText.getText();
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ee){
System.out.println(""+ee);
}
try{
con=DriverManager.getConnection("jdbc:odbc:renshi","","");
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM login");
boolean boo=true;
while(rs.next()){
if(rs.getString(1).equals(str1) && rs.getString(2).equals(str2)){
JOptionPane.showMessageDialog(this,"欢迎进入图书管理系统","login",JOptionPane.PLAIN_MESSAGE);
setVisible(false);
FrameMain t=new FrameMain();
boo=false;
}
}
if(boo){
JOptionPane.showMessageDialog(this,"用户不存在或者输入错误请重新输入!","提示",JOptionPane.WARNING_MESSAGE);
jtextfield1.setText("");
passwordText.setText("");
}
con.close();
}catch(SQLException eee){System.out.println(eee);}
}
else{
System.exit(0);
}
}
}
class FrameMain extends JFrame implements ActionListener{
JPanel contentPane;
JMenuBar mb1=new JMenuBar();
JMenu readr=new JMenu("读者");
JMenu book=new JMenu("图书");
JMenu help=new JMenu("帮助");
JMenu exit=new JMenu("退出");
JMenuItem r1=new JMenuItem("浏览读者信息");
JMenuItem b1=new JMenuItem("浏览图书信息");
JMenuItem b2=new JMenuItem("增加图书");
JMenuItem b3=new JMenuItem("删除图书");
JMenuItem b4=new JMenuItem("修改图书信息");
JMenuItem h1=new JMenuItem("使用网络信息帮助");
JMenuItem h2=new JMenuItem("读取java文件帮助信息");
JMenuItem e1=new JMenuItem("退出系统");
FrameMain(){
setTitle("图书馆信息管理系统");
readr.add(r1);
book.add(b1);
book.add(b2);
book.add(b3);
book.add(b4);
help.add(h1);
help.add(h2);
exit.add(e1);
mb1.add(readr);
mb1.add(book);
mb1.add(help);
mb1.add(exit);
setJMenuBar(mb1);
r1.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
h1.addActionListener(this);
h2.addActionListener(this);
e1.addActionListener(this);
setBounds(200,200,500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==r1){
new showReader();
}
else if(e.getSource()==b1){
new showBook();
}
else if(e.getSource()==b2){
new InsertBook();
}
else if(e.getSource()==b3){
new Delete();
}
else if(e.getSource()==b4){
new Modify();
}
else if(e.getSource()==h1){
Help1 flink=new Help1();
}
else if(e.getSource()==h2){
class Help2 extends JFrame {
Help2(){
byte b[]=new byte[30];
JTextArea text=new JTextArea(20,20);
setSize(330,300);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
add(text,BorderLayout.CENTER);
try{
FileInputStream input=new FileInputStream("help.java");
ProgressMonitorInputStream in=new ProgressMonitorInputStream(this,"读取help文件",input);
ProgressMonitor p=in.getProgressMonitor();
while(in.read(b)!=-1){
String s=new String(b);
text.append(s);
Thread.sleep(200);
}
}catch(InterruptedException e){}
catch(IOException e){}
}
}
new Help2();
}
else if(e.getSource()==e1){
System.exit(0);
}
}
}
class Help1 extends JFrame implements ActionListener,Runnable{
JButton button;
URL url;
JTextField text;
JEditorPane editPane;
byte b[]=new byte[118];
Thread thread;
public Help1(){
text=new JTextField(20);
editPane=new JEditorPane();
editPane.setEditable(false);
button=new JButton("确定");
button.addActionListener(this);
thread=new Thread(this);
JPanel p=new JPanel();
p.add(new JLabel("输入网址: "));
p.add(text);
p.add(button);
Container con=getContentPane();
con.add(new JScrollPane(editPane),BorderLayout.CENTER);
con.add(p,BorderLayout.NORTH);
setBounds(60,60,500,500);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
editPane.addHyperlinkListener(new HyperlinkListener (){
public void hyperlinkUpdate(HyperlinkEvent e){
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
try{
editPane.setPage(e.getURL());
}catch(IOException e1){
editPane.setText(""+e1);
}
}
}
});
}
public void actionPerformed(ActionEvent e){
if(!(thread.isAlive()))
thread=new Thread(this);
try{
thread.start();
}catch(Exception ee){
text.setText("我正在读取"+url);
}
}
public void run(){
try{
int n=-1;
editPane.setText(null);
url=new URL(text.getText().trim());
editPane.setPage(url);
}catch(MalformedURLException e1){
text.setText(""+e1);
return;
}catch(IOException eee){
text.setText(""+eee);
return;
}
}
}
class InsertBook extends JDialog implements ActionListener{
JLabel insertBook;
Object name[]={"编号","书名","出版社","出版日期"};
Object a[][]=new Object[1][4];
JTable table;
JButton insert;
Connection con;
Statement sql;
ResultSet rs;
InsertBook(){
insertBook=new JLabel("请输入新书信息:");
table=new JTable(a,name);
insert=new JButton("确定插入");
setLayout(new FlowLayout());
add(insertBook);
add(table);
add(insert);
setBounds(100,100,800,200);
setVisible(true);
insert.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ee){
System.out.println(""+ee);
}
try{
con=DriverManager.getConnection("jdbc:odbc:renshi","","");
sql=con.createStatement();
int k=sql.executeUpdate("INSERT INTO bookinformation VALUES('"+a[0][0]+"','"+a[0][1]+"','"+a[0][2]+"','"+a[0][3]+"')");
if(k==1)
JOptionPane.showMessageDialog(this,"插入成功","成功",JOptionPane.PLAIN_MESSAGE);
setVisible(false);
con.close();
} catch(SQLException ee){
JOptionPane.showMessageDialog(this,"插入失败","失败",JOptionPane.ERROR_MESSAGE);
}
}
}
class showReader extends JDialog implements ActionListener{
JTable table;
Object a[][];
Object name[]={"学号","姓名","年龄","出生日期"} ;
JButton showReader;
showReader(){
showReader=new JButton("显示信息");
showReader.addActionListener(this);
add(showReader,BorderLayout.NORTH);
setBounds(200,200,500,500);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ee){
System.out.println(""+ee);
}
try{
con=DriverManager.getConnection("jdbc:odbc:renshi","","");
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("SELECT * FROM readerinformation");
rs.last();
int lastNumber=rs.getRow();
a=new Object[lastNumber][4];
int k=0;
rs.beforeFirst();
while(rs.next()){
a[k][0]=rs.getString(1);
a[k][1]=rs.getString(2);
a[k][2]=rs.getString(3);
a[k][3]=rs.getString(4);
k++;
}
con.close();
}catch(SQLException ee){
System.out.println(ee);
}
table=new JTable(a,name);
getContentPane().removeAll();
add(showReader,BorderLayout.NORTH);
add(new JScrollPane(table),BorderLayout.CENTER);
setVisible(true);
validate();
}
}
class showBook extends JDialog implements ItemListener{
JTable table;
Object a[][];
Object name[]={"编号","书名","出版社","出版日期"} ;
JCheckBox showBook;
showBook(){
showBook=new JCheckBox("显示信息");
showBook.addItemListener(this);
add(showBook,BorderLayout.NORTH);
setBounds(200,200,500,500);
setVisible(true);
}
public void itemStateChanged(ItemEvent e){
JCheckBox box=(JCheckBox)e.getItemSelectable();
if(box.isSelected()){
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ee){
System.out.println(""+ee);
}
try{
con=DriverManager.getConnection("jdbc:odbc:renshi","","");
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("SELECT * FROM bookinformation");
rs.last();
int lastNumber=rs.getRow();
a=new Object[lastNumber][4];
int k=0;
rs.beforeFirst();
while(rs.next()){
a[k][0]=rs.getString(1);
a[k][1]=rs.getString(2);
a[k][2]=rs.getString(3);
a[k][3]=rs.getString(4);
k++;
}
con.close();
}catch(SQLException ee){
System.out.println(ee);
}
table=new JTable(a,name);
getContentPane().removeAll();
add(showBook,BorderLayout.NORTH);
add(new JScrollPane(table),BorderLayout.CENTER);
setVisible(true);
validate();
}
else table.setVisible(false);
}
}
class Delete extends JDialog implements ActionListener{
JLabel input;
JTextField inputNumber;
Object name[]={"编号","书名","出版社","出版日期"} ;
Object a[][]=new Object[1][4];
JTable table;
JButton delete;
JPanel pane=new JPanel();
Connection con;
Statement sql;
ResultSet rs;
String num;
Delete(){
input=new JLabel("请输入删除书的编号:(以回车结束)");
inputNumber=new JTextField(20);
delete=new JButton("删除");
table=new JTable(a,name);
pane.add(input);
pane.add(inputNumber);
add(pane,BorderLayout.NORTH);
add(table,BorderLayout.CENTER);
add(delete,BorderLayout.SOUTH);
inputNumber.addActionListener(this);
delete.addActionListener(this);
setBounds(100,100,800,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException eee){
System.out.println(""+eee);
}
try{
num=inputNumber.getText().trim();
con=DriverManager.getConnection("jdbc:odbc:renshi","","");
sql=con.createStatement();
rs=sql.executeQuery("SELECT *FROM bookinformation where 编号='"+num+"'");
boolean boo=rs.next();
if(boo==false){
JOptionPane.showMessageDialog(this,"编号不存在","提示",JOptionPane.WARNING_MESSAGE);
}
else{
a[0][0]=rs.getString(1);
a[0][1]=rs.getString(2);
a[0][2]=rs.getString(3);
a[0][3]=rs.getDate(4).toString();
table.repaint();
if(e.getSource()==delete){
int n=JOptionPane.showConfirmDialog(this,"确认要删除吗?","确认对话框",JOptionPane.YES_OPTION);
if(n==JOptionPane.YES_OPTION){
sql=con.createStatement();
int k=sql.executeUpdate("DELETE FROM bookinformation WHERE 编号='"+num+"'");
if(k==1)
{ JOptionPane.showMessageDialog(this,"删除成功!","删除",JOptionPane.PLAIN_MESSAGE);
setVisible(false);
}
else {
JOptionPane.showMessageDialog(this,"删除失败!","删除",JOptionPane.ERROR_MESSAGE);
setVisible(false);
}
}
}
}
con.close();
}catch(SQLException ee){
System.out.println(ee);
}
}
}
class Modify extends JDialog implements ActionListener{
JLabel infor;
JTextField inputNumber;
Object name[]={"编号","书名","出版社","出版日期"};
Object a[][]=new Object[1][4];
JTable table;
JButton queren;
Connection con;
Statement sql;
ResultSet rs;
String num;
Modify(){
infor=new JLabel("输入编号(以回车确认):");
inputNumber=new JTextField(20);
table=new JTable(a,name);
queren=new JButton("修改记录");
setLayout(new FlowLayout());
infor.setPreferredSize(new Dimension(100,70));
add(infor);
add(inputNumber);
add(table);
queren.setPreferredSize(new Dimension(80,50));
add(queren);
inputNumber.addActionListener(this);
queren.addActionListener(this);
setBounds(20,60,300,500);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ee){
System.out.println(""+ee);
}
if(e.getSource()==inputNumber){
try{
num=inputNumber.getText().trim();
con=DriverManager.getConnection("jdbc:odbc:renshi","","");
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM bookinformation WHERE 编号='"+num+"'");
boolean boo=rs.next();
if(boo==false){
JOptionPane.showMessageDialog(this,"编号不存在","提示",JOptionPane.WARNING_MESSAGE);
}
else{
a[0][0]=rs.getString(2);
a[0][1]=rs.getString(3);
a[0][2]=rs.getDate(4).toString();
table.repaint();
}
con.close();
} catch(SQLException eee){
System.out.println(eee);
}
}
if(e.getSource()==queren){
try{
table.repaint();
con=DriverManager.getConnection("jdbc:odbc:renshi","","");
sql=con.createStatement();
int k=sql.executeUpdate("UPDATE bookinformation SET 书名='"+a[0][0]+"',出版社='"+a[0][1]+"',出版日期 ='"+a[0][2]+"' WHERE 编号='"+num+"'");
if(k==1){
JOptionPane.showMessageDialog(this,"更新成功","成功",JOptionPane.PLAIN_MESSAGE);
con.close();
setVisible(false);
}
}catch(SQLException eeee){
JOptionPane.showMessageDialog(this,"更新失败"+eeee,"失败",JOptionPane.ERROR_MESSAGE);
}
}
}
}