黑马程序员_客户端聊天软件(网络编程、GUI、线程等)

------------------------- android培训java培训、期待与您交流! -------------------------

客户端聊天软件

功能介绍:

该产品的开发环境为:MyEclipse,既有客户端也有服务器且与数据库相关联(MySQL),需要验证用户名及密码才能登录聊天且不能重复登录,用户可以注册以及更改自己的密码 ,还有一个管理员,其权限为所有权限,这里可以删除用户,客户端界面可以由用户自己更换背景颜色和字体颜色,还可选择公聊或是私聊及聊天对象。

应用技术:

务器Socket、客户端Socket、GUI、线程、监听器及抽象适配器、JFrame的多种组件、关联数据库并实现注册和修改密码以及删除、各种验证、if判断及多种循环、添加图片、更换图标。

---图片样式---


---代码详解---

服务器端代码:

Serverthread类

package util;


import java.io.*;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.Vector;


public class Serverthread implements Runnable {


private String userName;//用户昵称
    Socket sc; // 客户端Socket变量
    Vector vc; // 所有用户线程对象集合
    DataInputStream input;
    DataOutputStream output;
    
    public Serverthread(){};
    
    public Serverthread(Socket sc,Vector vector) {
    System.out.println("连接服务器");
    this.sc=sc;//接受客户端传递过来的参数
        this.vc=vector;//接受传递过来的线程
        System.out.println("用户名是:"+userName);
        try{
        /*利用客户端sc获得客户端输入流对象*/


input=new DataInputStream(sc.getInputStream());
/*利用客户端sc获得客户端输出流对象*/
output=new DataOutputStream(sc.getOutputStream());

            boolean Flag1=true;
            while(Flag1){
            userName=input.readUTF();
            System.out.println("得到的用户名:"+userName);
                boolean Flag=false;
                StringTokenizer strtk=new StringTokenizer(Serverchat.allNames,";");
                while(strtk.hasMoreTokens()){
                    String tempName=strtk.nextToken();
                    if(userName.equals(tempName)){
                        output.writeUTF("*");
                        System.out.println("用户名不可用");
                        Flag=true;
                        break;
                    } }
                if(!Flag){
                    output.writeUTF("#");
                    System.out.println("用户名可用");
                    break;
                }
                if(userName.equals("")){
                    break;}
                }
            }
        catch(Exception e){e.printStackTrace();} 
       }


public void write(String msg){
        synchronized(output){// 同步对象
            try{
            output.writeUTF(msg);
            }catch(Exception e){


            }


        }
    }

    public String getUserName(){
    this.userName=userName;
    return userName;
    }
    
public void run(){


       try{
           while(true){


               String ss=input.readUTF();
               if(ss.equals("1")){
               
                try {
            for(int i=1;i<vc.size();i++){
                Serverthread str=(Serverthread)vc.get(i);
                //output.writeUTF(this.getUserName()+"退出了!");
                str.write(ss+this.getUserName());
                }
            vc.remove(this.sc);
            Serverchat.allNames = Serverchat.allNames.substring(0,Serverchat.allNames.indexOf(this.getUserName())-1)
            +Serverchat.allNames.substring(Serverchat.allNames.indexOf(this.getUserName())+this.getUserName().length());
            } catch (Exception e) {
            e.printStackTrace();
            }
                //exit();
               }else if(ss.charAt(0)=='3'){
               
                for(int i=1;i<vc.size();i++){
                Serverthread str=(Serverthread)vc.get(i);
                str.write(ss);
                }
               }else if(ss.charAt(0)=='4'){
               
                for(int i=1;i<vc.size();i++){
                Serverthread str=(Serverthread)vc.get(i);
                if(str.getUserName().equals(ss.substring(ss.indexOf("<")+1,ss.indexOf(">")))){
                str.write(ss);
                break;
                }
                }
               }

       }
     
   }catch(Exception e){

   }

}

}

Serverchat类

