This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

displaying data from JDBC to JTable

 
Ruel Soriano
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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("jdbcdbc: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......
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic