• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

SQL exception error

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh ok thanks a lot Lu Battist, errors are gone now.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic