
Hi.. I am doing a project on Inventory Management System for which i created the user interfaces.. I have a user interface in which i have used JTables.. Now my problem is I dont know how to show the Jtable which takes input from oracle table(my database table..) This is a very important table of my project.. Please help me out..

Hi Friend,
Try the following code:
import java.io.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
class JTableDatabase extends JFrame{
ResultSet rs;
JTableDatabase(){
final Vector columnNames = new Vector();
final Vector data = new Vector();
JPanel panel=new JPanel();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = con.createStatement();
ResultSet rs= st.executeQuery("Select * from employee");
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 );
}
}
catch(Exception e){}
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
add(panel);
}
public static void main(String arg[])
{
try
{
JTableDatabase frame=new JTableDatabase();
frame.setSize(550,200);
frame.setVisible(true);
}
catch(Exception e)
{}
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.