简体   繁体   中英

HSQLDB INSERT Value in a Database

Hi i have a Problem while inserting data in my databases ...

    String query = "INSERT INTO gutscheine (code, Wert) values ("+Code+","+Wert+")";
    SQLConnection sql = new SQLConnection("Gutscheine.db");
    PreparedStatement ps;
    try {
        ps = sql.con.prepareStatement(query);
        int i = ps.executeUpdate();   

        if (i == -1) {
            System.out.println("db error : " + query);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    sql.CloseConnection(); // my own funktion

My Error:

java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: GUTSCHEINE
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
    at com.daniel.guthaben.Gutschein.<init>(Gutschein.java:33)
    at com.daniel.guthaben.Main.main(Main.java:7)
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: GUTSCHEINE
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.SchemaManager.getTable(Unknown Source)
    at org.hsqldb.ParserDQL.readTableName(Unknown Source)
    at org.hsqldb.ParserDQL.readRangeVariableForDataChange(Unknown Source)
    at org.hsqldb.ParserDML.compileInsertStatement(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
    at org.hsqldb.Session.compileStatement(Unknown Source)
    at org.hsqldb.StatementManager.compile(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 4 more

Here i am trying adding context from the root user ... (on the left you can see the table exists) http://i.stack.imgur.com/FS5ws.png

Sorry I was using diffrent databases ... :(

it's generally not advised to use string building for your query. String query = "INSERT INTO gutscheine (code, Wert) values (?, ?)"; and then running it through a safe query preparation is going to be better.

That said, the error sounds pretty obvious? user lacks privilege or object not found: GUTSCHEINE . Doesn't look like you're connecting as a particular user from your code, so I suspect you forgot to pass along your username and password when setting up the database connection. If that part's fine, verify Gutscheine.db exists.

The problem here is simple. When you are inserting string values, you need to enclose them in single quotes. For example, "insert into table(col1, col2) values ('ABC','xyz');"

Have a look at the example provided in this link:

https://www.tutorialspoint.com/hsqldb/hsqldb_insert_query.htm

In your case, you need to enclose the values of Code and Wert in single quotes before you insert, or at the time of insert. The following approach worked for me:

Code = "'" + Code + "'";

Wert = "'" + Wert + "'";

Use your insert query now, and it should work.

Even though hsql is quite forgiving and requires no infrastructure, you need to create the table upfront - eg

CREATE TABLE Gutscheine ( code varchar 20, Wert varchar 20 )

Also, you might want to read about SQL-Injection and use PreparedStatement. While you're at it, google for "little bobby tables"

I haven't run into privilege issues with hsql yet, but you might want to check how you open the connection, eg if you give a proper user account. You can actually check a proper one by looking at the hsql storage, which is clear text. It should contain the required user name - eg "SA"

Also, with that error message on google, several threads come up that suggest that hsql version upgrade might help, or that you ended up with a corrupt database after not executing "SHUTDOWN;" after creation of the database. Another one is the database name "null" in your .script file. Well, check them for yourself and try which solution matches your problem (the two linked threads point to several possible solutions):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM