2016-11-21 13 views
0

親愛なる友人rs2xmlを使用してjtableに結果セットを表示する方法を知りたいと思います。jtablename.setmodel(dbutils.setresultsettotable(rs))を知っています。しかし、私はどのようにすべてのレコードをretrievとjtablle私のコードでそれらを表示するにはrs2xml jarライブラリを使用してJtableを生成する方法

private void search_by_topic() throws SQLException, ClassNotFoundException { 
    try 
    { 
     SessionFactory sf; 
     Configuration cfg=new Configuration(); 
     System.out.println("search_by_topic me agaya"); 
     cfg.configure("hiber_config/hibernate.cfg.xml"); 
     sf=cfg.buildSessionFactory(); 
     Session ses=sf.openSession(); 
     ses.beginTransaction(); 
     Query qr1=ses.createQuery("from Topic where chapter_id=:chapter_id"); 
     qr1.setParameter("chapter_id", chap_id); 
     List<Topic> topic_list=qr1.list(); 
     top_id=topic_list.get(jComboBox3.getSelectedIndex()).getTopic_id(); 
     System.out.println("Topic ID is "+top_id); 
     Query qr2=ses.createQuery("from Test as t where t.import_level=:import_level and t.import_level_id=:import_level_id"); 
     qr2.setParameter("import_level", topicimport); 
     qr2.setInteger("import_level_id",top_id); 
     List<Test> test_list=qr2.list(); 
     //Field[] fields = null; 
     System.out.println("Size of test_list is"+test_list.size()); 
     //Iterator<Test> itr=test_list.iterator(); 
     int[] test_id=new int[test_list.size()]; 

     for(int j=0;j<test_list.size();j++) 
     { 
      System.out.println("Test_List Import Wale me agaya"); 
      //fields=t.getClass().getDeclaredFields();  
      test_id[j]=test_list.get(j).getId(); 
      System.out.println("test_id: is "+test_id[j]); 
     } 
     System.out.println("Size of test_id array is "+test_id.length); 
     for(int j=0;j<test_id.length;j++) 
     { 
      System.out.println("test_id now is "+j); 
      int studentid=98; 
      String sqlquery="SELECT ST.ID id,ST.STUDENT_ID student_id,ST.attemptNumber attempt_Number,ST.TEST_ID testId,ST.TEST_ID student_test_id,T.description description,T.test_duration test_duration,T.test_score Test_Score,ST.SCHEDULED_START_TIME_BEGIN scheduled_start_time_begin,ST.SCHEDULED_START_TIME_END scheduled_start_time_end,ST.TEST_START_DATE test_start_date,ST.LAST_ACTIVE_TIME test_last_active_time,ST.TEST_ATTEMPTED test_attempted,ST.ALLOW_VIEW_REPORT allow_view_report FROM student_test as ST,test as T WHERE ST.PAYMENT_STATUS='A' AND ST.TEST_ID = T.id AND ST.STUDENT_ID='"+studentid+"' AND ST.TEST_ATTEMPTED='0' AND T.id='"+test_id[j]+"' ORDER BY ST.ID"; 
      Class.forName("com.mysql.jdbc.Driver"); 
      java.sql.Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/school_db","root",""); 
      Statement pst=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); 
      rs=pst.executeQuery(sqlquery); 
      System.out.println(rs.getRow()); 
      System.out.println(rs.toString()); 
      jTable1.revalidate(); 
      jTable1.setModel(DbUtils.resultSetToTableModel(rs)); 
      //jTable1.revalidate(); 
      /*SessionFactory sf1; 
      Configuration cfg1=new Configuration(); 
      System.out.println("join query me agaya"); 
      cfg1.configure("hiber_config/hibernate.cfg.xml"); 
      sf1=cfg.buildSessionFactory(); 
      Session ses1=sf1.openSession(); 
      ses1.beginTransaction(); 
      ses1.doWork(new Work() 
      { 
       @Override 
       public void execute(java.sql.Connection cnctn) throws SQLException 
       { 
        java.sql.PreparedStatement pst=cnctn.prepareStatement(sqlquery); 
        rs=pst.executeQuery(); 
        System.out.println(rs.toString()); 
       } 

      }); 
      ses1.getTransaction().commit(); 
     } 
     ses.getTransaction().commit(); 
     System.out.println("Test Data Retrieved"); 
     ses.close();*/ 
     } 
} 
    catch(HibernateException e) 
    { 
     e.printStackTrace(); 
    } 
} 
} 

である私のコードは正常にここで働いていると私はすべての結果セットを取得することができていますが、JTableの中で、私は唯一取得していますことを知っていただきたいと思います最後のループレコードは、すべてのループレコード は私に

