我有一个存储无线传感器模拟结果日志的 mysql 表。它存储每个传感器节点的节点 ID,每个节点的能量和每个传感器的状态,即它是处于发送状态还是接收状态等。现在我想创建一个 JTable,它为每行显示 5 个字段,它们是: NodeId ,剩余能量,已发送数据包数,已接收数据包数,已损坏数据包数。
我正在使用以下查询:
SELECT COUNT(*) FROM node WHERE nodeid='i' AND stetus='sending'
SELECT COUNT(*) FROM node WHERE nodeid='i' AND stetus='corrupted'
SELECT COUNT(*) FROM node WHERE nodeid='i' AND stetus='recieved'
SELECT MIN(energi) FROM node WHERE nodeid='i'
获取 JTable 内容的字段。以下是我编写的代码:请帮我解决这个问题。
我无法显示 JTable。
import java.util.Vector.*;
import java.sql.*;
JFrame f = new JFrame();
f.setSize(800,800);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Connection Db=null;
ResultSet Results,res,rest,re;
String stetus;
Vector data = new Vector();
Vector row = new Vector(50);
JPanel p = new JPanel();
Vector<String> columnNames = new Vector<String>();
double energi,nodeid,en;
String url= "jdbc:mysql://localhost:3306/prowler";
String username="root";
String password = "not telling you";
try {
Class.forName("com.mysql.jdbc.Driver");
Db= DriverManager.getConnection(url,username,password);
}
catch(ClassNotFoundException cnf) {
System.err.println("Unable to load JDBC bridge" + cnf);
System.exit(1);
}
catch(SQLException se) {
System.err.println("Cannot connect to database" + se);
System.exit(2);
}
int num = Integer.parseInt(name.getSelectedText());
for(int i=1;i<=num;i++) {
row.addElement(i);
try {
Statement st = Db.createStatement();
Results = st.executeQuery("SELECT COUNT(*) FROM node WHERE nodeid='i' AND stetus='sending'");
row.addElement(Results.getObject(1));
}
catch(SQLException se) {
System.out.println("Query Not Executed" + se);
}
try {
Statement st = Db.createStatement();
res = st.executeQuery("SELECT COUNT(*) FROM node WHERE nodeid='i' AND stetus='corrupted'");
row.addElement(res.getObject(1));
}
catch(SQLException se) {
System.out.println("Query Not Executed" + se);
}
try {
Statement st = Db.createStatement();
rest = st.executeQuery("SELECT COUNT(*) FROM node WHERE nodeid='i' AND stetus='recieved'");
row.addElement(rest.getObject(1));
}
catch(SQLException se) {
System.out.println("Query Not Executed" + se);
}
try {
Statement st = Db.createStatement();
re = st.executeQuery("SELECT MIN(energi) FROM node WHERE nodeid='i'");
row.addElement(re.getObject(1));
}
catch(SQLException se) {
System.out.println("Query Not Executed" + se);
}
data.addElement(row);
}
columnNames.add("Node Id");
columnNames.add("Packets Sent");
columnNames.add("Packets Corrupted");
columnNames.add("Packets Recieved");
columnNames.add("Energy Left");
JTable table = new JTable(data,columnNames);
JScrollPane jsp = new JScrollPane(table);
p.add(jsp);
f.add(p);