文章目录
- 任务16
- 【任务16.1】数据的请求和响应
- 【任务16.2】创建JTable的数据适配器类MatchedTableModel,直接从数据库获取数据。
任务16
【任务16.1】数据的请求和响应
数据的请求和响应方法
程序设计
package com.qst.dms.service;
import com.qst.dms.net.Request;
import com.qst.dms.net.Response;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class DmsNetService {
private static DmsNetService instance = new DmsNetService();
private DmsNetService(){
}
public static DmsNetService getInstance(){
return instance;
}
public static void sendRequest(Socket socket, Request request) throws IOException{
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(request);
out.flush();
}
public static Request receiveRequest(Socket socket) throws ClassNotFoundException, IOException{
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
Request req = (Request)in.readObject();
return req;
}
public static void sendResponse(Socket socket, Response response) throws IOException{
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(response);
out.flush();
}
public static Response receiveResponse(Socket socket) throws IOException,ClassNotFoundException{
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
Response res = (Response)in.readObject();
return res;
}
}
请求封装类
程序设计
package com.qst.dms.net;
import com.qst.dms.entity.MatchedDataBase;
import java.io.Serializable;
import java.util.List;
public class Request implements Serializable {
private static final long serialVersionUID = 1L;
public static final int DATA_TYPE_LOGREC = 1;
public static final int DATA_TYPE_TRANSPORT = 2;
private String time;
private String ip;
private int type;
private List<MatchedDataBase> data;
public Request(){
super();
}
public Request(String time, String ip, int type, List<MatchedDataBase> data){
super();
this.time=time;
this.ip=ip;
this.type=type;
this.data=data;
}
/**
* 获取
* @return time
*/
public String getTime() {
return time;
}
/**
* 设置
* @param time
*/
public void setTime(String time) {
this.time = time;
}
/**
* 获取
* @return ip
*/
public String getIp() {
return ip;
}
/**
* 设置
* @param ip
*/
public void setIp(String ip) {
this.ip = ip;
}
/**
* 获取
* @return type
*/
public int getType() {
return type;
}
/**
* 设置
* @param type
*/
public void setType(int type) {
this.type = type;
}
/**
* 获取
* @return data
*/
public List<MatchedDataBase> getData() {
return data;
}
/**
* 设置
* @param data
*/
public void setData(List<MatchedDataBase> data) {
this.data = data;
}
public String toString() {
return "Request{time = " + time + ", ip = " + ip + ", type = " + type + ", data = " + data + "}";
}
}
响应封装类
程序设计
package com.qst.dms.net;
import java.io.Serializable;
public class Response implements Serializable {
private static final long serialVersionUID = 1L;
public static final int OK =200;
public static final int ERROR = 500;
private String time;
private int state;
public Response(){
super();
}
public Response(String time, int state) {
super();
this.time = time;
this.state = state;
}
/**
* 获取
* @return time
*/
public String getTime() {
return time;
}
/**
* 设置
* @param time
*/
public void setTime(String time) {
this.time = time;
}
/**
* 获取
* @return state
*/
public int getState() {
return state;
}
/**
* 设置
* @param state
*/
public void setState(int state) {
this.state = state;
}
public String toString() {
return "Response{time = " + time + ", state = " + state + "}";
}
}
【任务16.2】创建JTable的数据适配器类MatchedTableModel,直接从数据库获取数据。
在com.qst.dms.entity类下创MatchedTableModel,继承AbstractTableModel
// 使用ResultSet来创建TableModel
private ResultSet rs;
private ResultSetMetaData rsmd;
// 标志位,区分日志和物流:1,日志;0,物流
private int sign;
public MatchedTableModel(ResultSet rs, int sign) {
this.rs = rs;
this.sign = sign;
try {
rsmd = rs.getMetaData();
} catch (Exception e) {
rsmd = null;
}
}
// 获取表格的行数
public int getRowCount() {
try {
rs.last();
// System.out.println(count);
return rs.getRow();
} catch (Exception e) {
return 0;
}
}
// 获取表格的列数
public int getColumnCount() {
try {
// System.out.println(rsmd.getColumnCount());
return rsmd.getColumnCount();
} catch (Exception e) {
return 0;
}
}
// 获取指定位置的值
public Object getValueAt(int rowIndex, int columnIndex) {
try {
rs.absolute(rowIndex + 1);
return rs.getObject(columnIndex + 1);
} catch (Exception e) {
return null;
}
}
// 获取表头信息
public String getColumnName(int column) {
String[] logArray = { "日志ID", "采集时间", "采集地点", "状态", "用户名", "IP", "日志类型" };
String[] tranArray = { "物流ID", "采集时间", "目的地", "状态", "经手人", "收货人",
"物流类型" };
return sign == 1 ? logArray[column] : tranArray[column];
}
// 菜单项,注册监听
miShow.addActionListener(new ShowDataListener());
// 工具栏项 注册监听
btnShow.addActionListener(new ShowDataListener());
LogRecService类中增加方法readLogResult()
//获取数据库中的所有匹配的日志信息,返回一个ResultSet
public ResultSet readLogResult() {
//获取匹配日志表中的所有数据,返回ResultSet
//创建语句时使用:
//Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
}
TransportService 类中增加方法readTransResult()
//获取数据库中的所有匹配的物流信息,返回一个ResultSet
public ResultSet readTransResult() {
//获取匹配物流表中的所有数据,返回ResultSet
//Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
}
程序设计
package com.qst.dms.entity;
import javax.swing.table.AbstractTableModel;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
public class MatchedTableModel extends AbstractTableModel {
// 使用ResultSet来创建TableModel
private ResultSet rs;
private ResultSetMetaData rsmd;
// 标志位,区分日志和物流:1,日志;0,物流
private int sign;
public MatchedTableModel(ResultSet rs, int sign) {
this.rs = rs;
this.sign = sign;
try {
rsmd = rs.getMetaData();
} catch (Exception e) {
rsmd = null;
}
}
// 获取表格的行数
public int getRowCount() {
try {
rs.last();
// System.out.println(count);
return rs.getRow();
} catch (Exception e) {
return 0;
}
}
// 获取表格的列数
public int getColumnCount() {
try {
// System.out.println(rsmd.getColumnCount());
return rsmd.getColumnCount();
} catch (Exception e) {
return 0;
}
}
// 获取指定位置的值
public Object getValueAt(int rowIndex, int columnIndex) {
try {
rs.absolute(rowIndex + 1);
return rs.getObject(columnIndex + 1);
} catch (Exception e) {
return null;
}
}
// 获取表头信息
public String getColumnName(int column) {
String[] logArray = { "日志ID", "采集时间", "采集地点", "状态", "用户名", "IP", "日志类型" };
String[] tranArray = { "物流ID", "采集时间", "目的地", "状态", "经手人", "收货人",
"物流类型" };
return sign == 1 ? logArray[column] : tranArray[column];
}
}
public static ResultSet readLogResult() {
ResultSet matchedLogsResult = null;
//获取匹配日志表中的所有数据,返回ResultSet
//创建语句时使用:
DBUtil db = new DBUtil();
Connection conn = null;
try {
conn = db.getConnection();
Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
// 查询匹配的日志
String querySqlMatchedLogs = "SELECT * FROM matchlogs";
matchedLogsResult = db.executeQuery(querySqlMatchedLogs,null);
} catch (Exception e) {
e.printStackTrace();
}
return matchedLogsResult;
}
public ResultSet readTransResult() {
//获取匹配物流表中的所有数据,返回ResultSet
ResultSet matchedTransResult = null;
//获取匹配日志表中的所有数据,返回ResultSet
//创建语句时使用:
DBUtil db = new DBUtil();
Connection conn = null;
ArrayList<MatchedTransport> matchedTrans = new ArrayList<>();
try {
conn = db.getConnection();
Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
// 查询匹配的日志
String querySqlMatchedLogs = "SELECT * FROM matchtransports";
matchedTransResult = st.executeQuery(querySqlMatchedLogs);
} catch (Exception e) {
e.printStackTrace();
}
return matchedTransResult;
}
测试
服务端
客户端