Click here to Skip to main content
15,881,828 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Java
import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class JTableDatabase extends JFrame{
    public JTableDatabase() {
        Vector columnNames = new Vector();
        Vector data = new Vector();
        
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection connect =DriverManager.getConnection("jdbc:odbc:employeedsn");
            
            String sql = "Select * from emp";
            Statement stmt = connect.createStatement();
            ResultSet rs = stmt.executeQuery( sql );
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
            for (int i = 1; i <= columns; i++) {
                columnNames.addElement( md.getColumnName(i) );
            }
            //my Problem start here
            while (rs.next()) {
                Vector row = new Vector(columns);
                for (int i = 1; i <= columns; i++) {
                    row.addElement( rs.getObject(i) );
                }
                data.addElement( row );
            }
            //and ends here
            rs.close();
            stmt.close();
        }
        catch(Exception e) {
            System.out.println( e );
        }
        JTable table = new JTable(data, columnNames);
        
        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
        
        JPanel buttonPanel = new JPanel();
        getContentPane().add( buttonPanel, BorderLayout.SOUTH );
    }

    public static void main(String[] args) {
    
        JTableDatabase frame = new JTableDatabase();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setTitle("Information Table");
        frame.setVisible(true);
        }
}


I Just want to know what is going in while loop.
Please help me in solving my problem
Thanx in advance
Posted
Updated 5-Jun-13 21:42pm
v2
Comments
Ron Beyer 5-Jun-13 23:05pm    
Maybe you can tell us what problem you are having, instead of "its here" and "tell me what is going on"
Shubhashish_Mandal 6-Jun-13 3:41am    
Sorry, unable to read your brain,please let us know the problem???
Richard MacCutchan 6-Jun-13 3:43am    
Step through your code with the debugger and you will be able to see what is going on.
Anoop Kr Sharma 6-Jun-13 6:20am    
I actually want to know why columns is passing in vector having name row in loop.
// Vector row = new Vector(columns);
another is what is the use of rs.getobject(i)
actually I am new to java programming

1 solution

it's simple ,
1- the argument of Vector class (here is columns) is initial capacity of your vector and
according to Oracle docs its "Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero."
So the capacity of your vector must be equal to number of your columns (imagine the vector as a row in database table).
2- Resultset is something(!) that points to current row in database table and getobject() method gets the value of the designated column (here is i) in the current row of this ResultSet object (rs) as an Object .
 
Share this answer
 
Comments
Anoop Kr Sharma 7-Jun-13 22:52pm    
thanx Farzad Nozarian

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900