You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
3.1 KiB
98 lines
3.1 KiB
7 years ago
|
package com.fr.data;
|
||
|
|
||
|
import java.io.InputStream;
|
||
|
import java.sql.Connection;
|
||
|
import java.sql.DriverManager;
|
||
|
import java.sql.ResultSet;
|
||
|
import java.sql.Statement;
|
||
|
import java.util.ArrayList;
|
||
6 years ago
|
import java.io.StringBufferInputStream;
|
||
7 years ago
|
|
||
|
public class XMLRead extends AbstractTableData {
|
||
6 years ago
|
// 列名数组,保存程序数据集所有列名
|
||
|
private String[] columnNames = { "id", "name", "MemoryFreeSize",
|
||
|
"MemoryTotalSize", "MemoryUsage" };
|
||
|
// 保存表数据
|
||
7 years ago
|
private ArrayList valueList = null;
|
||
|
|
||
|
public int getColumnCount() {
|
||
|
return 5;
|
||
|
}
|
||
|
|
||
|
public String getColumnName(int columnIndex) {
|
||
|
return columnNames[columnIndex];
|
||
|
}
|
||
|
|
||
|
public int getRowCount() {
|
||
|
init();
|
||
|
return valueList.size();
|
||
|
}
|
||
|
|
||
|
public Object getValueAt(int rowIndex, int columnIndex) {
|
||
|
init();
|
||
|
return ((Object[]) valueList.get(rowIndex))[columnIndex];
|
||
|
}
|
||
|
|
||
|
private void init() {
|
||
|
// 确保只被执行一次
|
||
|
if (valueList != null) {
|
||
|
return;
|
||
|
}
|
||
|
valueList = new ArrayList();
|
||
6 years ago
|
String sql = "select * from xmltest";
|
||
|
String[] name = { "MemoryFreeSize", "MemoryTotalSize", "MemoryUsage" };
|
||
|
Connection conn = this.getConncetion();
|
||
7 years ago
|
try {
|
||
|
Statement stmt = conn.createStatement();
|
||
|
ResultSet rs = stmt.executeQuery(sql);
|
||
|
// 用对象保存数据
|
||
6 years ago
|
Object[] objArray = null;
|
||
7 years ago
|
while (rs.next()) {
|
||
|
objArray = new Object[5];
|
||
6 years ago
|
String[] xmldata = null;
|
||
7 years ago
|
objArray[0] = rs.getObject(1);
|
||
|
objArray[1] = rs.getObject(2);
|
||
6 years ago
|
InputStream in = new StringBufferInputStream("<demo>"
|
||
|
+ rs.getObject(3).toString() + "</demo>");
|
||
|
GetXmlDate getxmldata = new GetXmlDate();
|
||
7 years ago
|
// 对xml流进行解析,返回的为name对应的value值数组
|
||
6 years ago
|
xmldata = getxmldata.readerXMLSource(in, name);
|
||
7 years ago
|
// 将解析后的值存于最终结果ArrayList中
|
||
6 years ago
|
objArray[2] = xmldata[0];
|
||
|
objArray[3] = xmldata[1];
|
||
|
objArray[4] = xmldata[2];
|
||
7 years ago
|
valueList.add(objArray);
|
||
|
}
|
||
|
// 释放数据源
|
||
|
rs.close();
|
||
|
stmt.close();
|
||
|
conn.close();
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
public Connection getConncetion() {
|
||
|
String driverName = "com.mysql.jdbc.Driver";
|
||
|
String url = "jdbc:mysql://review.finedevelop.com:3306/susie";
|
||
|
String username = "root";
|
||
|
String password = "ilovejava";
|
||
|
Connection con = null;
|
||
7 years ago
|
|
||
|
try {
|
||
|
Class.forName(driverName);
|
||
|
con = DriverManager.getConnection(url, username, password);
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
return null;
|
||
|
}
|
||
|
return con;
|
||
|
|
||
|
}
|
||
|
|
||
6 years ago
|
// 释放一些资源,因为可能会有重复调用,所以需释放valueList,将上次查询的结果释放掉
|
||
7 years ago
|
public void release() throws Exception {
|
||
|
super.release();
|
||
|
this.valueList = null;
|
||
|
}
|
||
|
}
|