package util;


import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;


public class Serverchat {


/**
* @param args
*/
public static String allNames="所有人;";
public static Vector vcthreads = new Vector();
    public Serverchat() {
   
    }


public static void main(String[] args) {
// TODO Auto-generated method stub
ServerSocket ss=null;

try {

System.out.println("等待连接。。。");
ss=new ServerSocket(8887);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


try {
while(true){
Socket sc=ss.accept();//有用户呼叫就建立一个客户端socket对象

   Serverthread st=new Serverthread(sc,vcthreads);
  
       if(st.getUserName().equals("")){
           continue;
        }
       vcthreads.addElement(st);
    new Thread(st).start();
    allNames=allNames+st.getUserName()+";";
    for(int i=0;i<vcthreads.size();i++){
         Serverthread st1=(Serverthread)vcthreads.get(i);
        st1.write("0"+"欢迎用户 "+st.getUserName()+" 登陆聊天室!"+allNames);
    }
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}



客户端代码

ConnectionManager类

package util;
import java.sql.*;

public final class ConnectionManager {


private static final String DRIVERCLASS="com.mysql.jdbc.Driver";
private static final String URL="jdbc:mysql://127.0.0.1:3306/huang";
private static final String USER="root";
private static final String PWD="root";

private static Connection conn;

public static Connection getConnectionobj(){
try {

Class.forName(DRIVERCLASS);
conn=DriverManager.getConnection(URL,USER,PWD);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}

public static void closeDBconn(Connection conn){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void closeDBconn(Statement state){
try {
state.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void closeDBconn(ResultSet rs){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Login类

package util;


import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Vector;


import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;




public class Login extends JFrame {


JTextField t1,t2,t3,t4;
JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,lshu,l11,lreturn;
JButton b1,b2,b3,b4;
Font font=new Font("宋体",1,12);
Font font8=new Font("宋体",1,15);
Vector v=new UserregDAO().getAllUser();
static String str1,str2,str3;
JPanel jp=(JPanel)this.getContentPane();
public Login(){

init();
}

private void init() {
// TODO Auto-generated method stub

SwingUtilities.invokeLater(new Runnable(){

public void run() {
try{
// TODO Auto-generated method stub
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (Exception exception) {
// TODO: handle exception
}
}

});


Toolkit tk=Toolkit.getDefaultToolkit();
Image image=tk.createImage("图标.jpg"); 
this.setIconImage(image);
//this.setBounds(200, 180, 600, 270);
this.setBounds(200, 180, 350, 270);
this.setVisible(true);
//JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(null);
jp.setBackground(new Color(200,210,100));
this.setTitle(" 用户登录");
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(3);
//Font font=new Font("宋体",1,12);

 ImageIcon image1 = new ImageIcon("biaoti2.jpg");
 //l1=new JLabel("2 0 1 1 客 户 端 聊 天 软 件");
 //l1.setBounds(55, 20, 300, 40);
 //l1.setFont(font8);
 l1=new JLabel(image1);
 //l1.setBounds(0, 1, 345, 65);
 l1.setBounds(0, 0, 600, 65);
 jp.add(l1);
 
 ImageIcon image2 = new ImageIcon("长竖.jpg");
 lshu=new JLabel(image2);
 lshu.setBounds(355, 1, 5, 260);
 jp.add(lshu);
 
 l2=new JLabel("用户名:");
 l2.setBounds(40, 90, 60, 20);
 l2.setFont(font);
 jp.add(l2);
 
 t1=new JTextField(15);
 t1.setBounds(100, 90, 120, 20);
 jp.add(t1);
 
 l4=new JLabel("注 册 用 户");
 l4.setForeground(Color.blue);
 l4.setBounds(240, 85, 80, 20);
 l4.setFont(font);
 jp.add(l4);
 l4.addMouseListener(new MouseAdapter(){


public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
l4.setForeground(Color.red);
}

public void mouseExited(MouseEvent arg0){
l4.setForeground(Color.blue);
}

public void mousePressed(MouseEvent arg0){
new Insert();
exit();
}


 });


 l3=new JLabel("密  码:");
 l3.setBounds(40, 130, 60, 20);
 l3.setFont(font);
 jp.add(l3);
 
 t2=new JPasswordField(15);
 t2.setBounds(100, 130, 120, 20);
 jp.add(t2);
 
 l5=new JLabel("修 改 密 码");
 l5.setForeground(Color.blue);
 l5.setBounds(240, 110, 80, 20);
 l5.setFont(font);
 jp.add(l5);
 l5.addMouseListener(new MouseAdapter(){


public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
l5.setForeground(Color.red);
}
public void mouseExited(MouseEvent arg0){
l5.setForeground(Color.blue);
}
public void mousePressed(MouseEvent arg0){
new Update();
exit();


}
 });


 l6=new JLabel();
 l6.setForeground(Color.red);
 l6.setVisible(false);
 l6.setBounds(60, 210, 260, 20);
 l6.setFont(font);
 jp.add(l6);
 l6.addMouseListener(new MouseAdapter(){


public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
l6.setVisible(false);
}
 });
 
 b1=new JButton("登   录");
 b1.setForeground(Color.black);
 b1.setBounds(70, 180, 80, 20);
 jp.add(b1);
 
 b1.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(t1.getText()==null||t1.getText().equals("")||t2.getText()==null||t2.getText().equals("")){
l6.setText("用 户 名 或 密 码 不 能 为 空 !!!");
l6.setVisible(true);
return;
}else{
UserregDAO dao=new UserregDAO();
boolean bool=dao.selectlogin(t1.getText(),t2.getText());
if(bool){
str3=t1.getText();
exit();
// new UserMain();
Userchat chat=new Userchat();
new Thread(chat).start();
}else{
l6.setText("用 户 名 或 密 码 错 误 !!!");
l6.setVisible(true);
}
}
}
 });
 
 b2=new JButton("退   出");
 b2.setForeground(Color.black);
 b2.setBounds(170, 180, 80, 20);
 jp.add(b2);
 b2.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
//exit();
}
 });
 
 l11=new JLabel("管 理 员登录");
 l11.setForeground(Color.blue);
 //l7.setBounds(410, 20, 200, 40);
 l11.setBounds(237,135, 120, 20);
 l11.setFont(font);
 jp.add(l11);
 l11.addMouseListener(new MouseAdapter(){


public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
l11.setForeground(Color.red);
}
public void mouseExited(MouseEvent arg0){
l11.setForeground(Color.blue);
}
public void mousePressed(MouseEvent arg0){
thissize1();
}
 });
 
this.validate();

}

public void exit(){
  this.dispose();
}

public void thissize1(){
 this.setBounds(200, 180, 600, 270);

 l8=new JLabel("管理员:");
 l8.setBounds(390, 90, 60, 20);
 l8.setFont(font);
 jp.add(l8);
 
 t3=new JTextField(15);
 t3.setBounds(450, 90, 120, 20);
 jp.add(t3);
 
 l9=new JLabel("密  码:");
 l9.setBounds(390, 130, 60, 20);
 l9.setFont(font);
 jp.add(l9);
 
 t4=new JPasswordField(15);
 t4.setBounds(450, 130, 120, 20);
 jp.add(t4);
 
 b3=new JButton("登  录");
 b3.setForeground(Color.black);
 b3.setBounds(405, 180, 70, 20);
 jp.add(b3);
 b3.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(t3.getText()==null||t3.getText().equals("")||t4.getText()==null||t4.getText().equals("")){
l10.setText("不能有空值!!!");
l10.setVisible(true);
}else{
if(t3.getText()=="huanglibo"||t3.getText().equals("huanglibo")&&t4.getText()=="616424735"||t4.getText().equals("616424735")){
new UserData();
exit();
}else{
l10.setText("管理员密码错误");
l10.setVisible(true);
//return;
}
}
}
 
 });
 
 b4=new JButton("退  出");
 b4.setForeground(Color.black);
 b4.setBounds(495, 180, 70, 20);
 jp.add(b4);
 b4.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
//exit();
}
 });


 lreturn=new JLabel("《《《");
 lreturn.setForeground(Color.blue);
 lreturn.setBounds(370, 210, 50, 30);
 jp.add(lreturn);
 lreturn.addMouseListener(new MouseAdapter(){


 public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
 lreturn.setForeground(Color.red);
}
public void mouseExited(MouseEvent arg0){
lreturn.setForeground(Color.blue);
}
 
 public void mousePressed(MouseEvent arg0){
thissize2();
}
 });
 
 l10=new JLabel();
 l10.setForeground(Color.red);
 l10.setVisible(false);
 l10.setBounds(450, 210, 250, 20);
 l10.setFont(font);
 jp.add(l10);
 l10.addMouseListener(new MouseAdapter(){


public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
l10.setVisible(false);
}
 });
 this.validate();
}

public void thissize2(){
this.setBounds(200, 180, 350, 270);
}

}


Insert

package util;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class Insert extends JFrame{


private JLabel l0,l1,l2,l3,l4,l5,l6,l7;
private JTextField t1,t2,t3;
private JButton b1,b2,b3;
Font font=new Font("宋体",1,24);
Font font1=new Font("宋体",1,25);

public Insert(){
init();
}


public void init() {
// TODO Auto-generated method stub

SwingUtilities.invokeLater(new Runnable(){

public void run() {
try{
// TODO Auto-generated method stub
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (Exception exception) {
// TODO: handle exception
}
}

});

Toolkit tk=Toolkit.getDefaultToolkit();
Image image=tk.createImage("图标.jpg"); 
this.setIconImage(image);
this.setVisible(true);
this.setBounds(300, 100, 470, 300);
this.setTitle(" 注册用户");
this.setResizable(false);
JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(null);
jp.setBackground(new Color(200,210,100));
this.setLayout(null);

l0=new JLabel("注  册  用  户");
l0.setFont(font);
l0.setBounds(130, 10, 200, 40);
jp.add(l0);

l1=new JLabel("用户名称:");
l1.setBounds(20, 70, 80, 20);
jp.add(l1);
t1=new JTextField(15);
t1.setBounds(90, 70, 100, 20);
jp.add(t1);
l4=new JLabel("由数字和字母组成且长度为:6--15字符");
l4.setForeground(Color.blue);
l4.setBounds(200, 70, 300, 20);
jp.add(l4);

l2=new JLabel("用户密码:");
jp.add(l2);
l2.setBounds(20, 110, 80, 20);
t2=new JPasswordField(15);
t2.setBounds(90, 110, 100, 20);
jp.add(t2);
l5=new JLabel("密码由数字和字母组成且长度为:6--15字符");
l5.setForeground(Color.blue);
l5.setBounds(200, 110, 300, 20);
jp.add(l5);

l3=new JLabel("重复密码:");
l3.setBounds(20, 150, 80, 20);
jp.add(l3);
t3=new JPasswordField(15);
t3.setBounds(90, 150, 100, 20);
jp.add(t3);
l6=new JLabel("两次密码需相同");
l6.setBounds(200, 150, 300, 20);
l6.setForeground(Color.blue);
jp.add(l6);

b1=new JButton("注   册");
b1.setBounds(40, 190, 80, 20);
jp.add(b1);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(t1.getText()==null||t1.getText().equals("")||
  t2.getText()==null||t2.getText().equals("")||
  t3.getText()==null||t3.getText().equals("")){
l7.setText("不允许有空值!!!");
l7.setVisible(true);
return;
}else if(t1.getText().length()<6||t1.getText().length()>15){
l7.setText("用户名长度不对,请验证。。。");
l7.setVisible(true);
return;
}else if(t2.getText().length()<6||t2.getText().length()>15){
l7.setText("第一次密码长度不对,请验证。。。");
l7.setVisible(true);
return;
}else if(t3.getText().length()<6||t3.getText().length()>15){
l7.setText("第二次密码长度不对,请验证。。。");
l7.setVisible(true);
return;
}else if(!t2.getText().equals(t3.getText())){
l7.setText("两次密码不对,请核对!!!");
l7.setVisible(true);
System.out.println("123");
return;
}else{

if(validate(t1.getText())&&validate(t2.getText())){
System.out.println("123456");
UserregDAO dao=new UserregDAO();
  boolean bool=dao.validateReg(t1.getText());
  if(!bool){
  l7.setText("此用户名已被注册,请更换!!!");
l7.setVisible(true);
return;
  }else{
  UserregDAO dao2=new UserregDAO();
  int info=dao2.insert(t1.getText(),t2.getText());
  if(info==1){
  l7.setText("恭喜您,注册成功");
l7.setVisible(true);
  }else{
  l7.setText("对不起,系统异常,稍后重试");
l7.setVisible(true);
  }
  }
}else{
l7.setText("用户名或密码格式有误请验证。。。");
l7.setVisible(true);
}


     }
}
});

