Java program to read database into jtable JDBC ODBC
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.util.Vector;
import java.awt.event.*;
/*This program may show depricated error while compiling this is not a problem we can run it by using java EmpInform*/
public class EmpInform extends JFrame
{
Connection con;
JTable jtb;
Vector data;
Vector columns;
Vector row;
Container c;
public EmpInform()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:emp1"); //emp1 is the DSN name
Statement st=con.createStatement();
String query="select * from employee";//employee is the table name
ResultSet rs=st.executeQuery(query);
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
columns = new Vector(columnCount);
for(int i=1; i<=columnCount; i++)
{columns.add(md.getColumnName(i));
//debug, you can remove it
System.out.println(md.getColumnName(i));
}
Vector data = new Vector();
while (rs.next())
{
row = new Vector(columnCount);
import java.sql.*;
import java.awt.*;
import java.util.Vector;
import java.awt.event.*;
/*This program may show depricated error while compiling this is not a problem we can run it by using java EmpInform*/
public class EmpInform extends JFrame
{
Connection con;
JTable jtb;
Vector data;
Vector columns;
Vector row;
Container c;
public EmpInform()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:emp1"); //emp1 is the DSN name
Statement st=con.createStatement();
String query="select * from employee";//employee is the table name
ResultSet rs=st.executeQuery(query);
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
columns = new Vector(columnCount);
for(int i=1; i<=columnCount; i++)
{columns.add(md.getColumnName(i));
//debug, you can remove it
System.out.println(md.getColumnName(i));
}
Vector data = new Vector();
while (rs.next())
{
row = new Vector(columnCount);
for(int i=1; i<=columnCount; i++)
{
row.add(rs.getString(i));//store each column value
}
data.add(row); //store each row data to data vector
}
jtb = new JTable(data,columns);
rs.close();
con.close();
c=getContentPane();
c.setLayout(null);
//jtb.setBounds(10,30,300,600);/*if your table contain more data than this size you can either change it to higher size or simply remove it and also remove the above line c.setLayout(null);*/
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jp=new JScrollPane(jtb,v,h);/*use of scrollpane is good for display table correctly*/
jp.setBounds(10,30,300,600);/*for display in a specified position*/
c.add(jp);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public static void main(String args[])
{
EmpInform emp=new EmpInform();
emp.setVisible(true);
emp.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
emp.setSize(new Dimension(400,520));
}
}
No comments:
Post a Comment