Java利用套接字實現應用程序對數據庫的訪問

前言

最近在完成軟件體系結構上機實驗時,遇到一個有點點小難度的選做題,題目信息如下:

利用套接字技術實現應用程序中對數據庫的訪問。應用程序隻是利用套接字連接向服務器發送一個查詢的條件,而服務器負責對數據庫的查詢,然後服務器再將查詢的結果利用建立的套接字返回給客戶端,如下圖所示。

本來吧,選做題,不太想做的,但是考慮到以後工作的方向和後端相關,那還是做吧。

本次實驗需要做一個GUI界面和一個連接查詢功能,在論壇上借鑒瞭其他大佬獲取網站內容的部分代碼,然後自己做瞭一個及其簡陋的swing界面,算是把這個實驗完成瞭。

本次實驗項目結構如下

--socketProject
    |--Client.java
    |--GUI.java
    |--SearchInfo.java
    |--Server.java
    |--ServerThread.java

Client.java

客戶端使用dis.readUTF()時,要註意再發送個字符或者空字符,這裡發送end,表示關閉連接。不然會出現EOFException

package socketProject;

import java.io.*;
import java.net.*;

public class Client {
    String studentNum = null;
    String result = null;

    public void setStudentNum(String num) {
        this.studentNum = num;
        System.out.println("stu: " + studentNum);
    }

    public void run() throws IOException {
        Socket ss = new Socket("127.0.0.1", 8888);
        System.out.println("Socket: " + ss);
        try {
            DataInputStream dis = new DataInputStream(ss.getInputStream());
            DataOutputStream dos = new DataOutputStream(ss.getOutputStream());
            // the interaction
            dos.writeUTF(studentNum); // 向服務器發送學號
            dos.flush();
            result = dis.readUTF().toString(); // 獲得客戶端的json字符串
            System.out.println(result);
            dos.writeUTF("end"); // 不加這句會報錯
            dos.flush();
            if (dos != null)
                dos.close();
            if (dis != null)
                dis.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ss != null)
                ss.close();
        }
    }
	// gui界面用於獲取json結果
    public String getResult() {
        return result;
    }
}

Server.java

package socketProject;

import java.io.*;
import java.net.*;

public class Server extends Thread {
    public static final int PORT = 8888;

    // public static void main(String[] args) throws IOException {
    public void run() {
        try (ServerSocket serverSocket = new ServerSocket(PORT)) {
            System.out.println("ServerSocket: " + serverSocket);
            try {
                while (true) {
                    Socket socket = serverSocket.accept();
                    System.out.println("Socket accept: " + socket);
                    Thread thread = new Thread(new ServerThread(socket));
                    thread.start(); // 開啟一個線程,使之支持接收多個客戶端的請求
                }
            } finally {
                serverSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ServerThread.java

package socketProject;

import java.io.*;
import java.net.*;

public class ServerThread extends Thread {
    Socket socket = null;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        try {
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            while (true) {
                String str = dis.readUTF().toString();
                String data = new SearchInfo().run(str);
                if (str.equals("end"))
                    break;

                dos.writeUTF(data);
            }
            dos.close();
            dis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

SearchInfo.java

package socketProject;

import java.io.*;
import java.net.*;

public class SearchInfo {
    public String run(String s) {
        String url = "your database interface";
        String param = s;
        String sendGET = GetUrl(url, param);
        return sendGET;
    }
    
    public static String GetUrl(String url, String param) {
        String result = ""; // define the result str
        BufferedReader read = null; // define the access result
        
        try {
            URL realUrl = new URL(url + param);
            URLConnection connection = realUrl.openConnection();
            
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 這裡補充通用的請求屬性
            connection.connect(); // 建立實際的連接
            
            read = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            while ((line = read.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (read != null) {// 關閉流
                try {
                    read.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    
    public String getJSON(String param) {
        return param;
    }
}

GUI.java

package socketProject;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;

public class GUI extends JFrame {
    private JButton connectDataBase;
    private JLabel entryStudentNum;
    private JTextField studentNum;
    private JButton sendRequest;
    private JLabel showResponseMsg;
    private JPanel northPanel;
    private JPanel southPanel;

    public GUI() {
        init();
    }

    public void init() {
        setTitle("沒啥技術含量的東西");
        // define the component for the window
        connectDataBase = new JButton("連接數據庫");
        entryStudentNum = new JLabel("輸入學號");
        studentNum = new JTextField();
        sendRequest = new JButton("發送");
        showResponseMsg = new JLabel();

        // add the component to the panel
        this.setLayout(new GridLayout(2, 1));

        northPanel = new JPanel(new GridLayout(1, 4));

        northPanel.add(connectDataBase);
        northPanel.add(entryStudentNum);
        northPanel.add(studentNum);
        northPanel.add(sendRequest);

        southPanel = new JPanel(new GridLayout(1, 1));

        southPanel.add(showResponseMsg);

        setButtons();

        this.add(northPanel);
        this.add(southPanel);
        // initial the window
        setBounds(400, 200, 600, 120);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void setButtons() {
        connectDataBase.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 這裡初始化服務端
                Server server1 = new Server();
                Thread th1 = new Thread(server1);
                th1.start();
                // 這裡一定要開啟服務端線程,否則在點擊此按鈕後,整個界面會卡住,無法進行下一步操作
            }
        });

        sendRequest.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Client client1 = new Client();
                client1.setStudentNum(studentNum.getText());
                // 獲取文本框的文字,並賦給客戶端的studentNum保存
                try {
                    client1.run();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                showResponseMsg.setText(client1.getResult());
                // 將得到的數據顯示在界面上
            }
        });
    }

    public static void main(String[] args) {
        new GUI();
    }
}

最終效果如下:

使用時,先點擊連接數據庫,然後根據學校提供的接口,輸入自己的學號,點擊發送,即可查詢個人信息。

不過由於項目工作區非maven以及未來方向非Java的緣故,沒有去深究如何提取json的值 (偷個懶)。

以上就是Java利用套接字實現應用程序對數據庫的訪問 的詳細內容,更多關於Java套接字的資料請關註WalkonNet其它相關文章!

推薦閱讀: