该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.sql.*;
public class SQL extends JFrame {
JPanel jp1,jp2,jp3,jp4;
JButton jb1,jb2,jb3;
JLabel jlb1,jlb2,jlb3;
JTextField jtf1;
JPasswordField jpw1,jpw2;
public SQL() {
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jlb1=new JLabel("用户名");
jlb2=new JLabel("密码");
jlb3=new JLabel("再次输入密码");
jtf1=new JTextField(16);
jpw1=new JPasswordField(22);
jpw2=new JPasswordField(22);
jb1=new JButton("检测");//后面的程序有问题
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String st;
st=jtf1.getText();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String url ="jdbc:mysql://localhost:3306/userdb"; //数据库操作
Connection conn= DriverManager.getConnection(url,"root","");
String str="insert into user values(?,?)";
PreparedStatement ps=conn.prepareStatement(str);
ps.setString(1, st);
Statement stmt=conn.createStatement();
String sql="select * from user";
ResultSet rs=stmt.executeQuery(sql);
String id=rs.getString("id");
if(st==id)
JOptionPane.showMessageDialog(null, "此用户名已被注册!");
//select count(*) as cont from usertable where username= *regname *
//if (rs.getInt( "cont ")> 0){
//重复
//}
} catch (SQLException e1) {
// TODO Auto-generated catch block
}
}
});
jb2=new JButton("注册");
jb2.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
String st1,st2,st3,st;
st1=jtf1.getText();
st2=jpw1.getText();//getPassword().toString().trim();
st3=jpw2.getText();//getPassword().toString().trim();
st=String.valueOf(jpw1.getPassword());
if(st2.equals(st3))
{
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String url ="jdbc:mysql://localhost:3306/userdb"; //数据库操作
Connection conn= DriverManager.getConnection(url,"root","");
String str="insert into user values(?,?)";
PreparedStatement ps=conn.prepareStatement(str);
ps.setString(1, jtf1.getText());
ps.setString(2,st);
ps.executeUpdate();
//ps.executeUpdate();
//ps.execute();
JOptionPane.showMessageDialog(null, "注册成功!");
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else{
JOptionPane.showMessageDialog(null, "请重新输入密码!");
}
}
});
jb3=new JButton("重输");
jb3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {//重置为空
jtf1.setText("");
jpw1.setText("");
jpw2.setText("");
}
});
this.setLayout(new GridLayout(4,1));
jp1.add(jlb1);
jp1.add(jtf1);
jp1.add(jb1);
jp1.setLayout(new FlowLayout(FlowLayout.RIGHT));
jp2.add(jlb2);
jp2.add(jpw1);
jp2.setLayout(new FlowLayout(FlowLayout.RIGHT));
jp3.add(jlb3);
jp3.add(jpw2);
jp3.setLayout(new FlowLayout(FlowLayout.RIGHT));
jp4.add(jb2);
jp4.add(jb3);
jp4.setLayout(new FlowLayout(FlowLayout.RIGHT));
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.setSize(350, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(200, 200);
this.setVisible(true);
pack();
}
public static void main(String[] args) {
SQL sq=new SQL();
}
}