0

I am trying to display data from a database in a JTable.

This is part of my ActionListener of my main class...

if(e.getSource()==jjb)
{
    String j[] = null;
    Vector r = new Vector();
    Vector finl = new Vector();
    try
    {
        d10.connection();
        int b = d10.getclmncnt();
        for(int g = 1; g<=b+1;g++)
        {
            j = new String[g];

        }
        for(int g = 1; g<=b;g++)
        {
            j[g] = d10.gettableclmn(g);
        }
        Vector v = new Vector();
        for(int g = 1; g<=b;g++)
        {
            v.addElement(j[g]);
        }
        int a = d10.getrwcnt();
        System.out.println("no of rows are   ::::  "+a);

        r = d10.getalldata(b);

        JTable t = new JTable(r,v);
        t.setLayout(null);
        TableColumn tc;
        for(int hh = 0;hh<t.getColumnCount();hh++)
        {               
            tc = t.getColumnModel().getColumn(hh);
            tc.setWidth(75);
        }
        JScrollPane js = new JScrollPane(t);

        js.setBounds(10, 100, 1000, 500);
        p5.add(js);
        p5.repaint();
    }
    catch (ClassNotFoundException e1)
    {
        e1.printStackTrace();
    }
    catch (SQLException e1)
    {
        e1.printStackTrace();
    }
}

This is my class for connection with the database...

public void connection()throws SQLException, ClassNotFoundException
{
    Class.forName("oracle.jdbc.driver.OracleDriver");
    System.out.println("Driver Loaded");
    Properties p = new Properties();
    p.setProperty("user","system");
    p.setProperty("password", "oracle10");
    p.setProperty("url","jdbc:oracle:thin:@Suneel-PC:1521:XE");
    c = DriverManager.getConnection(p.getProperty("url"),p.getProperty("user"),p.getProperty("password"));
    System.out.println("connected");
}

public String gettableclmn(int b2)
{
    String s = null;
    int fh = b2;
    try
    {
        Statement ss = c.createStatement();
        ResultSet rs = ss.executeQuery("select * from data");
        ResultSetMetaData rsmd = rs.getMetaData();
        s = rsmd.getColumnName(fh);
        System.out.println("columns names are     "+rsmd.getColumnName(fh));        
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }

    return s;
}

public int getclmncnt()
{
    int h = 0;

    ResultSetMetaData rsmd;
    try {
        Statement ss = c.createStatement();
        ResultSet rs = ss.executeQuery("select * from data");
        rsmd = rs.getMetaData();
        h = rsmd.getColumnCount();
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }

    return h;
}

public Vector getalldata(int b)
{
    int d = b;
    String j = null;
    Vector rr  = new Vector();
    Vector v = new Vector(d);
    try
    {
        Statement s = c.createStatement();
        ResultSet r = s.executeQuery("select * from data");

        while(r.next())
        {
            v.add(r.getInt(1));
            v.add(r.getInt(2));
            v.add(r.getInt(3));
            v.add(r.getInt(4));
            v.add(r.getInt(5));
            v.add(r.getInt(6));
            v.add(r.getInt(7));
            v.add(r.getInt(8));
            v.add(r.getInt(9));
            v.add(r.getInt(10));
            v.add(r.getInt(11));
            v.add(r.getString(12));
        }
        rr.add(v);
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }

    return rr;
}

public int getrwcnt()
{
    ResultSet r;
    int j = 0;
    try
    {
        Statement s = c.createStatement();
        r = s.executeQuery("select count (*) from data");
        r.next();
        j = r.getInt(1);
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }

    return j;
}

When I run this code, only the first result is shown in the JTable, however when I call function d10.getrwcnt(); it is giving me output as 79 rows. What is the problem with my code?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
shounak
  • 51
  • 2
  • 8
  • 2
    Can you put hard-coded data into the table dynamically? Can you successfully list the output from the DB (e.g. to the command line)? This seems like two separate problems. As an aside, for better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson May 30 '12 at 08:21

3 Answers3

1

Your issue is the loop of getalldata method.

Vector v = new Vector(d);
    try {

        Statement s = c.createStatement();
        ResultSet r = s.executeQuery("select * from data");

        while(r.next()) 
        //Every time when this loop gets executed you are adding
        //data to the same vector v, instead one instance depicts one row in database,
        //so you will have to create a new instance everytime.
        {
            v = new Vector(d); //create an instance for every row in database.
            v.add(r.getInt(1));
            v.add(r.getInt(2));
            v.add(r.getInt(3));
            v.add(r.getInt(4));
            v.add(r.getInt(5));
            v.add(r.getInt(6));
            v.add(r.getInt(7));
            v.add(r.getInt(8));
            v.add(r.getInt(9));
            v.add(r.getInt(10));
            v.add(r.getInt(11));
            v.add(r.getString(12));
            rr.add(v); // add the values here
        }



    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
mprabhat
  • 20,107
  • 7
  • 46
  • 63
  • take a look at [this](http://stackoverflow.com/questions/10620448/most-simple-code-to-populate-jtable-from-resultset/10623543#10623543) answer of mine – mprabhat May 31 '12 at 09:25
1

Move the first line and the last line,

    Vector v = new Vector(d);
    .... 
    while(r.next()) {
        v.add(r.getInt(1));
        v.add(r.getInt(2));
        ...
        v.add(r.getString(12));
    }

    rr.add(v);

inside the loop, as follows,

    while(r.next()) {
        Vector v = new Vector(d);
        v.add(r.getInt(1));
        v.add(r.getInt(2));
        ...
        v.add(r.getString(12));

        rr.add(v);
    }

Moreover, I can see your code is quite FUBAR, please don't mind it. You can do few things,

  • Deal with connection appropriately, try using some connection pooling API
  • Close your resultSet after use
  • Stop using Vector, use ArrayList instead, especially when Vector doesn't serve any purpose
  • Try to conform with Java Naming Convention
  • Use meaningful variables, not b, g, j etc..
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
  • i tried what you have suggested. but still it is not working properly.... previously only 1st line was added in the JTable. now the same line is repeating till the number of rows are occupied. – shounak May 31 '12 at 02:04
1

only points, because you didn‘t search something about follows

  • how Vector works

  • how JTable works

  • how LayotManagers works

  • read Related questions (right bottom)

  • dont reinvent the wheel, search for ResultSetTableModel or TableFromDatabase

  • you have issue with Concurency in Swing

mKorbel
  • 109,525
  • 20
  • 134
  • 319