📄 menu0_1.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.sql.*;
class Menu0_1 extends JFrame implements ActionListener//JFrame implements ActionListener//
{
//-------------------界面控件元素-----------------------------
JLabel lbl1=new JLabel("用户名");
JLabel lbl2=new JLabel("密码");
JLabel lbl3=new JLabel("重复密码");
JTextField txt1=new JTextField(10);
JTextField txt2=new JTextField(10);
JTextField txt3=new JTextField(10);
JTextField txt4 = new JTextField(10);
JButton btn1=new JButton("添加");
JButton btn2=new JButton("更新");
JButton btn3=new JButton("删除");
JButton btn4=new JButton("退出");
JButton btn5=new JButton("查询");
String col[]={"用户名","密码"};
String rows[][]=new String[20][2];
JTable tb;
JScrollPane jsp;
JTabbedPane tab=new JTabbedPane();
public Menu0_1() //构造函数
{
init();
this.setTitle("图书管理员管理");
//-------------------添加监听者-----------------------------
//btn2.setEnabled(false);//-------------先注掉更新按钮
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
}
public void init()
{
//-------------------Tab1-----------------------------
JPanel jp1=new JPanel();
jp1.setLayout(new GridLayout(2,2,10,10));
jp1.add(lbl1);jp1.add(txt1);
jp1.add(lbl2);jp1.add(txt2);
jp1.add(lbl3);jp1.add(txt3);
JPanel jp2=new JPanel();
jp2.add(btn1);jp2.add(btn4);
JPanel jp3=new JPanel();
jp3.setLayout(new BorderLayout());
jp3.add(jp1,BorderLayout.CENTER);
jp3.add(jp2,BorderLayout.SOUTH);
jp1.setLayout(new GridLayout(3,2));
//-------------------Tab2-----------------------------
JPanel jp4=new JPanel();
jp4.add(btn2);jp4.add(btn3);
JPanel jp7=new JPanel();
JLabel lbl4 = new JLabel("用户名");
jp7.add(lbl4);jp7.add(txt4);jp7.add(btn5);
tb=new JTable(rows,col);
jsp=new JScrollPane(tb);
JPanel jp8=new JPanel();
jp8.setLayout(new BorderLayout());
jp8.add(jp7,BorderLayout.NORTH);
jp8.add(jsp,BorderLayout.CENTER);
jp8.add(jp4,BorderLayout.SOUTH);
//-------------------放置Tab-----------------------------
tab.add(jp3,"添");tab.add(jp8,"查删改");
this.getContentPane().add(tab);
this.setLocation(200,200);
this.setSize(400,300);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
add_BookType();
if(e.getSource()==btn2)
update_BookType();
if(e.getSource()==btn3)
delete_BookType();
if(e.getSource()==btn4)
this.dispose();
if(e.getSource()==btn5)
search_BookType();
}
public void add_BookType() //添加书籍分类方法
{
try{// 储存
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:yyp","","");
Statement cmd=con.createStatement();
cmd.executeUpdate("insert into login(adName,adPwd) values('"+txt1.getText()+"',"+"'"+txt2.getText()+"')");
JOptionPane.showMessageDialog(this,"储存成功");
}
catch(Exception ex)
{
System.out.println(ex);
}
}
private void update_BookType() //更新书籍分类方法
{
try{
int flag=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:yyp","","");
Statement cmd=con.createStatement();
ResultSet rs=cmd.executeQuery("select * from login");
while(rs.next())
{
rows[flag][0]=rs.getString(1);//姓名
rows[flag][1]=rs.getString(2);//性别
flag++;
}tb.setModel(new DefaultTableModel(rows,col));
cmd.close();
con.close();
}catch(Exception ex)
{
System.out.println(ex);
}
}
private void delete_BookType() //删除书籍分类方法
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:yyp","","");
Statement cmd=con.createStatement();
cmd.executeUpdate("delete from login where adName='"+txt4.getText()+"'");
JOptionPane.showMessageDialog(this,"删除成功");
ResultSet rs=cmd.executeQuery("select * from login");
int flag1=0;
String rows1[][]=new String[20][2];
while(rs.next())
{
rows1[flag1][0]=rs.getString(1);//姓名
rows1[flag1][1]=rs.getString(2);//性别
flag1++;
tb.setModel(new DefaultTableModel(rows,col));
}
for(int i=0;i<=flag1;i++)
{rows[i][0]=rows1[i][0];
rows[i][1]=rows1[i][1];
}
}catch( Exception ex)
{
System.out.println(ex);
}
}
public void search_BookType()
{
try{//查询
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:yyp","","");
Statement cmd=con.createStatement();
ResultSet rs=cmd.executeQuery("select * from login");
while(rs.next())
{
String st1=rs.getString(1);
if(st1.equals(txt4.getText()))
{
JOptionPane.showMessageDialog(this,"该用户密码为:"+rs.getString(2));
}
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public static void main(String arg[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
Menu0_1 frm=new Menu0_1();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -