使用Vector在java中显示JTable中更改的mysql表内容

时间:2012-04-08 14:03:41

标签: mysql vector jtable jframe

我有一个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节点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);

Table NODE schema

1 个答案:

答案 0 :(得分: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)将跳过空值,但是你可以计算一个表达式,所以你将一个表达式一起计算,当你不想计算它时它是空的。