0

I am trying to create a simple SQLite database program that could create a database, a table, insert and retrieve data. How can I fix this? I keep getting the exception:

java.sql.SQLException: statement is not executing

package databaseproj;

import java.sql.*;

public class main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
            String sDriverName = "org.sqlite.JDBC";
            Class.forName(sDriverName);
            String dbURL= "jdbc:sqlite:hello.db";

            Connection conn= DriverManager.getConnection(dbURL);
            Statement st= conn.createStatement();

            String createtable="CREATE TABLE contacts(name text, number numeric, email text)";
            System.out.println("Table created successfully");
            String insertcontacts="INSERT INTO contacts(name, number, email) VALUES('nduka',08166459353,'ndukaude')";

            st.executeUpdate(createtable);
            st.executeUpdate(insertcontacts);
            System.out.println("Insert complete");

            ResultSet rs= st.getResultSet(); 

            conn.setAutoCommit(false);
            while ( rs.next() ){
                String name= rs.getString("name");
                int num=rs.getInt("number");
                String email=rs.getString("email");

                System.out.println("NAME: "+name);
                System.out.println("NUMBER: "+ num);
                System.out.println("EMAIL: "+email);
            }

            conn.commit();
            conn=null;
            System.out.println("connection emptied");
            rs.close();

            st.close();

            conn.close();
            System.out.println("connection closed");
        }

        catch ( Exception e ) {
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            System.exit(0);
        }
    }
}
4
  • 5
    Which statement is failing to execute? Commented Dec 18, 2015 at 10:33
  • If "number" is a phone number, you probably want to insert it as a varchar, not a numeric value . Commented Dec 18, 2015 at 10:34
  • is autocommit true in your case? otherwise you will not find the created table after execute the create sql Commented Dec 18, 2015 at 10:41
  • ResultSet rs= st.getResultSet(); does not execute Commented Dec 18, 2015 at 14:22

2 Answers 2

1

use execute(createtable) instead executeUpdate(createtable)

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

Comments

0

Dont know whether you found the solution. The reason for this is most probably that the statement (st) is still referring to Create Command when you call st.executeUpdate(insertcontacts).

Try to reinitialize the statement and run the insert command. It should work now!

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.