📄 userinfodeleframe.java
字号:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.sql.*;
public class UserInfoDeleFrame extends JFrame implements ActionListener
{
private Container c;
private JPanel panel1,panel2;
private JLabel nameLabel,passLabel;
private JButton ok,cancel;
private JPasswordField passText;
private JTextField nameText;
private int findFlag=0;
public UserInfoDeleFrame()
{
super("删除用户");
c=getContentPane();
c.setLayout(new BorderLayout());
nameLabel=new JLabel("用户名",JLabel.CENTER);
passLabel=new JLabel("密码",JLabel.CENTER);
nameText=new JTextField(10);
passText=new JPasswordField(12);
ok=new JButton("确定");
cancel=new JButton("取消");
panel1=new JPanel();
panel1.setLayout(new GridLayout(2,2));
panel1.add(nameLabel);
panel1.add(nameText);
panel1.add(passLabel);
panel1.add(passText);
panel2=new JPanel();
panel2.add(ok);
panel2.add(cancel);
ok.addActionListener(this);
cancel.addActionListener(this);
c.add(panel1,BorderLayout.CENTER);
c.add(panel2,BorderLayout.SOUTH);
setSize(300,100);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String userName1,userPass1;
userName1=nameText.getText();
userPass1=new String(passText.getPassword());
//连接数据库
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1)
{
System.out.println(e1.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:bookbase");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select userName from regist");
while(rs.next())
{
if(nameText.getText().equals(rs.getString("userName")))
{
findFlag=1;
break;
}
}
stmt.close();
con.close();
}
catch(SQLException ex)
{
System.out.println("SQLException:"+ex.getMessage());
}
//连接数据库结束
if(e.getSource()==ok)
{
if(findFlag==1)
{
if(userName1.equals(""))
JOptionPane.showMessageDialog(null,"用户名不能为空");
else if(userPass1.equals(""))
JOptionPane.showMessageDialog(null,"用户密码不能为空");
if((userName1.equals(""))&&(userPass1.equals("")))
JOptionPane.showMessageDialog(null,"信息不能为空");
//连接数据库
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1)
{
System.out.println(e1.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:bookbase");
Statement stmt=con.createStatement();
PreparedStatement pstmt=con.prepareStatement("delete * from regist where userName=? and userPass=?");
pstmt.setString(1,userName1);
pstmt.setString(2,userPass1);
pstmt.executeUpdate();
stmt.close();
con.close();
nameText.setText("");
passText.setText("");
}
catch(SQLException ex)
{
System.out.println("SQLException:"+ex.getMessage());
}
//数据库连接结束
}
else if(findFlag==0)
{
JOptionPane.showMessageDialog(null,"用户帐号不存在!");
passText.setText("");
nameText.setText("");
}
}
else if(e.getSource()==cancel)
{
this.dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -