Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:26:09+00:00 2026-06-04T18:26:09+00:00

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

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:26:11+00:00Added an answer on June 4, 2026 at 6:26 pm

    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..
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to display data from my database in a ListFragment. To do this,
I'm trying to display data from a database and I want to know what
i am trying to display data from a database to the text box and
I'am trying to display data from the database file which has the value Age
I'm trying to load data from database and display it in table like here:
I am trying to fetch data from my database and when I display it
I'm trying to display set of data which have retrieved from the sql database
I am trying to display data from my database to a Jsp page using
I am trying to fetch data from the following database data: And display the
I'm getting error Not unique table/alias while trying to display data from MySQL database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.