b2=new JButton("退   出");
b2.setBounds(160, 190, 80, 20);
jp.add(b2);
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
exit();
new Login();
//System.exit(0);
}
});

b3=new JButton("重   置");
b3.setBounds(280, 190, 80, 20);
jp.add(b3);
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
t1.setText("");
t2.setText("");
t3.setText("");
}
});

l7=new JLabel("不允许有空值!!!");
l7.setBounds(30, 225, 420, 30);
l7.setForeground(Color.red);
l7.setFont(font1);
l7.setVisible(false);
l7.addMouseListener(new MouseAdapter(){


public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
l7.setVisible(false);
}


 });
jp.add(l7);

this.validate();
}

public void exit(){
this.dispose();
}

public boolean validate(String str){

char cha1[]=str.toCharArray();//将给定的字符串转换成Char数组

for(int i=0;i<str.length();i++){//用for循环遍历数组
char cha=cha1[i];
if(Character.isDigit(cha)||Character.isLetter(cha)){//判断是否为数字或字母

}else{

return false;
}
}
return true;
}

}


Update类

package util;


import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Update extends JFrame{


private JLabel l0,l1,l2,l3,l4,l5,l6,l7,l8,l9;
private JTextField t1,t2,t3,t4;
public static JButton b1,b2,b3;
Font font=new Font("宋体",1,24);
Font font1=new Font("宋体",1,25);

public Update(){
init();
}


private void init() {
// TODO Auto-generated method stub

SwingUtilities.invokeLater(new Runnable(){

public void run() {
try{
// TODO Auto-generated method stub
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (Exception exception) {
// TODO: handle exception
}
}

});

Toolkit tk=Toolkit.getDefaultToolkit();
Image image=tk.createImage("图标.jpg"); /*image.gif是你的图标*/ 
this.setIconImage(image);
this.setVisible(true);
this.setBounds(300, 100, 430, 350);
this.setTitle(" 修改密码");
this.setResizable(false);
//this.setDefaultCloseOperation(3);
JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(null);
jp.setBackground(new Color(200,210,100));
this.setLayout(null);

l0=new JLabel("修 改 密 码");
l0.setFont(font);
l0.setBounds(130, 10, 200, 40);
jp.add(l0);

l1=new JLabel("用户名:");
l1.setBounds(20, 70, 80, 20);
jp.add(l1);
t1=new JTextField(15);
t1.setBounds(90, 70, 100, 20);
jp.add(t1);
l4=new JLabel("您必须已经注册");
l4.setForeground(Color.blue);
l4.setBounds(200, 70, 300, 20);
jp.add(l4);

l2=new JLabel("原密码:");
jp.add(l2);
l2.setBounds(20, 110, 80, 20);
t2=new JPasswordField(15);
t2.setBounds(90, 110, 100, 20);
jp.add(t2);
l5=new JLabel("与该用户名相对应的密码");
l5.setForeground(Color.blue);
l5.setBounds(200, 110, 300, 20);
jp.add(l5);

l3=new JLabel("新密码:");
l3.setBounds(20, 150, 80, 20);
jp.add(l3);
t3=new JPasswordField(15);
t3.setBounds(90, 150, 100, 20);
jp.add(t3);
l6=new JLabel("由数字或字母组成且长度为:6--15字符");
l6.setBounds(200, 150, 300, 20);
l6.setForeground(Color.blue);
jp.add(l6);

l9=new JLabel("重复密码:");
l9.setBounds(10, 190, 300, 20);
jp.add(l9);
t4=new JPasswordField(15);
t4.setBounds(90, 190, 100, 20);
jp.add(t4);
l8=new JLabel("两次密码需相同");
l8.setBounds(200, 190, 300, 20);
l8.setForeground(Color.blue);
jp.add(l8);

b1=new JButton("修   改");
b1.setBounds(40, 240, 80, 20);
jp.add(b1);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(t1.getText()==null||t1.getText().equals("")||
  t2.getText()==null||t2.getText().equals("")||
  t3.getText()==null||t3.getText().equals("")||
  t4.getText()==null||t4.getText().equals("")){
l7.setText("不能有空值!!!");
l7.setVisible(true);
//return;
}else if(t3.getText().length()<6||t3.getText().length()>15){
l7.setText("新密码长度不对,请验证。。。");
l7.setVisible(true);
//return;
}else if(t4.getText().length()<6||t4.getText().length()>15){
l7.setText("重复密码长度不对,请验证。。。");
l7.setVisible(true);
//return;
}else if(t3.getText().equals(t4.getText())){


if(validate2(t3.getText())){
System.out.println("123456");
UserregDAO dao=new UserregDAO();
  boolean bool=dao.validateUpdate(t1.getText(),t2.getText());
  if(!bool)
  {
  l7.setText("此用户不存在或密码错误!!!");
l7.setVisible(true);
//return;
  }
  else{
  UserregDAO dao2=new UserregDAO();
  int info=dao2.update(t3.getText(),t1.getText());
  if(info==1){
  l7.setText("恭喜您,密码修改成功,请牢记!!!");
l7.setVisible(true);
  }else{
  l7.setText("对不起,系统异常,稍后重试");
l7.setVisible(true);
  }
  }

}else{
l7.setText("新密码格式有误请验证!!!");
l7.setVisible(true);
}

}else{
l7.setText("两次密码不对,请核对!!!");
l7.setVisible(true);
System.out.println("123");

}

}
});

