?? useradd.java
字號:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
class Useradd extends JFrame implements ActionListener
{
JFrame f=new JFrame("用戶注冊");
JLabel lb1=new JLabel("用戶名:",Label.LEFT);
JLabel lb2=new JLabel("密碼:",Label.LEFT);
JLabel lb3=new JLabel("重復密碼:",Label.LEFT);
JButton bt1=new JButton("確定");
JButton bt2=new JButton("取消");
JTextField T1 = new JTextField(15);
JPasswordField T2 = new JPasswordField(15);
JPasswordField T3= new JPasswordField(15);
Container cp=f.getContentPane();
public Useradd(){
cp.setBackground(Color.orange);
bt1.setBackground(Color.pink);
bt2.setBackground(Color.pink);
cp.setLayout(null);
cp.add(lb1);lb1.setBounds(20,10,100,30);
cp.add(T1);T1.setBounds(120,10,100,30);
cp.add(lb2);lb2.setBounds(20,50,100,30);
cp.add(T2);T2.setBounds(120,50,100,30);
cp.add(lb3);lb3.setBounds(20,90,100,30);
cp.add(T3);T3.setBounds(120,90,100,30);
cp.add(bt1);bt1.setBounds(50,130,80,30);
cp.add(bt2);bt2.setBounds(160,130,80,30);
bt1.addActionListener(this);
bt2.addActionListener(this);
f.setBounds(400,300,300,200);
f.setVisible(true);
}
public static void main(String[] args)
{
String JDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String conURL="jdbc:odbc:Xsstudent";
try{Class.forName(JDriver);}
catch(Exception e){System.out.println("加載驅動失敗");}
Useradd df=new Useradd();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==bt2){f.setVisible(false);}
if (e.getSource()==bt1)
{ if(T1.getText().equals("")||T2.getText().equals("")||T3.getText().equals(""))
{ JOptionPane.showMessageDialog(this,"抱歉,您還有信息沒有填寫,請確認并重新填寫");}
else{
try{
Connection con=DriverManager.getConnection("jdbc:odbc:Xsstudent");
Statement s=con.createStatement();
String str="select * from user where userid= '"+T1.getText()+"'";
ResultSet rs=s.executeQuery(str);
boolean isexit=false;
try{isexit=rs.next();}
catch(Exception ee){}
if(isexit)
{
JOptionPane.showMessageDialog(this,"用戶名已存在");
T1.setText(null);}
else{
String s1=T2.getText();
String s2=T3.getText();
if (s1.equals(s2))
{
try{
Connection con1=DriverManager.getConnection("jdbc:odbc:Xsstudent");
Statement b=con1.createStatement();
String str1="insert into user values ('"+T1.getText().trim()+"','"+T2.getText().trim()+"')";
b.executeUpdate(str1);
T1.setText(null);
T2.setText(null);
T3.setText(null);
JOptionPane.showMessageDialog(this,"恭喜你注冊成功!");
b.close();
con1.close();
}catch(Exception ee)
{System.out.println("錯誤信息:"+ee.getMessage());}
}
else{
JOptionPane.showMessageDialog(this,"密碼輸入錯誤,請重新輸入密碼");
T2.setText(null);
T3.setText(null);
}
}
}
catch(Exception ee)
{System.out.println("錯誤信息:"+ee.getMessage());}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -