hi,
Im just wondering on how I could get a columns and a record count I mean the whole thing in a database aside from executing this statement "select count(*) from myDatabase" ...is there another statement that i can use instead of this one coz it doesnt retrieve any columns just the record count...thanks

Recommended Answers

All 7 Replies

Read the Javadocs of the classes ResultSetMetaData and DatabaseMetaData for more details regarding your database/table etc. A simple web search for these classes might give you sample snippets on how to go about using them.

First this could be helpful:

select count(*), col_1, col_2 
from myDatabase

Each "row" of the ResultSet will have the row count as well as the columns.

Also you can run 2 queries.
The first that will get you the row count
The second that will get you the columns


But the above suggestions aren't very good.


IF you want both the row count and the columns, I assume that you want the row count to create an array and then put inside the array the data from the table. Since you are using an array you need to know exactly how many rows will be returned before creating it. Right?

If that is the case, use the java.util.Vector. It has methods for adding and getting elements. It doesn't have a fixed size like the array. You can keep adding elements to it without knowing the number of rows the query returned

First this could be helpful:

select count(*), col_1, col_2 
from myDatabase

Each "row" of the ResultSet will have the row count as well as the columns.

Also you can run 2 queries.
The first that will get you the row count
The second that will get you the columns


But the above suggestions aren't very good.


IF you want both the row count and the columns, I assume that you want the row count to create an array and then put inside the array the data from the table. Since you are using an array you need to know exactly how many rows will be returned before creating it. Right?

If that is the case, use the java.util.Vector. It has methods for adding and getting elements. It doesn't have a fixed size like the array. You can keep adding elements to it without knowing the number of rows the query returned

yeah i want them to put in an array, actually i want those data from the database to be display in the JTable, i have 8 columns instead of using 8 vectors i just want to put it in a 2 dimensional Object array, thats why i want to know the record count and also the row and the col from the database...the changes everytime ill search for a record......

yeah i want them to put in an array, actually i want those data from the database to be display in the JTable, i have 8 columns instead of using 8 vectors i just want to put it in a 2 dimensional Object array, thats why i want to know the record count and also the row and the col from the database...the changes everytime ill search for a record......

You won't need 8 Vectors, but One. Create an Object that represents each row/record and put that in the vector

You won't need 8 Vectors, but One. Create an Object that represents each row/record and put that in the vector

honestly i cant picture out wat u want to emphasized, actually i am just a newbie in here so sori if im so slow in getting wat u up to...

i have 8 data in a row, so u want me create an array of Object and then put the data in it then convert it to a Vector? am i right?...

Object[] obj = new Object[8];
Vector v = new Vector();
while (rst.next()) {
        for (int x = 0 ; x < obj.length() ; x ++) {
                obj[x] = rst.getObject[x + 1];
        }
 v = (Vector) obj; 
}

if i am wrong, can u post some example code...thank u...

Have you looked the API of Vector?
I assume you didn't because if you did you would have found this method:
Vector.add

Also you need to create a class:

class ClassA {
  private String a = null;
  private String b = null;
.......
}

with public get,set methods.
Then for each row/loop of the database you create a new object of that class and you put that in the vector.

Vector v = new Vector();
while (rst.next()) {
        ClassA cla = new ClassA();
        cla.setMethod...(rst.get....(1));
        cla.setMethod...(rst.get....(2)); //other method

        v.add(cla);
}

Have you looked the API of Vector?
I assume you didn't because if you did you would have found this method:
Vector.add

Also you need to create a class:

class ClassA {
  private String a = null;
  private String b = null;
.......
}

with public get,set methods.
Then for each row/loop of the database you create a new object of that class and you put that in the vector.

Vector v = new Vector();
while (rst.next()) {
        ClassA cla = new ClassA();
        cla.setMethod...(rst.get....(1));
        cla.setMethod...(rst.get....(2)); //other method

        v.add(cla);
}

ok i get it now, tanx by the way...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.