How to do with SQLException in below code?  

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.sql.*;

class test extends JFrame throws SQLException {

JButton jbt;

public static void main(String[] args){

new test();

}

public void test(){

setTitle("Demo");

jbt = new JButton("Open SQL");

Container c = this.getContentPane();

c.setLayout(new BorderLayout());

c.add(jbt,BorderLayout.CENTER);

jbt.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

try{

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

String url = "jdbc:odbc:guihua";

Connection con = DriverManager.getConnection(url, "", "");

Statement s = con.createStatement();

String query = "select * from certificateList)";

s.execute(query);

con.close();

}

catch(SQLException e){

System.err.println(e.getMessage());

}

}

});

addWindowListener(new WindowAdapter(){

public void WindowClosing(WindowEvent e){

System.exit(0);

}

});

pack();

setVisible(true);

}

}

error info happened as below

test.java:9: '{' expected

public class test extends JFrame throws SQLException {

^

test.java:52: '}' expected

}

^

2 errors

waiting for your help!!!!!!!!!!!!!!!!!!!!!!

[1465 byte] By [syfwebber] at [2007-9-27 22:21:23]
# 1

A class cannot throw a exception. So what you have to do, to make your code compile:

remove throws SQLException from the class declaration and add it to the main method. (public static void main(String args[]) throws SQLException {...})

Now change the variablename e from the SQLException to a different name (eg. sqle because the actionevent is already bound to the variable e !) save and compile the class.

It should work now !

so long

ThK

ComLogic at 2007-7-7 12:43:44 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
Thank you very much!another question:Class.forName("") must throw ClassNotFoundException?
syfwebber at 2007-7-7 12:43:44 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...