Discussions

General J2EE: Database data in JTable

  1. Database data in JTable (6 messages)


    I am trying to display data from database into the JTable swing component. I can see the table and the heading coming from the vector. I cannot see why the data from the databse table is not loaded on to the Jtable. I just get a table with 6 rows blank.

    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    import java.sql.*;
    import java.util.*;

    public class dataTable extends JApplet
    {
    String dbdriver="jdbc:oracle:thin:@ds1.ctateu.edu:1521:wpac";
    String dbuser="system";
    String dbpass="pass";
    Container c;
    JScrollPane jsp;
    JTable table;
    int v,h;
    Connection connection;
    Statement stmt;
    ResultSet rs;
    String param_start,param_end, param_disp_start, param_disp_end,param_shortdesc, param_longdesc;

    public void init()
    {
    c= getContentPane();
    c.setLayout (new BorderLayout());
    v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
    h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;

    Vector heading = new Vector();
    heading.addElement("Event Start Date");
    heading.addElement("Event End Date");
    heading.addElement("Display Start Date");
    heading.addElement("Display End Date");
    heading.addElement("Short Description");
    heading.addElement("Long Description");
    Vector data = new Vector();

    try
    {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    connection=DriverManager.getConnection(dbdriver,dbuser,dbpass);
    stmt=connection.createStatement();
    rs= stmt.executeQuery("select * from univevents");

    while(rs.next())
    {
    param_start = rs.getString("start_date");
    param_end = rs.getString("end_date");
    param_disp_start = rs.getString("display_start");
    param_disp_end =rs.getString("display_end");
    param_shortdesc = rs.getString("short_desc");
    param_longdesc = rs.getString("long_desc");
    }
    }
    catch (Exception e)
    {
    //JOptionPane.showMessageDialogue("Exception: "+e.getMessage());
    }

    data.addElement(param_start);
    data.addElement(param_end);
    data.addElement(param_disp_start);
    data.addElement(param_disp_end);
    data.addElement(param_shortdesc);
    data.addElement(param_longdesc);

    table = new JTable(data, heading);
    jsp = new JScrollPane(table,v,h);
    //Add scrollpane to container
    c.add("Center",jsp);
      }//init

    }//JApplet

    Threaded Messages (6)

  2. Database data in JTable[ Go to top ]

    I guess the model you are sending to table is just a vector, but it needs vector of vectors. probably the following code may solve this
    <code>
    while(rs.next()) {
        param_start = rs.getString("start_date");
        param_end = rs.getString("end_date");
        param_disp_start = rs.getString("display_start");
        param_disp_end =rs.getString("display_end");
        param_shortdesc = rs.getString("short_desc");
        param_longdesc = rs.getString("long_desc");

        //create a row
        Vector tmp = new Vector();
        tmp.addElement(param_start);
        tmp.addElement(param_end);
        tmp.addElement(param_disp_start);
        tmp.addElement(param_disp_end);
        tmp.addElement(param_shortdesc);
        tmp.addElement(param_longdesc);
        
        //add to model
        data.addElement(tmp);
    }
    table = new JTable(data, heading);
    </code>
  3. Database data in JTable[ Go to top ]

    Tried that. For some reason it is not displaying any records from the database. The table is also greyed meaning doesnt show the rows in white like it should ?
  4. Database data in JTable[ Go to top ]

    Can JApplet having JTable access database directly?
  5. Database data in JTable[ Go to top ]

    Maybe u can consider having the applet taking to a service on the server which accesses the database. For it is unlikely that the server and port which served your applet is the same on which the database is listening. Thus you might be breaking the applet sandbox and hence u cannot connect.

  6. JTable[ Go to top ]

    While I m doing about Jtable data can't be added in Jtable.

    here is the code of data accessing from database

    //////////////////////////////////////////////////////////////////////////////////////////
    public static Vector getSalaryReport(String month, String year)throws Exception{

            Statement st=null;
            ResultSet res=null;
            String query="SELECT employee.emp_name, employee.emp_rank, employee.emp_joining, salary.amount, salary.deduct_1, salary.deduct_2, salary.deduct_3, salary.net_amount, salary.date_time, salary.month, salary.year FROM employee INNER JOIN salary ON employee.emp_id = salary.emp_id WHERE (((salary.month)='"+month+"') AND ((salary.year)='"+year+"'));";

            System.out.println(query);
            try{
              st=con.createStatement();
              res=st.executeQuery(query);
             Vector v=new Vector();
             Vector record=new Vector();
              while(res.next()){
                SalaryReportRecord rec=new SalaryReportRecord();
                //rec.setEmp_id(res.getInt("emp_id"));
               /* rec.setEmp_name(res.getString("emp_name"));
                rec.setEmp_rank(res.getString("emp_rank"));
                rec.setEmp_joining(res.getString("emp_joining"));
                rec.setAmount(res.getString("amount"));
                rec.setDeduct_1(res.getString("deduct_1"));
                rec.setDeduct_2(res.getString("deduct_2"));
                rec.setDeduct_3(res.getString("deduct_3"));
                rec.setNet_amount(res.getString("net_amount"));
                rec.setDate_time(res.getString("date_time"));
                */
               int i=0;


                String name=res.getString("emp_name");
                String rank=res.getString("emp_rank");
                String join=res.getString("emp_joining");
                String amount=res.getString("amount");
                String ded1=res.getString("deduct_1");
                String ded2=res.getString("deduct_2");
                String ded3=res.getString("deduct_3");
                String net=res.getString("net_amount");
                String date=res.getString("date_time");
                System.out.println(name+":"+rank+":"+join+":"+amount+":"+ded1+":"+ded2+":"+ded3+":"+net+":"+date);
                record.addElement(i);
                record.addElement(name);
                record.addElement(rank);
                record.addElement(join);
                record.addElement(amount);
                record.addElement(ded1);
                record.addElement(ded2);
                record.addElement(ded3);
                record.addElement(net);
                record.addElement(date);
                record.addElement("");

                v.addElement(record);
                i++;
              }

              return v;
            }finally{
              if(res!=null) res.close();
              if(st!=null) st.close();
        }//end finally


      }

    ///////////////////////////////////////////////////////////////////////////////////////////////////

    code of add this Vector into Jtable is

     private void getReportTable(){
        String month=(String)this.monthComboBox.getSelectedItem();
        String year=(String)this.yearjComboBox1.getSelectedItem();
        System.out.println(month);
        try{
          Vector  v=DatabaseManager.getSalaryReport(month,year);
          String salAttrib[]= {"S.No","Emp: Name","Rank","Date Joining","Amount","Deduct_1","Deduct_2","Deduct_3","Net Amount","Date/time","Signature"};
          Vector columnHeads = new Vector();
          for( int i = 0; i <salAttrib.length ; i++ )
            columnHeads.addElement(salAttrib[i]);
        
          this.table=new JTable(v,columnHeads);

          JScrollPane p=new JScrollPane(this.table);
          p.setBounds(50,200,900,650);
        this.add(p);

        }catch(Exception e){
          e.printStackTrace();
          JOptionPane.showMessageDialog(this,"Error:"+e.getMessage());
        }
      }//end getReport Table

  7. JTable[ Go to top ]

    While I m doing about Jtable data can't be added in Jtable.

    here is the code of data accessing from database

    //////////////////////////////////////////////////////////////////////////////////////////
    public static Vector getSalaryReport(String month, String year)throws Exception{

            Statement st=null;
            ResultSet res=null;
            String query="SELECT employee.emp_name, employee.emp_rank, employee.emp_joining, salary.amount, salary.deduct_1, salary.deduct_2, salary.deduct_3, salary.net_amount, salary.date_time, salary.month, salary.year FROM employee INNER JOIN salary ON employee.emp_id = salary.emp_id WHERE (((salary.month)='"+month+"') AND ((salary.year)='"+year+"'));";

            System.out.println(query);
            try{
              st=con.createStatement();
              res=st.executeQuery(query);
             Vector v=new Vector();
             Vector record=new Vector();
              while(res.next()){
                SalaryReportRecord rec=new SalaryReportRecord();
                //rec.setEmp_id(res.getInt("emp_id"));
               /* rec.setEmp_name(res.getString("emp_name"));
                rec.setEmp_rank(res.getString("emp_rank"));
                rec.setEmp_joining(res.getString("emp_joining"));
                rec.setAmount(res.getString("amount"));
                rec.setDeduct_1(res.getString("deduct_1"));
                rec.setDeduct_2(res.getString("deduct_2"));
                rec.setDeduct_3(res.getString("deduct_3"));
                rec.setNet_amount(res.getString("net_amount"));
                rec.setDate_time(res.getString("date_time"));
                */
               int i=0;


                String name=res.getString("emp_name");
                String rank=res.getString("emp_rank");
                String join=res.getString("emp_joining");
                String amount=res.getString("amount");
                String ded1=res.getString("deduct_1");
                String ded2=res.getString("deduct_2");
                String ded3=res.getString("deduct_3");
                String net=res.getString("net_amount");
                String date=res.getString("date_time");
                System.out.println(name+":"+rank+":"+join+":"+amount+":"+ded1+":"+ded2+":"+ded3+":"+net+":"+date);
                record.addElement(i);
                record.addElement(name);
                record.addElement(rank);
                record.addElement(join);
                record.addElement(amount);
                record.addElement(ded1);
                record.addElement(ded2);
                record.addElement(ded3);
                record.addElement(net);
                record.addElement(date);
                record.addElement("");

                v.addElement(record);
                i++;
              }

              return v;
            }finally{
              if(res!=null) res.close();
              if(st!=null) st.close();
        }//end finally


      }

    ///////////////////////////////////////////////////////////////////////////////////////////////////

    code of add this Vector into Jtable is

     private void getReportTable(){
        String month=(String)this.monthComboBox.getSelectedItem();
        String year=(String)this.yearjComboBox1.getSelectedItem();
        System.out.println(month);
        try{
          Vector  v=DatabaseManager.getSalaryReport(month,year);
          String salAttrib[]= {"S.No","Emp: Name","Rank","Date Joining","Amount","Deduct_1","Deduct_2","Deduct_3","Net Amount","Date/time","Signature"};
          Vector columnHeads = new Vector();
          for( int i = 0; i <salAttrib.length ; i++ )
            columnHeads.addElement(salAttrib[i]);
        
          this.table=new JTable(v,columnHeads);

          JScrollPane p=new JScrollPane(this.table);
          p.setBounds(50,200,900,650);
        this.add(p);

        }catch(Exception e){
          e.printStackTrace();
          JOptionPane.showMessageDialog(this,"Error:"+e.getMessage());
        }
      }//end getReport Table

     

     

    please its urgent to work