package cn.edu.pdsu;import java.awt.*;import java.awt.event.*;
import java.io.*;import java.*;import javax.swing.*;
public class Login {
public static void main(String args[]) { new WindowBox("软件体系结构实验二"); } }
class WindowBox extends Frame implements ActionListener, Runnable {
private Box baseBox1, boxV1, boxV2, baseBox2, boxV3, boxV4, boxV5,
baseBox3, boxV6, baseBox4, boxV7;
private Button but1, but2, but3; private Label lab1, lab2, lab3;
private TextField text1, text2;  private Socket socket = null;
private DataInputStream dis = null; private DataOutputStream dos = null;
private Thread thread;
public WindowBox(String str) {
super(str); socket = new Socket();
thread = new Thread(this); but1 = new Button("连接");
but2 = new Button("登录"); but3 = new Button("注册");
lab1 = new Label("姓名"); lab2 = new Label("密码");
lab3 = new Label(""); text1 = new TextField(12);
text2 = new TextField(12); boxV1 = ateVerticalBox();
boxV1.add(lab1); boxV1.ateV erticalStrut(8));
boxV1.add(lab2); boxV2 = ateVerticalBox();
boxV2.add(text1); boxV2.ateV erticalStrut(8));
boxV2.add(text2); baseBox1 = ateHorizontalBox();
baseBox1.add(boxV1); baseBox1.ateHorizontalStrut(10));
baseBox1.add(boxV2); boxV3 = ateVerticalBox();
boxV3.add(but1); boxV4 = ateVerticalBox();
boxV4.add(but2); boxV5 = ateVerticalBox();
boxV5.add(but3); baseBox2 = ateHorizontalBox();
baseBox2.add(boxV3); baseBox2.ateHorizontalStrut(10));
baseBox2.add(boxV4); baseBox2.ateHorizontalStrut(10));
baseBox2.add(boxV5); boxV7 = ateVerticalBox();
boxV7.add(lab3); baseBox4 = ateHorizontalBox();
baseBox4.add(boxV7); boxV6 = ateVerticalBox();
boxV6.add(baseBox1); boxV6.ateV erticalStrut(8));
boxV6.add(baseBox2); boxV6.ateV erticalStrut(8));
boxV6.add(baseBox4); baseBox3 = ateHorizontalBox();
baseBox3.add(boxV6); this.setLayout(new FlowLayout());
this.add(baseBox3); setBounds(520, 225, 250, 150);
setVisible(true); but1.addActionListener(this);
but2.addActionListener(this);  but3.addActionListener(this);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {it(0);} });
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == but1) {
try {
if (socket.isConnected()) {
} else {
InetAddress address = ByName("127.0.0.1");
InetSocketAddress socketAddress = new InetSocketAddress(address, 4331);
dos = new OutputStream()); thread.start();
}
} catch (Exception e2) {
e2.printStackTrace(); lab3.setText("连接服务器失败!"); } }
Source() == but2){
if (Text().equals("") || Text().equals("")) {
System.out.println("请输入信息!");
lab3.setText("请输入信息!");
}else{
try{
dos.writeUTF("1");  dos.Text());
dos.Text());
}catch(Exception e1){}
} }
if (e.getSource() == but3) {
if (Text().equals("") || Text().equals("")) {
System.out.println("请输入信息!"); lab3.setText("请输入信息!");
}else{
try{
dos.writeUTF("2");  dos.Text());
dos.Text());
}catch(Exception e1){}
} }
}
public void run() {
while (true) {
try{ lab3.adUTF());
}catch(Exception ee){lab3.setText("与服务器已断开!");break;}
} } }
服务器端代码:
package cn.edu.pdsu;import java.io.*;import java.*;import java.sql.*;
public class Server {
public static void main(String args[]) {
ServerSocket server = null; Socket you = null;
while (true) {
try { server = new ServerSocket(4331);
} catch (IOException e1) { System.out.println("正在监听");  }
try { System.out.println(" 等待客户呼叫");
you = server.accept();
System.out.println("客户的地址:" + InetAddress());
} catch (IOException e) {System.out.println("正在等待客户"); }
if (you != null) {new Server_thread(you).start();  }
} } }
class Server_thread extends Thread {
private DataOutputStream dos = null; private DataInputStream dis = null;
private Socket socket; private String flag = null,name = null,password = null;
private ConnectionDB conn = null;
Server_thread(Socket t) {
socket = t; conn = new ConnectionDB();
try {
dos = new OutputStream());
dis = new InputStream());
} catch (IOException e) {}
}
public void run() {
while (true) {湖南省教育考试院入口自考
try{ flag = adUTF();name = adUTF(); password = adUTF();
if(flag.equals("1")){
if (isExist(name, password)) {
System.out.println("登录成功!");dos.writeUTF("欢迎"+name+"登陆本系统!");
} else {
System.out.println("登录失败!");
dos.writeUTF("登录失败!");
} }
if(flag.equals("2")){
if(isExist(name)) {
System.out.println("该用户已存在!");dos.writeUTF("该用户已存在!");
}else{
if(register(name, password)){
System.out.println("注册成功");dos.writeUTF("注册成功!");
}else{
System.out.println("注册失败!");dos.writeUTF("注册失败!");
} } }
}catch(Exception e){System.out.println("客户离开"); return;}
} }
public boolean isExist(String name,String password){
boolean status = false;
ResultSet rs = uteQuery("select * from logininfo where name = '"+name+"' and password = '"+password+"'");
try {
()){status = true;}
} catch (Exception e) {} return status; }
public boolean isExist(String name){
boolean status = false;
ResultSet rs = uteQuery("select * from logininfo where name = '"+name+"'");
try {
()){status = true;}
} catch (Exception e) {} return status; }
public boolean register(String name,String password){
boolean status = false;
status = uteUpdate("insert into logininfo values('"+name+"','"+password+"')");
return status;
}}
数据连接层代码:
package cn.edu.pdsu;import java.sql.*;
public class ConnectionDB {
private Connection conn = null; private Statement sta = null; private ResultSet rs = null;
public ConnectionDB() {
try {
Class.forName("sql.Driver").newInstance();
conn = Connection("jdbc:mysql://localhost:3306/login", "root", "root");
} catch (Exception ex) {} }
public ResultSet executeQuery(String sql) {
try {
sta = ateStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONL Y); rs = uteQuery(sql);
} catch (Exception ex) {}
return rs; }
public boolean executeUpdate(String sql) {
boolean status = false;
try {
sta = ateStatement();uteUpdate(sql);status = true;
} catch (Exception ex) {}
return status; }
public void close() {
try {
if (rs != null) rs.close();
if (sta != null) sta.close();
if (conn != null) conn.close();
} catch (Exception e) {}
}}