Can't connect or insert to database using ucanaccess method in applet
Asked Answered
S

3

8

While connecting one applet to an Access DB using the jdbc:ucanaccess method, I get the following error:

Firstdb.java:44: error: unreported exception SQLException; 
must be caught or declared to be thrown
stmt.executeUpdate(sql);   
                  ^

The code that I used for the applet is as follows (add() and setBounds() are removed from init()):

public class Firstdb extends Applet implements ActionListener {
    TextField t1, t2;
    Label l1;
    Button b1, b2;
    Connection con;
    Statement stmt;

    public void init() {
        try {
            con = DriverManager.getConnection("jdbc:ucanaccess://H:/test/db.mdb");
            stmt = con.createStatement();
        } catch (Exception e) {
        }
    }

    public void actionPerformed(ActionEvent ae) {
        String sql;

        if (ae.getSource() == b1) {
            sql = "insert into user (Uname) values(" + t1.getText() + ")"; 
            stmt.executeUpdate(sql);
        } else if (ae.getSource() == b2) {
            //do something
        }
    }
}

Note: java version "1.8.0_141"

Why am I getting this error?

Sport answered 17/9, 2017 at 19:40 Comment(3)
1) Don't bury your head in the sand! Change }catch(Exception e){ } to }catch(Exception e){ e.printStackTrace(); } then copy/paste the resulting output from the Java Console. 2) Why use AWT? See this answer for many good reasons to abandon AWT components in favor of Swing.Eurus
If you want to get better answers you should edit your question to include the full stack trace for the error you are receiving.Wooden
what is the error. Post complete stacktrace.Matsu
W
5

Your code has two fatal flaws:

  1. value is not a valid SQL keyword. It should be values. [Fixed in subsequent edit to question.]
  2. Your dynamic SQL is generating command text with invalid syntax (unquoted string literal).

Also, user is a reserved word (function name), so if you need to use it as a table name you really should enclose it in square brackets.

The proper solution to issue #2 above is to use a parameterized query, e.g.,

sql = "insert into [user] ([Uname]) values (?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, t1.getText());
ps.executeUpdate();
Wooden answered 17/9, 2017 at 22:40 Comment(2)
code that tested sql = "insert into [user] ([Uname]) values ('test')"; PreparedStatement ps = con.prepareStatement(sql); try{ ps.executeUpdate(); }catch(Exception ee){ ee.printStackTrace(); } but no use same errorSport
@ShibinRajuMathew, the usage you mentioned in the comment above is wrong and may be the issue isn't solved. Else Thompson's suggestion should work!Oleic
H
2

It is a compile error which means that you either need to surround your stmt.executeUpdate(sql); with try-catch statement, or your method should throw an SQLException:

 public void actionPerformed(ActionEvent ae) {
    try {
        String sql;

        if (ae.getSource() == b1) {
            sql="insert into user (Uname) values("+t1.getText()+")"; 
            stmt.executeUpdate(sql);
        } else if (ae.getSource() == b2) {
            //do something
        }
    catch (SQLException e) {
        // do something
    }
}

or

public void actionPerformed(ActionEvent ae) throws SQLException {
    String sql;

    if (ae.getSource() == b1) {
        sql="insert into user (Uname) values("+t1.getText()+")"; 
        stmt.executeUpdate(sql);
    } else if (ae.getSource() == b2) {
        //do something
    }
}

EDIT: By the way, I don't see if you are loading or registering Oracle JDBC driver class for UCanAccess before opening the database connection:

// Step 1: Loading or registering Oracle JDBC driver class
try {
     Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
} catch(ClassNotFoundException cnfex) {
     System.out.println("Problem in loading or "
            + "registering MS Access JDBC driver");
     cnfex.printStackTrace();
}

So do you have it or no? If no, you should add it before getting your connection: con = DriverManager.getConnection("jdbc:ucanaccess://H:/test/db.mdb");

Healthful answered 27/9, 2017 at 16:16 Comment(5)
can't insert any value to db.Sport
java.security.AccessControlException: access denied ("java.util.logging.LoggingPermission" "control")Sport
Then, it means, that your initial issue has solved and there is another issue. There are few links related to that issue: Try this one: #7047824Healthful
Also try this: serverfault.com/questions/558931/…Healthful
This may be useful too if you have any configuration file: developer.jboss.org/thread/229833Healthful
M
0

The message

Firstdb.java:44: error: unreported exception SQLException; 
must be caught or declared to be thrown
stmt.executeUpdate(sql);   
                  ^

looks like a compile error so the question is, how do you compile/deploy the applet-classes and where exactly do you see that error message. IDEs like Eclipse are creating class-files even in case of compile errors containing and outputting the error message that you can also see in the IDE. Maybe you fail to update the applet classes and end up testing with the same old classes instead of the changed ones leading to the same message over and over again.

If you deploy the applet as part of a web app, the HTTP server might cache the class-file if the web app has been deployed as WAR-file. Also, browsers tend to cache class-files quite aggressively so you should make sure that the browser's cache is empty each time you start a new test.

Mcloughlin answered 26/9, 2017 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.