b2=new JButton("退   出");
b2.setBounds(160, 240, 80, 20);
jp.add(b2);
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
exit();
new Login();
//System.exit(0);
}
});

b3=new JButton("重   置");
b3.setBounds(280, 240, 80, 20);
jp.add(b3);
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
}
});

l7=new JLabel("不允许有空值!!!");
l7.setBounds(30, 270, 420, 30);
l7.setForeground(Color.red);
l7.setFont(font1);
l7.setVisible(false);
l7.addMouseListener(new MouseAdapter(){


public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
l7.setVisible(false);
}


 });
jp.add(l7);

this.validate();
}

public void exit(){
this.dispose();
}

public boolean validate2(String str){

char cha1[]=str.toCharArray();//将给定的字符串转换成Char数组

for(int i=0;i<str.length();i++){//用for循环遍历数组
char cha=cha1[i];
if(Character.isDigit(cha)||Character.isLetter(cha)){//判断是否为数字或字母

}else{

return false;
}
}
return true;
}

}


UserData类

package util;


import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;


import javax.sound.midi.SysexMessage;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;


public class UserData extends JFrame {


JList list;
JLabel l1,l2;
JButton b1,b2,b3,b4;
JScrollPane jsp1;
Font font=new Font("宋体",1,15);
Font font1=new Font("宋体",1,25);
String str;
DefaultListModel model;
//Vector v=new UserregDAO().select();

public UserData(){
init();
}


private void init() {
// TODO Auto-generated method stub

SwingUtilities.invokeLater(new Runnable(){

public void run() {
try{
// TODO Auto-generated method stub
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (Exception exception) {
// TODO: handle exception
}
}

});

Toolkit tk=Toolkit.getDefaultToolkit();
Image image=tk.createImage("图标.jpg"); /*image.gif是你的图标*/ 
this.setIconImage(image);
model=new DefaultListModel();
this.setVisible(true);
this.setBounds(300, 100, 300, 400);
this.setTitle(" 用户管理");
this.setResizable(false);
JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(null);
jp.setBackground(new Color(200,210,100));
this.setLayout(null);

l1=new JLabel("+++所有已注册用户+++");
l1.setBounds(10, 10, 300, 20);
l1.setFont(font);
jp.add(l1);

list=new JList();
list.setBorder(new LineBorder(new Color(199,255,200),3));
list.setBounds(20, 40, 150, 280);
list.addMouseListener(new MouseAdapter(){


public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getButton()==1){
String s=(String)list.getSelectedValue();
str=s;
}
}
});

jsp1=new JScrollPane(list);
jsp1.setVisible(true);
jsp1.setBounds(20, 40, 150, 280);
jsp1.setBorder(BorderFactory.createEtchedBorder());
jp.add(jsp1);
jsp1.getViewport().add(list);
// 分别设置水平和垂直滚动条自动出现 
jsp1.setHorizontalScrollBarPolicy( 
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
jsp1.setVerticalScrollBarPolicy( 
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

b1=new JButton("显  示");
b1.setBounds(200, 65, 70, 30);
jp.add(b1);
b1.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
model.removeAllElements();

Vector v=new UserregDAO().select();
if(v.size()!=0){

for(int i=0;i<v.size();i++){
String strs=(String)v.get(i);
model.addElement(strs+"\n");
}

}
list.setModel(model);
list.validate();
}

});


b2=new JButton("隐  藏");
b2.setBounds(200, 130, 70, 30);
jp.add(b2);
b2.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
model.removeAllElements();

}

});

b3=new JButton("删  除");
b3.setBounds(200, 190, 70, 30);
jp.add(b3);
b3.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
UserregDAO dao=new UserregDAO();
String s=str.trim();//去掉空格
int info=dao.delete(s);
if(info!=0){
l2.setText("删 除 成 功");
l2.setVisible(true);


model.removeAllElements();
Vector v=new UserregDAO().select();
if(v.size()!=0){

for(int i=0;i<v.size();i++){
String strs=(String)v.get(i);
model.addElement(strs+"\n");
}

}
list.setModel(model);
list.validate();
}else{
l2.setText("删 除 失 败");
l2.setVisible(true);
}
}

});

b2=new JButton("退  出");
b2.setBounds(200, 255, 70, 30);
jp.add(b2);
b2.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent arg0){
// TODO Auto-generated method stub
System.exit(0);
//exit();
}

});
l2=new JLabel();
l2.setBounds(40, 330, 150, 30);
l2.setForeground(Color.red);
l2.setVisible(false);
l2.setFont(font1);
jp.add(l2);
l2.addMouseListener(new MouseAdapter(){


public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
l2.setVisible(false);
}


});

this.validate();
}

public void exit(){
this.dispose();
}

}


UserMain类

package util;
import java.util.Vector;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import com.*;

public class UserMain {

public UserMain(){
Userchat chat=new Userchat();
new Thread(chat).start();
}

public static void main(String[] args) {
// TODO Auto-generated method stub

SwingUtilities.invokeLater(new Runnable(){

public void run() {
try{
// TODO Auto-generated method stub
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (Exception exception) {
// TODO: handle exception
}
new Login();

}

});


}


}


Userreg类

package util;

public final class Userreg {


private String name; 
private String pwd; 


public final String getName() {
return name;
}


public final void setName(String name) {
this.name = name;
}


public final String getPwd() {
return pwd;
}


public final void setPwd(String pwd) {
this.pwd = pwd;
}


Userreg(){

}


Userreg(String name,String pwd){
this.name=name;
this.pwd=pwd;
}

}


UserregDAO类

package util;


import java.sql.*;
import java.util.*;


public final class UserregDAO {


private static Connection connobj;

public Vector getAllUser(){
Vector users=new Vector();
Statement state=null;
ResultSet rs=null;

try {
state=connobj.createStatement();
rs=state.executeQuery("select * from login");

while(rs.next()){
Userreg temp=new Userreg();
temp.setName(rs.getString(1));
temp.setPwd(rs.getString(2));
users.add(temp);

}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
ConnectionManager.closeDBconn(rs);
ConnectionManager.closeDBconn(state);
ConnectionManager.closeDBconn(connobj);
}

return users;
}

public UserregDAO(){
getConnection();
}

private void getConnection() {
// TODO Auto-generated method stub
connobj=ConnectionManager.getConnectionobj();
}

public boolean validateReg(String userName){

boolean bool=true;
java.sql.PreparedStatement ps=null;
java.sql.ResultSet rs=null;
String sql="select * from Login where name=?";
try {
ps=connobj.prepareStatement(sql);
ps.setString(1, userName);
rs=ps.executeQuery();
if(rs.next())
bool=false;// 有相同的用户名
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{

try {
rs.close();
ps.close();
connobj.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bool;
}

public int insert(String userName,String pwd){
int info=0;
PreparedStatement ps=null;
String sql="insert into Login values(?,?)";
try {
ps=connobj.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);

info=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ConnectionManager.closeDBconn(connobj);
}
return info;
}

public boolean validateUpdate(String userName,String pwd){

boolean bool=false;
java.sql.PreparedStatement ps=null;
java.sql.ResultSet rs=null;
String sql="select * from Login where name=? and pwd=?";
try {
ps=connobj.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
rs=ps.executeQuery();
if(rs.next())
bool=true;// 有相同的用户名
} catch (SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{

try {
rs.close();
ps.close();
connobj.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bool;
}

public int update(String pwd,String userName){
int info=0;
PreparedStatement ps=null;
String sql="update login set pwd=? where name=?";
try {
ps=connobj.prepareStatement(sql);
ps.setString(1, pwd);
ps.setString(2, userName);
info=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ConnectionManager.closeDBconn(connobj);
}
return info;

}

public int delete(String userName){
int info=0;
PreparedStatement ps=null;
String sql="delete from Login where name=?";
try {
ps=connobj.prepareStatement(sql);
ps.setString(1, userName);
info=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ConnectionManager.closeDBconn(connobj);
}
return info;
}

public Vector select(){

Vector users=new Vector();

PreparedStatement ps=null;
ResultSet rs=null;
String sql="select name from Login";
try {
ps=connobj.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
String s=rs.getString(1);
users.add(s);
}

} catch (SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{

try {
rs.close();
ps.close();
connobj.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return users;
}


public boolean selectlogin(String userName,String pwd){

boolean bool=false;
java.sql.PreparedStatement ps=null;
java.sql.ResultSet rs=null;
String sql="select * from Login where name=? and pwd=?";
try {
ps=connobj.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
rs=ps.executeQuery();
if(rs.next())
bool=true;// 有该用户名
} catch (SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{

try {
rs.close();
ps.close();
connobj.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bool;
}
}



------------------------- android培训java培训、期待与您交流! -------------------------

详细请查看:http://edu.csdn.net/heima

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值