mardi 4 août 2015

Sql queries not executed on H2 database

Recently i changed from HSQLDB to H2, changed a bit of code and my queries stopped executing.

I test my SQL code with RazorSQL where i try to access my DB from , bud to my suprise there is no table created,no errors thrown no null pointers , valid sql, db file created - everything seems to be running alright bud no content in database whatsoever.

Here are a crucial parts of my database access/creation.

I use connector class to create connect to database

public class H2DatabaseConnector {

    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;


    public static final String[] PREP_STATEMENTS = new String[]{};

    public static final String DB_FILE_NAME = File.separator + "dboc";
    public static final String DB_DIR = Info.OC_DIR + "oc_database\\";

    static {
        try {
            Class.forName("org.h2.Driver");
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public H2DatabaseConnector(String username, String password) {
        try {
            openConnection(username, password);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

.....
//data credentials are correct
 private void openConnection(String username, String password) throws SQLException {
        connection = DriverManager.getConnection("jdbc:h2:file:" + DB_DIR + DB_FILE_NAME+";DATABASE_TO_UPPER=false", username, password);
        statement = connection.createStatement();
    }

  public void execute() {
    }

}

And utility class where i execute my sql commands

public class DbUtil {


    public static final void createUsersTable(){
         new H2DatabaseConnector(Info.Db.DB_MAIN_USERNAME,Info.Db.DB_MAIN_PASSWORD) {

            @Override
            public void execute() {
                try {
                    getStatement().execute("CREATE TABLE users(name VARCHAR(255) NOT NULL,password VARCHAR(255) NOT NULL,email VARCHAR(255));");
                    System.out.println("Table Created users");
                    getStatement().executeUpdate("INSERT INTO users VALUES ('sa','sa',NULL);");
                    closeConnection();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }

        }.execute();
    } 

}

Execution is going thru as expected bud no table is created. Why is this happening? There are no errors shown,and sql syntax is correct since when i open database in **RazorSQL and create/insert table there everything works.**

Any ideas? Im literally stuck on this for a whole day.

Aucun commentaire:

Enregistrer un commentaire