How to update a Jtable with new data
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ritesh_r
Posted On:   Wednesday, August 8, 2001 10:33 AM

Hi All: I am including my source code.I am unable to update my Jtable with new data populated from Vectors. Let me know where am I wrong. //Declare the Packages going to be imported here below import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.table.*; import java.util.*; import java.sql.*; // public class JApplicationSql extends JFrame implements ItemListener { //Initialize the Components DefaultTableModel defaulttablemodel;// = new DefaultTableModel(); JTable jtable ;//= new JTable(defaulttablemodel);//A Dynamic Modifiable JTable Vector row,column,row_1,column_1;//No. of r   More>>

Hi All:

I am including my source code.I am unable to update my Jtable with new data populated from Vectors.


Let me know where am I wrong.

//Declare the Packages going to be imported here below
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;

//

public class JApplicationSql extends JFrame implements ItemListener
{
//Initialize the Components
DefaultTableModel defaulttablemodel;// = new DefaultTableModel();
JTable jtable ;//= new JTable(defaulttablemodel);//A Dynamic Modifiable JTable
Vector row,column,row_1,column_1;//No. of rows and columns in a JTable
JLabel label_combo=new JLabel();
JComboBox comboBox =new JComboBox();
JPanel jpanel;
//comboBox.setEditable(true);

//Declare the Container1
Container contentPane;

//Database Declarations
//Connection connection;
//String dbUrl = "jdbc:odbc:mydsn";
//String user = "sa";
//String password = "";

private Statement stmt;
private ResultSet rs;
private ResultSetMetaData rmeta;
String combo_value="";
//String row_value="";


//Initialization of Constructor
public JApplicationSql()
{
jpanel = new JPanel();

label_combo.setText("Select the Department");
label_combo.setFont(new Font("Verdana",1,17));

comboBox.addItem(" 10 ");
comboBox.addItem(" 20 ");
comboBox.addItem(" 30 ");
comboBox.addItem(" 40 ");
comboBox.addItem(" 50 ");

//contentPane.setLayout(new FlowLayout());
jpanel.add(label_combo);
jpanel.add(comboBox);

//Add the Listeners
comboBox.addItemListener(this);

contentPane = getContentPane();
contentPane.add(jpanel,BorderLayout.NORTH);

//Load the Drivers and Connection Objects and Establish the connection
try
{
System.out.println("Locating Drivers");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connecting to Database");
//connection = DriverManager.getConnection(dbUrl,user,password);
Connection connection = DriverManager.getConnection("jdbc:odbc:mydsn;UID=sa;PWD=");
System.out.println("Got Connection");
stmt = connection.createStatement();
}
catch (Exception sqlex)
{
System.out.println("Unable to Connect!!");
sqlex.printStackTrace();
}

}
public static void main(String args[])
{
try
{
JApplicationSql app=new JApplicationSql();
app.setSize(500,350);
app.setVisible(true);
}catch(Exception e)
{}
}


{
try
{
if (e.getStateChange()==ItemEvent.SELECTED)
{
combo_value=(String)comboBox.getSelectedItem();
populate(combo_value);
jtable.repaint();


/* combo_value=(String)comboBox.getSelectedItem();
//int index=(int)comboBox.getSelectedIndex();
System.out.println("Value of the combo is "+combo_value);
String sql_query="SELECT A.DeptId,A.EmpId,A.Name,A.Designation, B.DeptName FROM EmployeeDetails A,Master_Department B where A.DeptId = B.DeptId and A.DeptId ="+combo_value;
//The Resultset Based Sql Query
//rs = stmt.executeQuery("SELECT EmployeeDetails.EmpId,EmployeeDetails.Name,EmployeeDetails.Designation, EmployeeDetails.DeptId,Master_Department.DeptName FROM EmployeeDetails INNER JOIN Master_Department ON EmployeeDetails.DeptId = 'index'");
rs=stmt.executeQuery(sql_query);

rmeta=rs.getMetaData();

//Retrieve the number of columns and rows and add the number to Vectors
column_1= retrieveColumns();

//Row Data
row_1=retrieveRows();
System.out.println(row_1.size());

//Display the ResultSet data in a JTable
DefaultTableModel defaulttablemodel =new DefaultTableModel(row_1,column_1);
jtable =new JTable(defaulttablemodel);
JScrollPane scrolltab=new JScrollPane(jtable);

//jpanel.add(scrolltab);
contentPane.add(scrolltab,BorderLayout.CENTER);
//contentPane.add(jpanel,BorderLayout.CENTER);
//contentPane.validate(true);
jtable.repaint();*/
}
}
catch(Exception SqlE)
{
System.out.println("Error in retrieving");
}
}

//The Column Data
public Vector retrieveColumns()
{
column=new Vector();
try
{
for(int i=1;i <=rmeta.getColumnCount();i++)
column.addElement(rmeta.getColumnName(i));
//System.out.println(column.size());
}
catch(Exception e)
{
System.out.println("The column Exception is :"+e);
}
return column;
}

//The Row Data
public Vector retrieveRows()
{
row=new Vector();
try
{
while(rs.next())
{

Vector currow=new Vector();//Current Row
for(int i=1;i <=rmeta.getColumnCount();i++)
currow.addElement(rs.getString(i));
System.out.println(currow.size());
row.addElement(currow);
}
}
catch(SQLException se)
{
System.out.println("The Exception is :"+se);
}
return row;
}
public void populate(String val)
{
try
{
System.out.println("Value of the combo is "+val);
String sql_query="SELECT A.DeptId,A.EmpId,A.Name,A.Designation, B.DeptName FROM EmployeeDetails A,Master_Department B where A.DeptId = B.DeptId and A.DeptId ="+val;

//The Resultset Based Sql Query
//rs = stmt.executeQuery("SELECT EmployeeDetails.EmpId,EmployeeDetails.Name,EmployeeDetails.Designation, EmployeeDetails.DeptId,Master_Department.DeptName FROM EmployeeDetails INNER JOIN Master_Department ON EmployeeDetails.DeptId = 'index'");
rs=stmt.executeQuery(sql_query);

rmeta=rs.getMetaData();

//Retrieve the number of columns and rows and add the number to Vectors
column_1= retrieveColumns();

//Row Data
row_1=retrieveRows();


//Display the ResultSet data in a JTable
defaulttablemodel =new DefaultTableModel(row_1,column_1);
jtable =new JTable(defaulttablemodel);
defaulttablemodel.addTableModelListener(jtable);

JScrollPane scrolltab=new JScrollPane(jtable);
jtable.setPreferredScrollableViewportSize(new Dimension(500, 70));
jpanel.add(scrolltab);

contentPane.add(scrolltab,BorderLayout.CENTER);
//jpanel.repaint();
//jtable.repaint();
//contentPane.add(jpanel,BorderLayout.CENTER);
//contentPane.validate(true);

}
catch(Exception eop){System.out.println(eop);}
}
}
>>>>>>>>>>>

Thanking you

regards
Ritesh

   <<Less
About | Sitemap | Contact