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

public class JTableDatabase {
public static void main(String[] args) {
Vector columnNames = new Vector();
Vector data = new Vector();
JPanel p=new JPanel();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:access");
String sql = "Select * from data";
Statement stmt = con.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) );
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++){
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
rs.close();
stmt.close();
}
catch(Exception e){
System.out.println(e);
}
JTable table = new JTable(data, columnNames);
TableColumn col;
for (int i = 0; i < table.getColumnCount(); i++) {
col = table.getColumnModel().getColumn(i);
col.setMaxWidth(250);
}
JScrollPane scrollPane = new JScrollPane( table );
p.add( scrollPane );
JFrame f=new JFrame();
f.add(p);
f.setSize(600,400);
f.setVisible(true);
}
}

Thanks

Edited 2 Years Ago by Prabhat_1

you write the code that does so.
if your code fails, you post it here, and say what you're stuck with.

Prabhat_1: no doubt you meant well, but there are several reasons you shouldn't just post code here.

the rules each member here agreed on ( http://www.daniweb.com/community/rules ) requires us to (when we post a question) at least to provide some proof that we actually put in an effort for ourselves.

give the OP the chance to learn, not just to copy paste a "working" solution.
sure, this will work. but using Vector? (just a for instance).
your code is far from ideal. not only will the OP learn in a way that he literally just learns this code by heart, without truly understanding what it does, but he'll learn the flaws in the code as being "good code".

Prabhet:
I'm sure you intended to help, but here we try to teach people the knowledge and skills they need. We don't just do their homework for them.
If you do want to post code here as an example or solution you should be careful that is is a good example, not just a load of uncommented unindented code based on an obsolete version of Java, otherwize you may find that it attracts a lot of criticism!

This article has been dead for over six months. Start a new discussion instead.