-1

我有一个存储无线传感器模拟结果日志的 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);

表节点架构

4

1 回答 1

0

好吧,如果我得到你,这很简单,比如

Select nodeid, 
Count(case stetus when 'sending' then 1 else null end) as Sent,
Count(case stetus when 'received' then 1 else null end) as Received,
Count(case stetus when 'corrupted' then 1 else null end) as Corrupted,
Min(energi) as remaining
From Node Group By NodeId

我认为

计数内的案例操作是一个偷偷摸摸的把戏。Count(SomeColumnName) 将跳过空值,但您可以计算一个表达式,因此您可以一起 bash 一个,当您不希望它在计数中时,它是空值。

于 2012-04-08T21:14:47.323 回答