Program to show data from database in JTable

import java.sql.*;
import javax.swing.*;
import java.awt.*;
class showdata extends JFrame
{
JTable jt;
JScrollPane sp;
showdata()
{
try
{
//This access database file must exist in the current directory
String data[][]={ {“”,””,””,””},
{“”,””,””,””},
{“”,””,””,””},
{“”,””,””,””},
{“”,””,””,””},
{“”,””,””,””}
};
String column[]={“ID”,”Roll NO”, “Student Name”,”Result”};
String url=”jdbc:odbc:myproject”;

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection c=DriverManager.getConnection(url);
Statement st=c.createStatement();
ResultSet rs=st.executeQuery(“select * from stu”);
int r=0;
while(rs.next())
{
data[r][0]=String.valueOf(rs.getInt(1));
data[r][1]=String.valueOf(rs.getInt(2));
data[r][2]=rs.getString(3);
data[r][3]=rs.getString(4);
r=r+1;
}

jt=new JTable(data,column);
jt.setBounds(30,40,200,300);
sp=new JScrollPane(jt);
getContentPane().add(sp);
setSize(300,400);
setVisible(true);
}
catch(Exception ee)
{
System.out.println(ee);
}
}
}

class exampledsn
{
public static void main(String ar[])
{
showdata s1=new showdata();
}
}

Author: Vikas Kansal

Hi.. I am Vikas Kansal. I am a "Computer Faulty" and a "Software & Web developer". I am an active member of A Social and Educational Organization NIRMAN.

Leave a Reply

Your email address will not be published. Required fields are marked *