📄 readerjiemian.java
字号:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.sql.*;
class readerAdd extends Frame implements ActionListener
{
Label label1,label2,label3,label4,label5,label6;
TextField text1,text2,text3,text4,text5,text6;
TextArea area;
Button button1,button2;
readerAdd(String s)
{
super(s);
setLayout(null);
setBackground(Color.lightGray);
Font f=new Font("",Font.LAYOUT_LEFT_TO_RIGHT,20);
label1=new Label("编号"); label1.setFont(f); label1.setForeground(Color.blue);
label2=new Label("姓名"); label2.setFont(f); label2.setForeground(Color.blue);
label3=new Label("性别"); label3.setFont(f); label3.setForeground(Color.blue);
label4=new Label("出生日期"); label4.setFont(f); label4.setForeground(Color.blue);
label5=new Label("联系电话"); label5.setFont(f); label5.setForeground(Color.blue);
label6=new Label("登记日期"); label6.setFont(f); label6.setForeground(Color.blue);
text1=new TextField(15); text1.setFont(f); text1.setBackground(Color.cyan);
text2=new TextField(15); text2.setFont(f); text2.setBackground(Color.cyan);
text3=new TextField(15); text3.setFont(f); text3.setBackground(Color.cyan);
text4=new TextField(15); text4.setFont(f); text4.setBackground(Color.cyan);
text5=new TextField(15); text5.setFont(f); text5.setBackground(Color.cyan);
text6=new TextField(15); text6.setFont(f); text6.setBackground(Color.cyan);
area=new TextArea(20,10); area.setFont(f); area.setBackground(Color.lightGray); area.setEditable(false);
Font g=new Font("",Font.BOLD,16);
button1=new Button("添加"); button1.setFont(g); button1.addActionListener(this);
button2=new Button("刷新"); button2.setFont(g); button2.addActionListener(this);
setBounds(300,250,600,450);
label1.setBounds(50,50,80,30); add(label1);
label2.setBounds(50,90,80,30); add(label2);
label3.setBounds(50,130,80,30); add(label3);
label4.setBounds(50,170,80,30); add(label4);
label5.setBounds(50,210,80,30); add(label5);
label6.setBounds(50,250,80,30); add(label6);
text1.setBounds(160,50,150,30); add(text1);
text2.setBounds(160,90,150,30); add(text2);
text3.setBounds(160,130,150,30); add(text3);
text4.setBounds(160,170,150,30); add(text4);
text5.setBounds(160,210,150,30); add(text5);
text6.setBounds(160,250,150,30); add(text6);
area.setBounds(320,50,200,230);
add(area);
button1.setBounds(170,320,90,30);
button2.setBounds(320,320,90,30);
add(button1);
add(button2);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
int n=JOptionPane.showConfirmDialog(this, "确认要添加吗?","确认对话框",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==JOptionPane.NO_OPTION||n==JOptionPane.CANCEL_OPTION)
{
area.setText("已经取消添加读者信息\n");
}
else if(n==JOptionPane.YES_OPTION)
{
if(text1.getText().equals("")&&text2.getText().equals(""))
{
JOptionPane.showMessageDialog(this, "缺少重要信息","警告",JOptionPane.WARNING_MESSAGE);
}
else
{
Connection con;
Statement sql;
// ResultSet rs;
String insertStr;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1){ area.setText("没有建立连接\n");}
try
{
con=DriverManager.getConnection("jdbc:odbc:bcfshuju","","");
sql=con.createStatement();
sql.executeUpdate("insert into reader values"+"("+"'"+text1.getText()+"'"+","+"'"+text2.getText()+"'"+","+"'"+text3.getText()+"'"+","+"'"+text4.getText()+"'"+","+"'"+text5.getText()+"'"+","+"'"+text6.getText()+"'"+")"+";");
JOptionPane.showConfirmDialog(this, "信息插入成功?","确认对话框",JOptionPane.PLAIN_MESSAGE);
con.close();
}
catch (SQLException e2)
{
JOptionPane.showConfirmDialog(this, "信息插入失败?","确认对话框",JOptionPane.PLAIN_MESSAGE);
}
}
}
text1.setText(null);
text2.setText(null);
text3.setText(null);
text4.setText(null);
text5.setText(null);
text6.setText(null);
}
else if(e.getSource()==button2)
{
text2.setText(null);
text3.setText(null);
text4.setText(null);
text5.setText(null);
text6.setText(null);
area.setText(null);
}
}
}
class readerDel extends Frame implements ActionListener
Label label1,label2,label3,label4,label5,label6;
TextField text1,text2,text3,text4,text5,text6;
TextArea area;
Box box1,box2,box3,basebox;
Button button1;
readerDel(String s)
{
super(s);
setLayout(null);
setBackground(Color.lightGray);
Font f=new Font("",Font.LAYOUT_LEFT_TO_RIGHT,20);
setBounds(300,250,550,450);
setBackground(Color.orange);
label1=new Label("编号"); label1.setFont(f); label1.setForeground(Color.blue);
label2=new Label("姓名"); label2.setFont(f); label2.setForeground(Color.blue);
label3=new Label("性别"); label3.setFont(f); label3.setForeground(Color.blue);
label4=new Label("登记日期"); label4.setFont(f); label4.setForeground(Color.blue);
text1=new TextField(15); text1.setFont(f); text1.setBackground(Color.cyan);
text2=new TextField(15); text2.setFont(f); text2.setBackground(Color.cyan);
text3=new TextField(15); text3.setFont(f); text3.setBackground(Color.cyan);
text4=new TextField(15); text4.setFont(f); text4.setBackground(Color.cyan);
button1=new Button("删除");
button1.setBounds(230,350,90,30);
button1.addActionListener(this);
add(button1);
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
box3=Box.createVerticalBox();
box1.setBounds(100,100,150,200);
box2.setBounds(250,100,200,200);
box1.add(label1);
box1.add(Box.createVerticalStrut(10));
box1.add(label2);
box1.add(Box.createVerticalStrut(10));
box1.add(label3);
box1.add(Box.createVerticalStrut(10));
box1.add(label4);
box1.add(Box.createVerticalStrut(10));
box2.add(text1);
box2.add(Box.createVerticalStrut(10));
box2.add(text2);
box2.add(Box.createVerticalStrut(10));
box2.add(text3);
box2.add(Box.createVerticalStrut(10));
box2.add(text4);
box2.add(Box.createVerticalStrut(10));
add(box1);
add(box2);
box1.setBackground(Color.green);
box2.setBackground(Color.green);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
public void actionPerformed (ActionEvent e)
{
Connection con;
Statement sql;
String delStr;
if(e.getSource()==button1)
{
int n=JOptionPane.showConfirmDialog(this, "确认要删除吗?","确认对话框",JOptionPane.OK_CANCEL_OPTION);
if(n==JOptionPane.CANCEL_OPTION)
{
JOptionPane.showMessageDialog(this, "取消删除", "确认",JOptionPane.OK_OPTION);
}
else
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1)
{
JOptionPane.showMessageDialog(this, "没有建立连接", "警告",JOptionPane.WARNING_MESSAGE);
}
if(text1.getText().equals(""))
{
delStr="delete * from reader where no="+text1.getText()+";";
}
else if(text2.getText().equals(""))
{
delStr="delete * from reader where reName="+text2.getText()+";";
}
else if(text3.getText().equals(""))
{
delStr="delete * from reader where sex="+text3.getText()+";";
}
else if(text4.getText().equals(""))
{
delStr="delete *from reader where loTime="+text4.getText()+";";
}
else if(text1.getText()==""&&text2.getText()==""&&text3.getText()==""&&text4.getText()=="")
{
JOptionPane.showMessageDialog(this, "不能为空值", "警告",JOptionPane.WARNING_MESSAGE);
}
try
{
con=DriverManager.getConnection("jdbc:odbc:bcfshuju","","");
sql=con.createStatement();
sql.executeUpdate("del");
con.close();
}
catch (SQLException e1)
{
JOptionPane.showConfirmDialog(this, "信息删除失败?","确认对话框",JOptionPane.PLAIN_MESSAGE);
}
}
}
text1.setText(null);
text2.setText(null);
text3.setText(null);
text4.setText(null);
text5.setText(null);
}
}
class readerSearch extends Frame implements ActionListener
{
Label label1,label2,label3,label4,label5;
TextField text1,text2,text3,text4,text5;
Button button;
TextArea area;
readerSearch(String s)
{
super(s);
setBounds(400,300,560,500);
setVisible(true);
setResizable(false);
setBackground(Color.lightGray);
validate();
Font f=new Font("",Font.LAYOUT_LEFT_TO_RIGHT,20);
Font g=new Font("",Font.LAYOUT_LEFT_TO_RIGHT,20);
label1=new Label("按照读者编号");
label1.setBounds(100,50,170,30);
label1.setFont(f);
label2=new Label("按照姓名查询");
label2.setBounds(100,90,170,30);
label2.setFont(f);
label3=new Label("按照出生日期查询");
label3.setBounds(100,130,170,30);
label3.setFont(f);
label4=new Label("按照登记日期查询");
label4.setBounds(100,170,170,30);
label4.setFont(f);
label5=new Label("自定义查询");
label5.setBounds(100,210,170,30);
label5.setFont(f);
add(label1);
add(label2);
add(label3);
add(label4);
add(label5);
text1=new TextField(15);
text2=new TextField(15);
text3=new TextField(15);
text4=new TextField(15);
text5=new TextField(15);
text1.setBackground(Color.cyan);
text2.setBackground(Color.cyan);
text3.setBackground(Color.cyan);
text4.setBackground(Color.cyan);
text5.setBackground(Color.cyan);
text1.setFont(g);
text2.setFont(g);
text3.setFont(g);
text4.setFont(g);
text5.setFont(g);
text1.setBounds(280,50,180,30);
text2.setBounds(280,90,180,30);
text3.setBounds(280,130,180,30);
text4.setBounds(280,170,180,30);
text5.setBounds(280,210,180,30);
add(text1);
add(text2);
add(text3);
add(text4);
add(text5);
area=new TextArea(15,40);
area.setBackground(Color.cyan);
area.setBounds(100,250,360,160);
area.setEditable(false);
add(area);
button=new Button("查询");
button.setBounds(240,430,80,30);
//button.setBackground(Color.yellow);
add(button);
button.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
public void actionPerformed(ActionEvent e)
{
Connection con;
Statement sql;
ResultSet rs;
String searchStr;
if(e.getSource()==button)
{
if(text1.getText()!=null)
{
searchStr="select * from reader where rNo="+text1.getText()+";";
}
else if(text2.getText()!=null)
{
searchStr="select * from reader where rName="+text2.getText()+";";
}
else if(text3.getText()!=null)
{
searchStr="select * from reader where birDate="+text3.getText()+";";
}
else if(text4.getText()!=null)
{
searchStr="select * from reader where loDate="+text4.getText()+";";
}
else if(text5.getText()!=null)
{
searchStr="text5.getText()";
}
else
{
area.setText("不能为空值");
}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1)
{
JOptionPane.showConfirmDialog(this, "连接失败?","警告",JOptionPane.WARNING_MESSAGE);
}
try
{
con=DriverManager.getConnection("jdbc:odbc:bcfshuju","","");
sql=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//结果集的游标可以上下移动,数据库变化时,当前结果集不变,不能更新数据库
rs=sql.executeQuery("searchStr");
while(rs.next())
{
area.append("读者编号\t"+rs.getString("rNo")+"\n"
+"读者姓名\t"+rs.getString("rName")+"\n"
+"性别\t"+rs.getString("sex")+"\n"
+"出生日期\t"+rs.getString("rs.birDate")+"\n"
+"联系电话\t"+rs.getString("phone")+"\n"
+"登记日期\t"+rs.getString("loDate"));
}
con.close();
}
catch (SQLException e2)
{
JOptionPane.showConfirmDialog(this, "信息查询失败?","确认",JOptionPane.PLAIN_MESSAGE);
}
text1.setText(null);
text2.setText(null);
text3.setText(null);
text4.setText(null);
text5.setText(null);
}
}
}
class readerJieMian extends Frame implements MouseListener
{
Label label1,label2,label3;
readerJieMian()
{
setTitle("读者信息管理");
setBounds(300,150,600,450);
setBackground(Color.green);
setVisible(true);
validate();
Font f=new Font("",Font.ITALIC,30);
label1=new Label("读者信息采集");
label3=new Label("读者信息查询");
label2=new Label("读者信息删除");
label1.setFont(f);
label2.setFont(f);
label3.setFont(f);
label1.setBounds(100,100,200,40);
label2.setBounds(220,210,200,40);
label3.setBounds(340,310,200,40);
add(label1);
add(label2);
add(label3);
label1.addMouseListener(this);
label2.addMouseListener(this);
label3.addMouseListener(this);
addMouseListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
public void mouseClicked(MouseEvent e)
{
Font f=new Font("",Font.TRUETYPE_FONT,30);
label1.setFont(f);
label2.setFont(f);
label3.setFont(f);
}
public void mouseEntered(MouseEvent e)
{
if(e.getSource()==label1)
{
label1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(e.getSource()==label2)
{
label2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(e.getSource()==label3)
{
label3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else
{
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e)
{
if(e.getSource()==label1)
{
new readerAdd("读者信息采集");
}
else if(e.getSource()==label2)
{
new readerDel("读者信息删除");
}
else if(e.getSource()==label3)
{
new readerSearch("读者信息查询");
}
}
public void mouseReleased(MouseEvent e) {}
}
/*public class reader
{
public static void main(String[] args)
{
new readerJieMian();
}
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -