Java?swing實(shí)現(xiàn)應(yīng)用程序?qū)?shù)據(jù)庫(kù)的訪問問題
用Java swing實(shí)現(xiàn)一個(gè)套接字訪問數(shù)據(jù)庫(kù)
最近在完成軟件體系結(jié)構(gòu)上機(jī)實(shí)驗(yàn)時(shí),遇到一個(gè)有點(diǎn)點(diǎn)小難度的選做題,題目信息如下:
利用套接字技術(shù)實(shí)現(xiàn)應(yīng)用程序中對(duì)數(shù)據(jù)庫(kù)的訪問。應(yīng)用程序只是利用套接字連接向服務(wù)器發(fā)送一個(gè)查詢的條件,而服務(wù)器負(fù)責(zé)對(duì)數(shù)據(jù)庫(kù)的查詢,然后服務(wù)器再將查詢的結(jié)果利用建立的套接字返回給客戶端,如下圖所示。
本來(lái)吧,選做題,不太想做的,但是考慮到以后工作的方向和后端相關(guān),那還是做吧。
本次實(shí)驗(yàn)需要做一個(gè)GUI界面和一個(gè)連接查詢功能,在論壇上借鑒了其他大佬獲取網(wǎng)站內(nèi)容的部分代碼,然后自己做了一個(gè)及其簡(jiǎn)陋的swing界面,算是把這個(gè)實(shí)驗(yàn)完成了。
本次實(shí)驗(yàn)項(xiàng)目結(jié)構(gòu)如下
--socketProject |--Client.java |--GUI.java |--SearchInfo.java |--Server.java |--ServerThread.java
Client.java
客戶端使用dis.readUTF()
時(shí),要注意再發(fā)送個(gè)字符或者空字符,這里發(fā)送end
,表示關(guān)閉連接。不然會(huì)出現(xiàn)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); // 向服務(wù)器發(fā)送學(xué)號(hào) dos.flush(); result = dis.readUTF().toString(); // 獲得客戶端的json字符串 System.out.println(result); dos.writeUTF("end"); // 不加這句會(huì)報(bào)錯(cuò) 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結(jié)果 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(); // 開啟一個(gè)線程,使之支持接收多個(gè)客戶端的請(qǐng)求 } } 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)"); // 這里補(bǔ)充通用的請(qǐng)求屬性 connection.connect(); // 建立實(shí)際的連接 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) {// 關(guān)閉流 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("沒啥技術(shù)含量的東西"); // define the component for the window connectDataBase = new JButton("連接數(shù)據(jù)庫(kù)"); entryStudentNum = new JLabel("輸入學(xué)號(hào)"); studentNum = new JTextField(); sendRequest = new JButton("發(fā)送"); 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) { // 這里初始化服務(wù)端 Server server1 = new Server(); Thread th1 = new Thread(server1); th1.start(); // 這里一定要開啟服務(wù)端線程,否則在點(diǎn)擊此按鈕后,整個(gè)界面會(huì)卡住,無(wú)法進(jìn)行下一步操作 } }); 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()); // 將得到的數(shù)據(jù)顯示在界面上 } }); } public static void main(String[] args) { new GUI(); } }
最終效果如下:
使用時(shí),先點(diǎn)擊連接數(shù)據(jù)庫(kù),然后根據(jù)學(xué)校提供的接口,輸入自己的學(xué)號(hào),點(diǎn)擊發(fā)送,即可查詢個(gè)人信息。
不過由于項(xiàng)目工作區(qū)非maven以及未來(lái)方向非Java的緣故,沒有去深究如何提取json的值 (偷個(gè)懶)。
參考鏈接
Java請(qǐng)求一個(gè)URL,獲取返回的數(shù)據(jù)
java.io.datainputstream.readunsignedshort_socket編程報(bào)異常java.io.EOFException
到此這篇關(guān)于Java swing實(shí)現(xiàn)應(yīng)用程序?qū)?shù)據(jù)庫(kù)的訪問的文章就介紹到這了,更多相關(guān)Java swing訪問數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java如何讀取properties文件將參數(shù)值配置到靜態(tài)變量
這篇文章主要介紹了java如何讀取properties文件將參數(shù)值配置到靜態(tài)變量問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08簡(jiǎn)單講解Android開發(fā)中觸摸和點(diǎn)擊事件的相關(guān)編程方法
這篇文章主要介紹了Android開發(fā)中觸摸和點(diǎn)擊事件的相關(guān)編程方法,包括事件偵聽器等安卓開發(fā)中常用的接口的基本使用方法,需要的朋友可以參考下2015-12-12Java實(shí)現(xiàn)畫線、矩形、橢圓、字符串功能
本篇文章主要介紹了Java實(shí)現(xiàn)畫線、矩形、橢圓、字符串功能的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-05-05IDEA中Spring項(xiàng)目的工程構(gòu)建
這篇文章主要介紹了IDEA中Spring項(xiàng)目的工程構(gòu)建,Spring框架是輕量級(jí)的JavaEE框架,可以解決企業(yè)應(yīng)用開發(fā)的復(fù)雜性,有兩個(gè)核心部分:IOC和Aop,今天來(lái)學(xué)習(xí)如何構(gòu)建spring項(xiàng)目,需要的朋友可以參考下2023-05-05