private void search_by_topic() throws SQLException, ClassNotFoundException { 
     try 
     { 

      SessionFactory sf; 
      Configuration cfg=new Configuration(); 
      System.out.println("search_by_topic me agaya"); 
      cfg.configure("hiber_config/hibernate.cfg.xml"); 
      sf=cfg.buildSessionFactory(); 
      Session ses=sf.openSession(); 
      ses.beginTransaction(); 
      Query qr1=ses.createQuery("from Topic where chapter_id=:chapter_id"); 
      qr1.setParameter("chapter_id", chap_id); 
      List<Topic> topic_list=qr1.list(); 
      top_id=topic_list.get(jComboBox3.getSelectedIndex()).getTopic_id(); 
      System.out.println("Topic ID is "+top_id); 
      Query qr2=ses.createQuery("from Test as t where t.import_level=:import_level and t.import_level_id=:import_level_id"); 
      qr2.setParameter("import_level", topicimport); 
      qr2.setInteger("import_level_id",top_id); 
      List<Test> test_list=qr2.list(); 
      //Field[] fields = null; 
      System.out.println("Size of test_list is"+test_list.size()); 
      //Iterator<Test> itr=test_list.iterator(); 
      int[] test_id=new int[test_list.size()]; 

      for(int j=0;j<test_list.size();j++) 
      { 
       System.out.println("Test_List Import Wale me agaya"); 
       //fields=t.getClass().getDeclaredFields();  
       test_id[j]=test_list.get(j).getId(); 
       System.out.println("test_id: is "+test_id[j]); 
      } 
      System.out.println("Size of test_id array is "+test_id.length); 
      for(int j=0;j<test_id.length;j++) 
      { 
       System.out.println("test_id now is "+j); 
       int studentid=98; 
       String sqlquery="SELECT ST.ID id,ST.STUDENT_ID student_id,ST.attemptNumber attempt_Number,ST.TEST_ID testId,ST.TEST_ID student_test_id,T.description description,T.test_duration test_duration,T.test_score Test_Score,ST.SCHEDULED_START_TIME_BEGIN scheduled_start_time_begin,ST.SCHEDULED_START_TIME_END scheduled_start_time_end,ST.TEST_START_DATE test_start_date,ST.LAST_ACTIVE_TIME test_last_active_time,ST.TEST_ATTEMPTED test_attempted,ST.ALLOW_VIEW_REPORT allow_view_report FROM student_test as ST,test as T WHERE ST.PAYMENT_STATUS='A' AND ST.TEST_ID = T.id AND ST.STUDENT_ID='"+studentid+"' AND ST.TEST_ATTEMPTED='0' AND T.id='"+test_id[j]+"' ORDER BY ST.ID"; 
       Class.forName("com.mysql.jdbc.Driver"); 
       java.sql.Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/school_db","root",""); 
       Statement pst=con.createStatement(); 
       rs=pst.executeQuery(sqlquery); 
       while(rs.next()) 
       { 
        //JOptionPane.showMessageDialog(null, "Table me Agaaya"); 
        System.out.println("Number of rows:- "+rs.getRow()); 
        System.out.println(rs.toString()); 
       } 
      } 
      ses.getTransaction().commit(); 
      System.out.println("Test Data Retrieved"); 
      ses.close(); 
    } 
    catch(HibernateException e) 
    { 
     e.printStackTrace(); 
    } 
} 
} 

を助けてくださいこれは、uは、このデータを渡す方法を私に伝えることができ、それは今、すべてのデータを表示するSystem.out.printlnで私のコードとその作業罰金ではありませんjtableを使用して、dbutilsを使用しているときにその列を取得することもできますnは自動的に名前と私はjtablename.setmodel(dbutils.setresultsettotable(RS)を)知っている手動のJTableに

答えて

0

を列名を与えていないです。

まあDBUtilsは、単一のTableModelに

を作成しますが、JTableの中で私だけではない、最後のループレコードごとにループレコードあなたが新しいのTableModelと古いのTableModelを交換しておく

を取得しています。

だから、次の2つの選択肢があります。

  1. あなたが複数のテーブルを作成する必要があり、それぞれのTableModelに1つのフレームに

  2. をそれぞれのJTableを追加あなたがにそれぞれのTableModelからデータをコピーする必要がありますマスターTableModelを作成し、次にマスターTableModelを単一のJTableに表示します。つまり、ループの外側では、列名と0行のデータだけを持つDefaultTableModelを作成します。次に、それぞれのループを繰り返し、DBUtilsモデルの各行のデータをメインのTableModelにコピーします。ループ外でメインのTableModelを使用してテーブルを作成します。

編集:

コードの基本的な構造は、のようになります。

// Create an empty table model 

String[] columnNames = {"Column1", "Column2", ...}; 
DefaultTableModel groupModel = new DefaultTableModel(columnNames,0); 

... 

// loop for all the ids 

for(int j=0;j<test_id.length;j++) 
{ 
     ... 

    // copy the data from the idc table model to the group table model 

    DefaultTableModel idModel = DbUtils.resultSetToTableModel(rs); 

    for (int i = 0; I < idModel.getRowCount()) 
    { 
     Vector<Object> row = new Vector<Object>(); 

     for (int j = 0; j < rsModel.getColumnCount() 
      row.addElement(rsModel.getValueAt(i, j); 

     groupModel.addRow(row); 
    } 
} 

JTable table = new JTable(groupModel); 
+0

uが私が持っていたのjava –

+0

で新鮮だとして、それはたくさん私を助けるだろうコードを修正してくださいすることができます編集したコード私のコードを読んで助けてください –

+0

@LovePoet私たちはあなたのコードを書くのではありません。あなたは試して学びます。コードがどのように見えるかの一般的な構造を教えてくれました。時間をかけてTableModlesの仕組みを理解し、空白を埋めてください。 – camickr

関連する問題