Results 1 to 15 of 15
  1. #1
    jttslg is offline Member
    Join Date
    Mar 2010
    Posts
    26
    Rep Power
    0

    Default error while using sql update statement

    while running the following program i got a runtime error:

    exception [Microsoft][ODBC Microsoft Access Driver] Operation must use an updateable query.




    import java.io.*;
    import java.sql.*;

    public class database
    {
    public static void main(String args[])throws IOException
    {
    Connection con=null;
    Statement stmt=null;
    // ResultSet rs=null;
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con=DriverManager.getConnection("jdbc:odbc:Access" );
    //String s="SELECT * FROM CUSTOMER";
    stmt=con.createStatement();
    String s1 = "insert into CUSTOMER values('adf','dsf')";
    if (stmt.executeUpdate(s1)>0)
    System.out.println("record inserted");
    else
    System.out.println("record insertion failed");
    // rs=stmt.executeQuery(s);
    // while (rs.next())
    // {
    // System.out.println(" " + rs.getString("name"));
    // }
    }
    catch(Exception e)
    {
    System.out.println("exception "+e.getMessage());
    }
    }
    }


    plz help me solve the problem
    Last edited by jttslg; 06-26-2011 at 05:03 PM.

  2. #2
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    try stmt.executeQuery() instead

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    I don't do JDBC, but I don't see anywhere in that code where you tell Access which database (.mdb) to use.

    @d3n1s: the question is about inserting a record, not about querying a table.

    db

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DarrylBurke View Post
    I don't do JDBC, but I don't see anywhere in that code where you tell Access which database (.mdb) to use.

    @d3n1s: the question is about inserting a record, not about querying a table.
    The OP edited his/her post; the orignal one had a ddl statement where a table was supposed to be created. The error message was different too (something about a read-only database)

    kind regards,

    Jos
    Last edited by JosAH; 06-26-2011 at 05:55 PM. Reason: typo ...
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    I don't do JDBC, but I don't see anywhere in that code where you tell Access which database (.mdb) to use.

    @d3n1s: the question is about inserting a record, not about querying a table.

    db
    You're right db i guess i misread. Before i began using hibernate i had the same approach to inserting a record into mysql and the method he uses work fine. Are you able to run a simple select query to return a rowset using this connection object? As in :

    Java Code:
    stmt = con.createStatement();
    String sql = "SELECT max(id) as id FROM tblPerson";
    ResultSet rs = stmt.executeQuery(sql);
    rs.first();
    Id = rs.getInt("id") + 1;
    rs.close();

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Quote Originally Posted by jttslg View Post
    con=DriverManager.getConnection("jdbc:odbc:Access" );
    I am not sure with this line.

    In your stmt = con.createStatement() you have to put the value for parameters such as resultSetType and resultSetConcurrency.
    Syntax for this is:
    Java Code:
    createStatement(int resultSetType, int resultSetConcurremcy)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    Quote Originally Posted by mine0926 View Post
    I am not sure with this line.
    That's fine. It's the syntax for the jdbc-odbc bridge. So long as the OP has an ODBC source defined as Access then that'll work.

    Quote Originally Posted by mine0926 View Post
    In your stmt = con.createStatement() you have to put the value for parameters such as resultSetType and resultSetConcurrency.
    Syntax for this is:
    Java Code:
    createStatement(int resultSetType, int resultSetConcurremcy)
    Nope. You don't have to have any parameters on a createStatement() command. I rarely bother, as the default result set is usually OK.

  8. #8
    jttslg is offline Member
    Join Date
    Mar 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    I don't do JDBC, but I don't see anywhere in that code where you tell Access which database (.mdb) to use.

    @d3n1s: the question is about inserting a record, not about querying a table.

    db
    how do i tell Access which database (.mdb) to use.
    plz help

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    Have you declared an odbc data source called Access?
    If not then you need to do that.
    I'm sure there'll be some Microsoft documentation on how to do that.

  10. #10
    jttslg is offline Member
    Join Date
    Mar 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Have you declared an odbc data source called Access?
    If not then you need to do that.
    I'm sure there'll be some Microsoft documentation on how to do that.
    that i had already done .... in fact when i use executeQuery method it works fine it is only when i try to update the table this error comes.... plz help

  11. #11
    jttslg is offline Member
    Join Date
    Mar 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Have you declared an odbc data source called Access?
    If not then you need to do that.
    I'm sure there'll be some Microsoft documentation on how to do that.
    that i have already done in fact when i use executeQuery method it works fine ... it is only when i try to update the table this error comes.... plz help

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    OK, then give us the full error message with stack trace.
    You'll have to change your catch block to use printStackTrace().

  13. #13
    jttslg is offline Member
    Join Date
    Mar 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    OK, then give us the full error message with stack trace.
    You'll have to change your catch block to use printStackTrace().
    init:
    deps-module-jar:
    deps-ear-jar:
    deps-jar:
    compile-single:
    run-main:
    java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Operation must use an updateable query.
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc .java:6957)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java :7114)
    at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java :3110)
    at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcSt atement.java:338)
    at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(Jdbc OdbcStatement.java:288)
    at database.main(database.java:18)
    BUILD SUCCESSFUL (total time: 1 second)

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    Looks like your Access db might be already open?
    Something somewhere is marked as read only.

  15. #15
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default

    This topic was cross posted to CodeRanch. The OP has not posted anything new in either topic, however it is probably worth looking in the other topic to see if there is anything new.

Similar Threads

  1. Replies: 1
    Last Post: 05-17-2011, 09:43 AM
  2. fcpred.class update error
    By Fulltimer2011 in forum Advanced Java
    Replies: 0
    Last Post: 03-24-2011, 07:41 PM
  3. Error in JDBC batch update
    By murali23krishna in forum JDBC
    Replies: 8
    Last Post: 02-21-2011, 11:42 AM
  4. Replies: 1
    Last Post: 01-10-2011, 01:44 AM
  5. Update statement not working.
    By OMFGITSROHIT in forum JDBC
    Replies: 5
    Last Post: 04-08-2010, 01:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •