please help me sort out SQLException problems

hello.

i have a problem trying to get JCheckBox to work with JTextArea. what i expected to happen when i would click on a check box is that data from a table in a MS Access database would be displayed in the text area.

the program below compiles OK. when i run it a frame appears with check boxes, a text area and a button inside it. the text area is smaller than it should be. but when i press one of the check boxes, the following SQLExceptions appear in the jGRASP console. how can i get rid of these strange errors?

-jGRASP exec: java ViewProductDetails

java.sql.SQLException: Invalid handle

at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)

at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)

at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)

at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)

at ViewProductDetails$1.actionPerformed(ViewProductDetails.java:79)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

-jGRASP: process aborted by user.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

publicclass ViewProductDetailsextends JFrame{

JPanel pnlBox, pnlBody, pnlFooter;

JCheckBox name;

JCheckBox ageCategory;

JCheckBox type;

JCheckBox year;

JButton returnToProductMenu;

JTextArea jta;

Container contentpane;

Connection db;

Statement statement;

public ViewProductDetails(){

super("View Product Details");

contentpane = getContentPane();

contentpane.setLayout(new BorderLayout());

pnlBox =new JPanel();

pnlBody =new JPanel();

pnlFooter =new JPanel();

jta =new JTextArea();

jta.setFont(new Font("Serif", Font.PLAIN, 12));

jta.setLineWrap(true);

jta.setWrapStyleWord(true);

jta.setEditable(false);

name =new JCheckBox("Name");

ageCategory =new JCheckBox("Age Category");

type =new JCheckBox("Type");

year =new JCheckBox("Year");

pnlBox.add(name);

pnlBox.add(ageCategory);

pnlBox.add(type);

pnlBox.add(year);

JScrollPane jsp =new JScrollPane(jta);

pnlBody.add(jsp, BorderLayout.CENTER);

returnToProductMenu =new JButton("Return To Product Menu");

pnlFooter.add(returnToProductMenu);

contentpane.add(pnlBox,BorderLayout.NORTH);

contentpane.add(pnlBody,BorderLayout.CENTER);

contentpane.add(pnlFooter,BorderLayout.SOUTH);

pack();

setLocationRelativeTo(null);

setVisible(true);

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}

catch(Exception e){

System.out.println("Problem loading the driver");

}

String url ="jdbc:odbc:VideoLibrary";

try{

db = DriverManager.getConnection(url,"","");

statement = db.createStatement();

DatabaseMetaData dbmd = db.getMetaData();

ResultSet rs = dbmd.getTables(null, null, null,new String[]{"TABLE"});

}

catch(Exception e){

System.out.println("Could not initialise the database");

e.printStackTrace();

}

name.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

try{

ResultSet rs1 = statement.executeQuery("SELECT * FROM Product ORDER BY name");

ResultSetMetaData rsmd1 = rs1.getMetaData();

for(int i = 1; i <= rsmd1.getColumnCount(); i++){

jta.append(rsmd1.getColumnName(i) +"");

}

jta.append("\n");

while(rs1.next()){

for(int i = 1; i <= rsmd1.getColumnCount(); i++){

jta.append(rs1.getObject(i) +"");

}

jta.append("\n");

}

}

catch(SQLException ea){

ea.printStackTrace();

}

}

});

ageCategory.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

try{

ResultSet rs2 = statement.executeQuery("SELECT * FROM Product ORDER BY ageCategory");

ResultSetMetaData rsmd2 = rs2.getMetaData();

for(int i = 1; i <= rsmd2.getColumnCount(); i++){

jta.append(rsmd2.getColumnName(i) +"");

}

jta.append("\n");

while(rs2.next()){

for(int i = 1; i <= rsmd2.getColumnCount(); i++){

jta.append(rs2.getObject(i) +"");

}

jta.append("\n");

}

}

catch(SQLException eb){

eb.printStackTrace();

}

}

});

type.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

try{

ResultSet rs3 = statement.executeQuery("SELECT * FROM Product ORDER BY type");

ResultSetMetaData rsmd3 = rs3.getMetaData();

for(int i = 1; i <= rsmd3.getColumnCount(); i++){

jta.append(rsmd3.getColumnName(i) +"");

}

jta.append("\n");

while(rs3.next()){

for(int i = 1; i <= rsmd3.getColumnCount(); i++){

jta.append(rs3.getObject(i) +"");

}

jta.append("\n");

}

}

catch(SQLException ec){

ec.printStackTrace();

}

}

});

year.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

try{

ResultSet rs4 = statement.executeQuery("SELECT * FROM Product ORDER BY year");

ResultSetMetaData rsmd4 = rs4.getMetaData();

for(int i = 1; i <= rsmd4.getColumnCount(); i++){

jta.append(rsmd4.getColumnName(i) +"");

}

jta.append("\n");

while(rs4.next()){

for(int i = 1; i <= rsmd4.getColumnCount(); i++){

jta.append(rs4.getObject(i) +"");

}

jta.append("\n");

}

}

catch(SQLException ed){

ed.printStackTrace();

}

}

});

returnToProductMenu.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

setVisible(false);

}

});

try{

if(statement !=null){

statement.close();

}

if(db !=null){

db.close();

}

}

catch(Exception e){

System.out.println("Could not close the current connection");

e.printStackTrace();

}

}

publicstaticvoid main(String[] args){

new ViewProductDetails();

}

}

[13723 byte] By [james-mcfaddena] at [2007-11-27 11:41:51]
# 1

> statement.close();

It appears to me that this line of code is executed before any of the queries are executed, as in:

> statement.executeQuery("SELECT * FROM Product ORDER BY year");

for example.

Similarly, your connection is closed before doing any queries.

> db.close();

warnerjaa at 2007-7-29 17:40:41 > top of Java-index,Java Essentials,New To Java...
# 2

The last thing you do in your constructor is Close the statement and connection objects.... you then try to perform queries using the statement object in your action listeners (which you have already closed)

It might be worth while to create a method to return a connection object which each action listener method can call.

i.e. your action listener calls for a conneciton object, creates a new statement, perfomrs the query and then in a finally{} block close the statement and connection

c0demonk3ya at 2007-7-29 17:40:41 > top of Java-index,Java Essentials,New To Java...
# 3

hello.

thanks for the reply. how can i modify the modified code below so that users of my video library system would be able to view CD/DVD/Game information by name, age category, type and year? the text area is not displaying in a proper size - i have to use the scroll pane to view the data in the text area. i should be able to view the data in the text area without a scrollpane because there is not much data in the database. how can i make the text area bigger?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

public class ViewProductDetails extends JFrame{

JPanel pnlBox, pnlBody, pnlFooter;

JCheckBox name;

JCheckBox ageCategory;

JCheckBox type;

JCheckBox year;

JButton returnToProductMenu;

JTextArea jta;

Container contentpane;

Connection db;

Statement statement;

public void makeConnection(){

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}

catch(Exception e){

System.out.println("Problem loading the driver");

}

}

public void setHostURL(){

String url = "jdbc:odbc:VideoLibrary";

closeDB();

try{

db = DriverManager.getConnection(url,"","");

statement = db.createStatement();

DatabaseMetaData dbmd = db.getMetaData();

ResultSet rs = dbmd.getTables(null, null, null, new String[]{"TABLE"});

}

catch(Exception e){

System.out.println("Could not initialise the database");

e.printStackTrace();

}

}

public void selectProductOne(){

try{

ResultSet rs1 = statement.executeQuery("SELECT * FROM Product ORDER BY name");

ResultSetMetaData rsmd1 = rs1.getMetaData();

for(int i = 1; i <= rsmd1.getColumnCount(); i++){

jta.append(rsmd1.getColumnName(i) + "");

}

jta.append("\n");

while(rs1.next()){

for(int i = 1; i <= rsmd1.getColumnCount(); i++){

jta.append(rs1.getObject(i) + "");

}

jta.append("\n");

}

}

catch(SQLException ea){

ea.printStackTrace();

}

}

public void selectProductTwo(){

try{

ResultSet rs2 = statement.executeQuery("SELECT * FROM Product ORDER BY ageCategory");

ResultSetMetaData rsmd2 = rs2.getMetaData();

for(int i = 1; i <= rsmd2.getColumnCount(); i++){

jta.append(rsmd2.getColumnName(i) + "");

}

jta.append("\n");

while(rs2.next()){

for(int i = 1; i <= rsmd2.getColumnCount(); i++){

jta.append(rs2.getObject(i) + "");

}

jta.append("\n");

}

}

catch(SQLException eb){

eb.printStackTrace();

}

}

public void selectProductThree(){

try{

ResultSet rs3 = statement.executeQuery("SELECT * FROM Product ORDER BY type");

ResultSetMetaData rsmd3 = rs3.getMetaData();

for(int i = 1; i <= rsmd3.getColumnCount(); i++){

jta.append(rsmd3.getColumnName(i) + "");

}

jta.append("\n");

while(rs3.next()){

for(int i = 1; i <= rsmd3.getColumnCount(); i++){

jta.append(rs3.getObject(i) + "");

}

jta.append("\n");

}

}

catch(SQLException ec){

ec.printStackTrace();

}

}

public void selectProductFour(){

try{

ResultSet rs4 = statement.executeQuery("SELECT * FROM Product ORDER BY year");

ResultSetMetaData rsmd4 = rs4.getMetaData();

for(int i = 1; i <= rsmd4.getColumnCount(); i++){

jta.append(rsmd4.getColumnName(i) + "");

}

jta.append("\n");

while(rs4.next()){

for(int i = 1; i <= rsmd4.getColumnCount(); i++){

jta.append(rs4.getObject(i) + "");

}

jta.append("\n");

}

}

catch(SQLException ed){

ed.printStackTrace();

}

}

public void closeDB(){

try{

if(statement != null){

statement.close();

}

if(db != null){

db.close();

}

}

catch(Exception e){

System.out.println("Could not close the current connection");

e.printStackTrace();

}

}

public ViewProductDetails(){

super("View Product Details");

contentpane = getContentPane();

contentpane.setLayout(new BorderLayout());

pnlBox = new JPanel();

pnlBody = new JPanel();

pnlFooter = new JPanel();

jta = new JTextArea();

jta.setFont(new Font("Serif", Font.PLAIN, 12));

jta.setLineWrap(true);

jta.setWrapStyleWord(true);

jta.setEditable(false);

name = new JCheckBox("Name");

ageCategory = new JCheckBox("Age Category");

type = new JCheckBox("Type");

year = new JCheckBox("Year");

pnlBox.add(name);

pnlBox.add(ageCategory);

pnlBox.add(type);

pnlBox.add(year);

JScrollPane jsp = new JScrollPane(jta);

pnlBody.add(jsp, BorderLayout.CENTER);

returnToProductMenu = new JButton("Return To Product Menu");

pnlFooter.add(returnToProductMenu);

contentpane.add(pnlBox,BorderLayout.NORTH);

contentpane.add(pnlBody,BorderLayout.CENTER);

contentpane.add(pnlFooter,BorderLayout.SOUTH);

pack();

setLocationRelativeTo(null);

setVisible(true);

name.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

makeConnection();

setHostURL();

selectProductOne();

closeDB();

}

});

ageCategory.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

makeConnection();

setHostURL();

selectProductTwo();

closeDB();

}

});

type.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

makeConnection();

setHostURL();

selectProductThree();

closeDB();

}

});

year.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

makeConnection();

setHostURL();

selectProductFour();

closeDB();

}

});

returnToProductMenu.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

setVisible(false);

}

});

}

public static void main(String[] args){

new ViewProductDetails();

}

}

james-mcfaddena at 2007-7-29 17:40:41 > top of Java-index,Java Essentials,New To Java...