2019-07-03 19:52:32
本片代碼主要有Java的桌面圖形界面,Java I/O流,JDBC等等內容實現用戶登錄,在此地基礎上添加了用戶注冊。代碼相當的簡陋,但是簡單,容易理解。
數據庫使用Start SampServer實現
源碼:
加載數據庫驅動,獲取數據庫連接
package 包位置
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcRe {
static {
String driverName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/abc";
String name="root";
String passwd="";
}
public static void locadClass() throws ClassNotFoundException {
//加載驅動
String driverName="com.mysql.jdbc.Driver";
Class.forName(driverName);
}
public static Connection getConnection() throws Exception {
String url="jdbc:mysql://localhost:3306/abc";
String name="root";
String passwd="";
//獲得連接
Connection conn = DriverManager.getConnection(url, name, passwd);
return conn;
}
public static void result(Connection conn, Statement stam) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
}
if (stam != null) {
try {
stam.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stam = null;
}
}
}
注冊代碼
package 包位置
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Register extends JFrame implements ActionListener{
JPanel p1,p2,p3,p4,p5,p6,p7,p8;
JLabel l1,l2,l3,l4,l5,l6,l7,l8;
JTextField j1,j4,j5,j6,j7;
JPasswordField j2,j3;
JRadioButton r1,r2;
JCheckBox ck1,ck2,ck3,ck4;
JTextArea adddr;
JComboBox degree;
JButton bu1,bu2;
Connection conn;
Statement stam;
public Register() {
super("用戶注冊");
this.setLayout(new GridLayout(8,1));
p1 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l1 =new JLabel("用戶名:");
j1=new JTextField(18);
p1.add(l1);
p1.add(j1);
this.add(p1);
p2 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l2 =new JLabel("密碼:");
j2=new JPasswordField(19);
p2.add(l2);
p2.add(j2);
this.add(p2);
p3 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l3 =new JLabel("確認密碼:");
j3=new JPasswordField(16);
p3.add(l3);
p3.add(j3);
this.add(p3);
p4 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l4 =new JLabel("確性別:");
r1=new JRadioButton("男");
r2=new JRadioButton("女");
this.r1.setSelected(true);
p4.add(l4);
p4.add(r1);
p4.add(r2);
this.add(p4);
p5 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l5 =new JLabel("愛好:");
ck1=new JCheckBox("閱讀");
ck2=new JCheckBox("上網");
ck3=new JCheckBox("游泳");
ck4=new JCheckBox("旅游");
p5.add(l5);
p5.add(ck1);
p5.add(ck2);
p5.add(ck3);
p5.add(ck4);
this.add(p5);
p6 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l6 =new JLabel("地址:");
adddr =new JTextArea(2,19);
p6.add(l6);
p6.add(adddr);
this.add(p6);
p7 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l7 =new JLabel("學歷:");
String str[]= {"小學","初中","高中","大學"};
degree=new JComboBox(str);
p7.add(l7);
p7.add(degree);
this.add(p7);
p8 =new JPanel(new FlowLayout(FlowLayout.CENTER));
bu1=new JButton("注冊");
bu2=new JButton("取消");
p8.add(bu1);
p8.add(bu2);
this.add(p8);
//創建監聽
awtEvent();
}
public static void main(String[] args) {
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//監聽
private void awtEvent() {
//單選框互斥
r1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(r1.isSelected()){
r2.setSelected(false);
}
}
});
r2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(r2.isSelected()){
r1.setSelected(false);
}
}
});
//注冊按鈕監聽
bu1.addActionListener(this);
//取消按鈕監聽代碼
bu2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
//獲取選取的性別
String s1=null;
if(r1.isSelected()) {
s1="男";
}else {
s1="女";
}
//獲取復選框中的值
String info ="";
//通過面板屬性名獲取到該面板上的所有組件
for(Component c:p5.getComponents()){
//通過 instanceof方法篩選復選框組件
if(c instanceof JCheckBox){
//判斷復選框組件是否被選中
if(((JCheckBox) c).isSelected()){
//獲取該復選框組件信息
info += ((JCheckBox)c).getText();
}
}
}
//下拉列表框中的值
String x = degree.getSelectedItem().toString();
//判斷用戶名和密碼是否為空
judge();
//判斷用戶名是否重復
userName();
//兩次密碼是否一致
pwdName();
if(e.getSource()==bu1) {
try {
//加載數據庫驅動
conn=JdbcRe.getConnection();
//創建執行sql語句的對象
stam=conn.createStatement();
//編寫sql語句
String sql="insert into user values('"+j1.getText()+"','"+j2.getText()+"','"+j3.getText()+"','"+s1+"','"+info+"','"+adddr.getText()+"','"+x+"')";
//執行sql語句
stam.execute(sql);
//提示框
JOptionPane.showMessageDialog(null, "注冊成功!!!!!");
//關閉注冊界面窗體
this.dispose();
//打開登錄窗體
User user=new User();
user.setVisible(true);
user.setSize(600,500);
user.setLocation(600,200);
}catch (Exception e1) {
e1.printStackTrace();
}finally {
JdbcRe.result(conn, stam);
}
}
}
//判斷用戶名密碼是否為空
private void judge() {
if (j1.getText().isEmpty()) {// 判斷用戶名是否為空
this.dispose();
JOptionPane.showMessageDialog(this, "用戶名不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE);
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
if (new String(j2.getText()).isEmpty()) {// 判斷密碼是否為空
this.dispose();
JOptionPane.showMessageDialog(this, "密碼不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE);
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
if (new String(j3.getText()).isEmpty()) {// 判斷確認密碼是否為空
this.dispose();
JOptionPane.showMessageDialog(this, "確認密碼不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE);
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
}
//判斷新的用戶是否和原用戶名有重復
private void userName() {
try {
conn=JdbcRe.getConnection();
stam=(Statement)conn.createStatement();
//編寫SQL語句
String sql="select id num from User where id='"+j1.getText()+"'";
ResultSet rs=stam.executeQuery(sql);
int num=0;
if(rs!=null&&rs.next()) {
num=rs.getInt("num");
System.out.println(num);
}
//判斷用戶名是否有相同
if (num>0) {
JOptionPane.showMessageDialog(this, "用戶名已使用,請重新選取","警告消息",JOptionPane.WARNING_MESSAGE);
this.dispose();
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
//判斷相關用戶的密碼和確認密碼相同
private void pwdName() {
if (j2.equals(j3)) {
JOptionPane.showMessageDialog(this, "兩次密碼不相同","警告消息",JOptionPane.WARNING_MESSAGE);
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
}
}
用戶登錄界面代碼
package 包位置
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import chat.ChatRoomClient;
import chat.ChatRoomServer;
import cn.jhc.util.ComStr;
import cn.jhc.util.RandomYzm;
import cn.jhc.view.MainWinow;
public class User extends JFrame implements ActionListener,MouseListener{
String rYzm;
JPanel p1,p2,p3,p4;
JLabel j1,j2,j3,lyzm;
JTextField t1,inYzm;
JPasswordField t2;
JButton b,b1,yzm,b2;
JRadioButton jr1;
Connection conn;
Statement stam;
public User() {
super("用戶登錄界面");
this.setLayout(new GridLayout(4,1));
p1=new JPanel(null);
p2=new JPanel(null);
p3=new JPanel(null);
p4=new JPanel(null);
JLabel jl1=new JLabel("大傻逼登錄系統");
jl1.setFont(new Font("宋體",Font.BOLD,20));
jl1.setBounds(180,0, 170, 80);
j1=new JLabel("用戶名:");
j1.setFont(new Font("宋體",Font.BOLD,20));
j1.setBounds(90,0,110,30);
j2=new JLabel("密 碼:");
j2.setFont(new Font("宋體",Font.BOLD,20));
j2.setBounds(90,80,110,30);
lyzm =new JLabel("驗證碼:");
lyzm.setFont(new Font("宋體",Font.BOLD,20));
lyzm.setBounds(90,45,110,30);
//用於輸入驗證碼的文本框
inYzm =new JTextField();
inYzm.setFont(new Font("宋體",Font.BOLD,20));
inYzm.setBounds(190,45,190,30);
//顯示驗證碼的按鈕
yzm=new JButton();
rYzm = RandomYzm.getYzm();
yzm.setFont(new Font("宋體",Font.BOLD,20));
yzm.setBounds(400,45,130,30);
yzm.setOpaque(false);
yzm.setBorder(BorderFactory.createCompoundBorder(null,null));
yzm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
yzmAction(evt);
}
});
//用戶名輸入框
t1=new JTextField(9);
t1.setFont(new Font("宋體",Font.BOLD,20));
t1.setBounds(190,0,190,30);
//用戶密碼框
t2=new JPasswordField(9);
t2.setFont(new Font("宋體",Font.BOLD,20));
t2.setBounds(190,80,190,30);
b=new JButton("登錄");
b.setFont(new Font("宋體",Font.BOLD,20));
b.setBounds(90, 20,290, 30);
b1=new JButton("注冊");
b1.setFont(new Font("宋體",Font.BOLD,20));
b1.setBounds(400, 0, 130, 30);
b2=new JButton("修改密碼");
b2.setFont(new Font("宋體",Font.BOLD,20));
b2.setBounds(400, 80, 130, 30);
jr1=new JRadioButton("記住密碼");
jr1.setFont(new Font("宋體",Font.BOLD,20));
jr1.setBounds(400,20, 160, 30);
jr1.setSelected(false);
p1.add(jl1);
p2.add(j1);
p2.add(j2);
p2.add(b1);
p2.add(t1);
p2.add(t2);
p2.add(b2);
p3.add(lyzm);
p3.add(inYzm);
p3.add(yzm);
p4.add(b);
p4.add(jr1);
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p4);
//創建監聽
awtEvent();
//獲取相應的用戶名和密碼
addPassword();
}
private void awtEvent() {
//登錄按鍵監聽
b.addActionListener(this);
//注冊按鍵監聽
b1.addMouseListener(this);
//修改密碼監聽
b2.addActionListener(this);
}
//用於刷新驗證碼
private void yzmAction(ActionEvent evt) {
//重新獲取驗證碼
rYzm=RandomYzm.getYzm();
//設置顯示的驗證碼
yzm.setText(rYzm);
}
private void addPassword() {
try {
BufferedReader buf=new BufferedReader(new FileReader("user.txt"));
String lien = buf.readLine();
if(lien!= null) {
String[] users=lien.split(" ");
t1.setText(users[0]);
t2.setText(users[1]);
}
buf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
User user=new User();
user.setVisible(true);
user.setSize(600,500);
user.setLocation(600,200);
user.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b) {
try {
conn=JdbcRe.getConnection();
stam=(Statement)conn.createStatement();
//編寫SQL語句
String sql="select id,pwd from User where id='"+t1.getText()+"'and pwd='"+t2.getText()+"'";
//執行SQL語句
ResultSet rs=stam.executeQuery(sql);
if(rs.next()) {
String get_InYzm=inYzm.getText();
System.out.println("獲取到相應賬號");
if(ComStr.compar2Str(get_InYzm, rYzm)){
System.out.println("驗證碼正確");
if(jr1.isSelected()){
FileWriter fWriter=new FileWriter("user.txt");
fWriter.write(t1.getText());
fWriter.write(" ");
fWriter.write(t2.getText());
fWriter.close();
}
this.dispose();//關閉窗口
String name=JOptionPane.showInputDialog(this,"請輸入你的昵稱:");
String num="ww";
//記住密碼功能中用流將數據傳到username.txt中
FileWriter f=new FileWriter("username.txt");
f.write(num);
f.write(" ");
f.write(name);
f.flush();
JOptionPane.showMessageDialog(null, "登錄成功!");
}else {
JOptionPane.showMessageDialog(this, "驗證碼錯誤");
inYzm.setText("");
}
}else {
JOptionPane.showMessageDialog(null, "密碼或賬號錯誤,登錄失敗!","標題",JOptionPane.ERROR_MESSAGE);
t1.setText("");
t2.setText("");
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
if(e.getSource()==b2) {
this.dispose();
UserTrue userTrue=new UserTrue();
userTrue.setVisible(true);
userTrue.setSize(370,300);
userTrue.setLocation(300,300);
}
}
@Override
public void mouseClicked(MouseEvent e) {
this.dispose();
Register register=new Register();
register.setVisible(true);
register.setSize(300,300);
register.setLocation(100,100);
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
用戶是否存在驗證
package 包位置;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class UserTrue extends JFrame implements ActionListener{
JPanel p1;
JLabel l1;
JTextField j1;
JPanel p2;
JLabel l2;
JPasswordField j2;
JPanel p3;
JLabel l3;
JPasswordField j3;
JPanel p4;
JButton bu1;
Connection conn;
Statement stam;
public UserTrue() {
super("用戶驗證");
this.setLayout(new GridLayout(4,1));
p1 =new JPanel(null);
l1 =new JLabel("用 戶 名:");
l1.setFont(new Font("宋體",Font.BOLD,20));
l1.setBounds(50, 20, 120, 30);
j1=new JTextField(10);
j1.setFont(new Font("宋體",Font.BOLD,20));
j1.setBounds(160, 20, 130, 30);
p1.add(l1);
p1.add(j1);
this.add(p1);
p2 =new JPanel(null);
l2 =new JLabel("密 碼:");
l2.setFont(new Font("宋體",Font.BOLD,20));
l2.setBounds(50, 20, 120, 30);
j2=new JPasswordField(10);
j2.setFont(new Font("宋體",Font.BOLD,20));
j2.setBounds(160, 20, 130, 30);
p2.add(l2);
p2.add(j2);
this.add(p2);
p3 =new JPanel(null);
l3 =new JLabel("確認密碼:");
l3.setFont(new Font("宋體",Font.BOLD,20));
l3.setBounds(50, 20, 120, 30);
j3=new JPasswordField(10);
j3.setFont(new Font("宋體",Font.BOLD,20));
j3.setBounds(160, 20, 130, 30);
p3.add(l3);
p3.add(j3);
this.add(p3);
p4 =new JPanel(null);
bu1=new JButton("確認");
bu1.addActionListener(this);
bu1.setBounds(100, 20, 120,30);
p4.add(bu1);
this.add(p4);
}
public static void main(String[] args) {
UserTrue userTrue=new UserTrue();
userTrue.setVisible(true);
userTrue.setSize(370,300);
userTrue.setLocation(300,300);
userTrue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bu1) {
try {
//加載驅動
conn=JdbcRe.getConnection();
//創建sql語句對象
stam=conn.createStatement();
//創建sql語句
String sql="select id,pwd,loginName from user where id='"+j1.getText()+"' and pwd='"+j2.getText()+"' and loginName='"+j3.getText()+"'";
//執行sql語句
ResultSet rs=stam.executeQuery(sql);
if(rs.next()) {
this.dispose();
UserModi userTrue=new UserModi();
userTrue.setVisible(true);
userTrue.setSize(370,300);
userTrue.setLocation(300,300);
userTrue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}else {
j1.setText("");
j2.setText("");
j3.setText("");
JOptionPane.showMessageDialog(this, "用戶名或密碼不存在");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
FileWriter fileWriter=new FileWriter("nameExit.txt");
fileWriter.write(j1.getText());
fileWriter.flush();
fileWriter.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
用戶修改密碼代碼
Package包位置
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class UserModi extends JFrame implements ActionListener{
JPanel p1,p2,p3,p4,p5;
JLabel l1,l2,l3;
JTextField j1;
JPasswordField j2,j3;
JButton bu1,bu2;
Connection conn;
Statement stam;
public UserModi() {
super("用戶修改");
this.setLayout(new GridLayout(4,1));
p1 =new JPanel(null);
l1 =new JLabel("用 戶 名:");
l1.setFont(new Font("宋體",Font.BOLD,20));
l1.setBounds(50, 20, 120, 30);
j1=new JTextField();
//j1=new JLabel("12");
j1.setFont(new Font("宋體",Font.BOLD,20));
j1.setBounds(160, 20, 130, 30);
//j1.setEditable(false);
p1.add(l1);
p1.add(j1);
this.add(p1);
try {
BufferedReader bufferedReader=new BufferedReader(new FileReader("nameExit.txt"));
String lenString=bufferedReader.readLine();
if (lenString!=null) {
String[] aaStrings=lenString.split(" ");
j1.setText(aaStrings[0]);
}
// bufferedReader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p2 =new JPanel(null);
l2 =new JLabel("密 碼:");
l2.setFont(new Font("宋體",Font.BOLD,20));
l2.setBounds(50, 20, 120, 30);
j2=new JPasswordField(10);
j2.setFont(new Font("宋體",Font.BOLD,20));
j2.setBounds(160, 20, 130, 30);
p2.add(l2);
p2.add(j2);
this.add(p2);
p3 =new JPanel(null);
l3 =new JLabel("確認密碼:");
l3.setFont(new Font("宋體",Font.BOLD,20));
l3.setBounds(50, 20, 120, 30);
j3=new JPasswordField(10);
j3.setFont(new Font("宋體",Font.BOLD,20));
j3.setBounds(160, 20, 130, 30);
p3.add(l3);
p3.add(j3);
this.add(p3);
p4 =new JPanel(null);
bu1=new JButton("確認");
bu1.addActionListener(this);
bu1.setBounds(50, 20, 120,30);
p4.add(bu1);
bu2=new JButton("退出");
bu2.setBounds(170, 20, 120,30);
bu2.addActionListener(this);
p4.add(bu2);
this.add(p4);
}
public static void main(String[] args) {
UserModi userTrue=new UserModi();
userTrue.setVisible(true);
userTrue.setSize(370,300);
userTrue.setLocation(300,300);
userTrue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bu1) {
try {
//加載驅動
conn=JdbcRe.getConnection();
//創建sql語句對象
stam=conn.createStatement();
//創建sql語句
String sql="update user set pwd=('"+j2.getText()+"') , loginName=('"+j3.getText()+"') where id=('"+j1.getText()+"')";
//執行sql語句
stam.execute(sql);
JOptionPane.showMessageDialog(null, "修改成功!!!!!");
//關閉修改窗體
this.dispose();
//打開登錄窗體
User user=new User();
user.setVisible(true);
user.setSize(600,500);
user.setLocation(600,200);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (e.getSource()==bu2) {
j1.setText("");
this.dispose();
}
}
}
驗證碼判斷
Package 包位置
/**
* 實現不區分大小寫比較兩字符串是否相等
* 輸出為true false
* @author Lenovo
*
*/
public class ComStr {
public static boolean compar2Str(String str1,String str2) {
char[] ch1=str1.toCharArray();//將字符串str1轉換為字符數組
char[] ch2=str2.toCharArray();//將字符串str2轉換為字符數組
String newStr1="";//聲明轉換為小寫的字符串str1
String newStr2="";//聲明轉換為小寫的字符串str1
for (int i = 0; i < ch1.length; i++) {
if(ch1[i]>=65&&ch1[i]<=90) {//如果字符串為大寫就將其轉換為小寫
ch1[i]+=32;
}
newStr1 +=ch1[i];
}
for (int i = 0; i < ch2.length; i++) {
if(ch2[i]>=65&&ch2[i]<=90) {
ch2[i]+=32;
}
newStr2 +=ch2[i];
}
//如果兩相等將輸出true
if(newStr2.equals(newStr1)) {
return true;
}
return false;
}
}
驗證碼隨機
Package 包位置
/**
* 用於隨機產生長度為4的可變字符串
*/
import java.util.Random;
public class RandomYzm {
public static String getYzm() {
//定義長度為4
int lenght=4;
//35個隨機數
String untes="1,2,3,4,5,6,7,8,9,q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m";
//使用字符串將隨機數分割開
String unte[]=untes.split(",");
//對隨機數的對象進行實例化
Random rd=new Random();
//設置用於存儲產生的字符串
StringBuffer buf=new StringBuffer();
for (int i = 0; i < lenght; i++) {
buf.append(unte[rd.nextInt(35)]);
}
return buf.toString();
}
}