Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

displaying data from JDBC to JTable

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check this code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.util.*;
import javax.swing.JScrollPane;
import javax.swing.border.*;


public class TestingAgain extends JFrame{
Connection con;
Vector columnName, data;
JTable myTable;
JScrollPane myPane;
JButton select;

/** Creates a new instance of TestingAgain */
public TestingAgain() {
JPanel panel = new JPanel();
select = new JButton("select");

try{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException cnfEx)
{
cnfEx.printStackTrace();
}
con = DriverManager.getConnection("jdbc:odbc:MS Access Database", "", "");
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery("select group,description from groupcode");

columnName = new Vector();
columnName.addElement("Group");
columnName.addElement("Description");

data = new Vector();
while(rs.next());
{
data.addElement(new Vector());
((Vector)data.lastElement()).addElement(rs.getString(1));
((Vector)data.lastElement()).addElement(rs.getString(2));
}
rs.close();
stat.close();
con.close();
}
catch (SQLException sqlEx)
{
sqlEx.printStackTrace();
JOptionPane.showMessageDialog(null,"Error loading table.\nPlease check setup");
}
myTable = new JTable(data, columnName);
myPane = new JScrollPane(myTable);
Border etchedBorder = BorderFactory.createEtchedBorder();
Border titleBorder = BorderFactory.createTitledBorder(etchedBorder,"Group Code");
myPane.setBorder(titleBorder);
panel.add(myPane);
panel.add(select);

panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
getContentPane().add(panel);
pack();
setVisible(true);
}
public static void main(String[]agrs)
{
new TestingAgain();
}
}

i have a problem getting the data in my JTable it display the JOPtion...i think i have error in the part:
data.addElement(new Vector());
((Vector)data.lastElement()).addElement(rs.getString(1));
((Vector)data.lastElement()).addElement(rs.getString(2));
can someone get me a new code to run this program...
I would appreciate it very much......
 
author & internet detective
Posts: 41184
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"rule_S",

We're pleased to have you here with us here at JavaRanch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks,
Jeanne
Forum Bartender
 
Jeanne Boyarsky
author & internet detective
Posts: 41184
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ruel Soriano:
i have a problem getting the data in my JTable it display the JOPtion...i think i have error in the part:


What error did you get?
 
Ruel Soriano
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in this part i got errors, it says invalid cursor state.....
and these are the codes were i got it....

data.addElement(new Vector());
((Vector)data.lastElement()).addElement(rs.getString(1));
((Vector)data.lastElement()).addElement(rs.getString(2));
can someone get me a new code to run this program...
I would appreciate it very much......
 
Jeanne Boyarsky
author & internet detective
Posts: 41184
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Ruel",
Thank you for changing your display name. Unfortunately, it still does not follow our naming conventions. We require both a first and last name. Right now, you only have one. Please update your display name.
 
reply
    Bookmark Topic Watch Topic
  • New Topic