0

I have 2 problems.

  1. The INSERT queries below are not inserting records in the msAccess database. Can someone please explain why this is happenning and how I can fix it? I replaced single quotes with double quotes, but that doesnt help either.

  2. In this code below, only 1 query gets executed, first one, rest are all skipped. I have to comment previous queries each time and recompile to execute the next one. Is there another way of doing this?

    try {
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       Connection conn=null;
       conn= DriverManager.getConnection("jdbc:odbc:SS");
       Statement s;
       s=conn.createStatement();
       ResultSet rs;
       rs=s.executeQuery("drop table users");
       rs=s.executeQuery("CREATE TABLE users ( id AUTOINCREMENT, username varchar(255) , pass varchar(255), PRIMARY KEY(id) ) ");
       rs=s.executeQuery(" insert into users (username, pass) values( 'name1', 'pass1') ");
       rs=s.executeQuery(" insert into users (username, pass) values( 'name2', 'pass2') ");
    } catch (SQLException ex) { 
       ex.printStackTrace(); 
    } catch(Exception ee) {
       ee.printStackTrace();
    }
    
4
  • 1
    do you get any exceptions? Commented Mar 4, 2013 at 8:20
  • Use a PreparedStatement Commented Mar 4, 2013 at 8:25
  • The problem could be s.executeQuery("drop table users") will throw an exception if the table does not exists. You need to check whether the table exists before dropping it Commented Mar 4, 2013 at 8:32
  • The exceptions I get are: "no resultset was generated". Also, I used the drop statement on an existing table only. The main problem is that its not inserting anything in the database. Commented Mar 4, 2013 at 9:05

2 Answers 2

1

There is no ResultSet involved, because you do not make any queries, you should just execute updates. Your program should work if you change your code like this:

Connection conn=null;
Statement s=null;
try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn = DriverManager.getConnection("jdbc:odbc:SS");
    s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    s.executeUpdate("drop table users");
    s.executeUpdate("CREATE TABLE users ( id AUTOINCREMENT, username varchar(255) , pass varchar(255), PRIMARY KEY(id) ) ");
    s.executeUpdate("insert into users (username, pass) values( 'name1', 'pass1') ");
    s.executeUpdate("insert into users (username, pass) values( 'name2', 'pass2') ");
} catch (SQLException ex) {
    ex.printStackTrace();
} catch (Exception ee) {
    ee.printStackTrace();
} finally {
    try {
        conn.close();
        s.close();
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
}

Beware that in order to insert into a database table you should create your statetement passing the appropriate parameters.

Another important point is that you should always close the Connection, the Statement and the ResultSet when you are finished. When you close your Connection the second insert will be flushed to the database table.

Sign up to request clarification or add additional context in comments.

Comments

0

For the first problem you should use executeUpdate for statements altering the database, and executeQuery for when you retrieve data from the database.

For the second one you should create multiple statements, and not use the same statement for every query you are trying to execute. you also should close each statement/connection properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.