This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes displaying data from JDBC to JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "displaying data from JDBC to JTable" Watch "displaying data from JDBC to JTable" New topic
Author

displaying data from JDBC to JTable

Ruel Soriano
Greenhorn

Joined: May 17, 2007
Posts: 12
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

Joined: May 26, 2003
Posts: 31585
    
185

"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


[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31585
    
185

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

Joined: May 17, 2007
Posts: 12
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

Joined: May 26, 2003
Posts: 31585
    
185

"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.
 
 
subject: displaying data from JDBC to JTable