Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL exception error

 
jin sun
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've just started learning JDBC and I'm having a big problem with my project. When I complie the class that contains all the JDBC, I get no errors, but when I try to complie the GUI I'm using with the JDBC class I get the error:
"ResultsInterface.java:69: unreported exception java.sql.SQLException; must be caught or declared to be thrown
race r1 = new race(rq);"


heres the code to the GUI:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ResultsInterface
{
private int WIDTH = 500;
private int HEIGHT = 300;

private JFrame frame;
private JPanel panel;
private JLabel inputLabel1;
private JTextField racename;
private JButton enter;
private JTextArea resultdisplay;
private JScrollPane scroll;

//----------------------------------------------------
// Sets up the GUI
//----------------------------------------------------
public ResultsInterface()
{
frame = new JFrame ("Race Results");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

inputLabel1 = new JLabel("Enter name of race");

racename = new JTextField(40);

enter = new JButton("Enter");
enter.addActionListener (new TempListener());

resultdisplay = new JTextArea (10, 15);
scroll = new JScrollPane (resultdisplay);

panel = new JPanel();
panel.setPreferredSize (new Dimension(WIDTH, HEIGHT));
panel.setBackground (Color.red);
panel.add (inputLabel1);
panel.add (racename);
panel.add (enter);
panel.add (scroll);

frame.getContentPane().add (panel);
}

//----------------------------------------------------
// Displays the primary application frame.
//----------------------------------------------------
public void display()
{
frame.pack();
frame.show();
}

//------------------------------------------------------------------
// Represents an action listener for the input fields
//------------------------------------------------------------------
private class TempListener implements ActionListener
{
//--------------------------------------------------------------
// Stores information in array when the enter button is pressed
// in the text field
//--------------------------------------------------------------

public void actionPerformed (ActionEvent event)
{
String rq = racename.getText();
race r1 = new race(rq);
}
}
}
this is my JDBC class (it complied correctly though):
import java.sql.*;

public class race
{
private String raquery;
public race(String raceq)throws SQLException
{
raquery=raceq;

DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
boolean found = false;
String user, pass, query, result;
user = "awds";
pass = "asdf";
query = "Select race from nascar.races where race=raquery";

try
{
Connection conn = DriverManager.getConnection("jdbc racle:thin:@Neelix:1521:itec",user,pass);
Statement stmt = conn.createStatement ();
ResultSet rset = stmt.executeQuery (query);

while (rset.next ())
{
found = true;
if (found == true)
{
result =rset.getString("");
System.out.println(result);
}
}

if (found == false)
System.out.println("There is no drivers with those values.");

conn.close();
}

catch (SQLException e)
{
System.out.println ("Could not load the db"+e);
}
}
}

Can anyone help me out with this? Any help would be greatly appreciated, because I'm really stumped, I handled the exceptions already in the JDBC class.
[ February 16, 2005: Message edited by: jin sun ]
 
Lu Battist
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to surround the following line:
race r1 = new race(rq);

like this:


with a try-catch block, that catches the sql exception thrown by the race constructor. Or delcare that the action performed method throws SQLException but somewhere up the line it has to be caught and dealt with so might as well do it right there on line 69.

By the way:



Editing post:
You may prefer to just remove the SQLException being thrown here if you've handled it in the constructor:
public race(String raceq)throws SQLException

You may need to move the following line into the try block, I can't remember what exceptions this one might throw.
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
[ February 16, 2005: Message edited by: Lu Battist ]
 
jin sun
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh ok thanks a lot Lu Battist, errors are gone now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic