java.util.regex.PatternSyntaxException with UCanAccess - java

I am trying to connect to an Access database with Java 8 like in this answer :
Manipulating an Access database from Java without ODBC
I am unfamiliar with the creation/design of the DB.
The connection seems to work because I get a few error messages like this :
WARNING:Error in the metadata of the table MB02: table's row count in
the metadata is 556 but 557 records have been found and loaded by
UCanAccess. All will work fine, but it's better to repair your
database.
And I have this method trying to pull something out of the DB with an SQL SELECT Statement :
public void searchDatabase()
throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
Connection con = DriverManager
.getConnection("jdbc:ucanaccess:///C:/Users/Blabla.mdb");
Statement stmt = con.createStatement();
String query = "SELECT * FROM MB01";
ResultSet resultSet = stmt.executeQuery(query);
while(resultSet.next()){
System.out.println(resultSet.getString(1));
}
}
I keep getting this error :
Exception in thread "main" java.util.regex.PatternSyntaxException:
Dangling meta character '?' near index 19 MB 01 BEZAHLT ???
^ at java.util.regex.Pattern.error(Unknown Source) at java.util.regex.Pattern.sequence(Unknown Source) at
java.util.regex.Pattern.expr(Unknown Source) at
java.util.regex.Pattern.compile(Unknown Source) at
java.util.regex.Pattern.(Unknown Source) at
java.util.regex.Pattern.compile(Unknown Source) at
java.lang.String.replaceAll(Unknown Source) at
net.ucanaccess.converters.SQLConverter.replaceWhiteSpacedTableNames0(SQLConverter.java:547)
at
net.ucanaccess.converters.SQLConverter.replaceWhiteSpacedTables(SQLConverter.java:526)
at
net.ucanaccess.converters.SQLConverter.convertSQL(SQLConverter.java:372)
at
net.ucanaccess.converters.SQLConverter.convertSQL(SQLConverter.java:472)
at
net.ucanaccess.jdbc.UcanaccessStatement.convertSQL(UcanaccessStatement.java:57)
at
net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:206)
at DBCrawler.searchDatabase(DBCrawler.java:27)
Line 27 is where the error is thrown is :
ResultSet resultSet = stmt.executeQuery(query);
The thing is I can leave the query String empty or put any nonsense values in it and I still get the error.
Where do I start searching for the problem or how can I fix this?

I created an Access database with a table named 19 MB 01 BEZAHLT ??? and was able to reproduce your issue with UCanAccess 3.0.5. The problem will be was fixed in a future release of UCanAccess 3.0.6.

Related

HSQLDB "Unknown JDBC escape sequence" in CallableStatement

I am using SpringMVC and I have some issues with a HSQLDB and a CallableStatement used like this:
Java code
String call = "{ ? = call MY_FUNCTION(?,?) }";
CallableStatement statement = connection.prepareCall(call);
Exception
It throws the following exception:
java.sql.SQLException: Unknown JDBC escape sequence: { ? = call MY_FUNCTION(?,?) }
at org.hsqldb.jdbc.jdbcConnection.onStartEscapeSequence(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.nativeSQL(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.(Unknown Source)
at org.hsqldb.jdbc.jdbcCallableStatement.(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.prepareCall(Unknown Source)
Looking at the HSQLDB JDBCConnection docs it says:
... to avoid a great deal of complexity, certain forms of input whitespace are currently not recognised. For instance, the driver handles "{?= call ...}" but not "{ ?= call ...} or "{? = call ...}"
Which leads to believe that call should be defined as
String call = "{?= call MY_FUNCTION(?,?)}";
This however throws another exception which looks even more wrong to me since it fails in the <init> and does not even reach the part where the escaping is done:
java.sql.SQLException: Unexpected token: MY_FUNCTION in statement [ call MY_FUNCTION(?,?) ]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.jdbcCallableStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.prepareCall(Unknown Source)
According to this answer it is not possible but it is dated and looking at the docs for jdbcCallableStatement it says it is supported.
I am completely at loss on if what I am trying to do is possible, if there is some conflict with SpringMVC or HSQLDB or something completely different.
Is it possible to use CallableStatement with HSQLDB 1.8 and if so, how?

MySQLSyntaxErrorException error in Like clause

I have this function, which gets a List of objects from MySQL, using the like clause, but when testing, returns me a error of MySQLSyntaxErrorException, what i should do?
Code
#Override
public List<Livro> getLista(String nm) {
// TODO Auto-generated method stub
List<Livro> lista = new ArrayList<Livro>();
try {
String query= "SELECT L.*, nmautor, nmeditora FROM tblivro L, tbeditora E,tbautor A "
+ "WHERE nmlivro LIKE ? and L.cdautor = A.cdautor and L.cdeditora = E.cdeditora ";
PreparedStatement stmt = conn.prepareStatement(query);
String like = "%"+nm+"%";
stmt.setString(1, like);
ResultSet rs = stmt.executeQuery(sql);
...
}}
Full Error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2470)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2546)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2504)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1370)
at biblioc.dados.LivroDAO.getLista(LivroDAO.java:387)
The error is in this line:
ResultSet rs = stmt.executeQuery(sql);
Replace this with
ResultSet rs = stmt.executeQuery();
To execute a SQL string with JDBC, you only need to pass the SQL string to JDBC once, and when you are using a PreparedStatement that one time is when you prepare the statement. Once you've set all the parameters, calling executeQuery() with no arguments will run the prepared statement with the parameter values you set.
Calling the version of executeQuery that takes a single string parameter will run the SQL in that string, without allowing you to use any parameter values. This is because the PreparedStatement interface inherits this method from the Statement interface.
This behaviour of the MySQL JDBC driver is particularly confusing. You are far from being the first person to make this mistake and wonder what is going on, and I cannot believe that you will be the last. As pointed out in the comments, the JDBC driver is supposed to throw an exception when you call executeQuery with a string on a PreparedStatement. Had it done so, you may well have found a solution to your problem much quicker.
I don't know what your variable sql is. Perhaps it just contains a single letter a - I was able to reproduce your error message by running stmt.executeQuery("a").

NullPointerException while accessing HSQLDB database

I am accessing HSQLDB through A jdbc drive in my java program. The script file of the database is as follows.
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
CREATE CACHED TABLE TBL1(ID CHAR(5),NAME VARCHAR(11),ZIP VARCHAR(11))
CREATE UNIQUE INDEX IDX_ID ON TBL1(ID)
CREATE CACHED TABLE TBL2(ID CHAR(5),STREET CHAR(5))
CREATE INDEX IDX_RS_ID ON TBL2(ID)
CREATE CACHED TABLE TBL3(ID VARCHAR(8) NOT NULL PRIMARY KEY,LON FLOAT,LAT FLOAT)
CREATE GLOBAL TEMPORARY MEMORY TABLE OVERLAY(ID CHAR(8),AROUTE CHAR(10),ZROUTE
CHAR(10),TYPE CHAR(8)) ON COMMIT PRESERVE ROWS
SET TABLE TBL1 INDEX'4490648 5643624 0'
SET TABLE TBL2 INDEX'529105280 529105280 0'
SET TABLE TBL3 INDEX'775814624 0'
CREATE VIEW OVERLAYSPAN (ID,STREET) AS SELECT DISTINCT ID, NAME FROM OVERLAY AS O, TBL2 as
R WHERE O.ID = R.ID
CREATE USER SA PASSWORD ""
GRANT DBA TO SA
SET WRITE_DELAY 10
In the java program, I am first removing all entries (1000 entries) from TBL1 and reinserting new entries (1500) in the same table. The program executes normally, but, when I am attempting to re-execute the same program, I am getting the following error.
java.sql.SQLException: error in script file line: 9 S1000 General error
java.lang.NullPointerException in statement [SET TABLE TBL2 INDEX'529105280 529105280 0']
Here is the fill stack trace from the program.
java.sql.SQLException: error in script file line: 9 S1000 General error java.lang.NullPointerException in statement [SET TABLE TBL2 INDEX'529105280 529105280 0']
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.<init>(Unknown Source)
at org.hsqldb.jdbcDriver.getConnection(Unknown Source)
at org.hsqldb.jdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at HSQLTesting.main(HSQLTesting.java:98)
Where at line 98, the program is attempting to make a connection with the database.
Connection sqlconnect = DriverManager.getConnection("jdbc:hsqldb:file:C:/birdseyedb/birdseye", "SA", "");
Any idea what's going wrong here?
Thanks in advance.

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 - e.g.
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, e.g. 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 - e.g. "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):
http://sourceforge.net/p/hsqldb/mailman/message/28944633/
http://www.coderanch.com/t/460394/JDBC/databases/HSQLDB-user-lacks-privilege-object

Weird java.sql.SQLException error

I have a weird issue and most the stuff I seem to find on the web is referring to a driver or connection problem...
I am parsing threw a dBASE table to get some id numbers:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cnxn = DriverManager.getConnection("jdbc:odbc:dBASE Files;DefaultDir=\"C:\"");
Statement stmt = cnxn.createStatement();
command = "SELECT * FROM table";
ResultSet result = stmt.executeQuery(command);
while (result.next()) {
System.out.println(result.getInt(1));
}
The result that i get in the console is
1
2
3
4
...
1312
1313
1314
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid string or buffer length
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
the line that generated the error is:
System.out.println(result.getInt(1));
Since i am getting a result out of it, I don't think it's a connection error or a driver one. I looked at the table and I don't seem to have anything wrong with the row 1315, i tried deleting it and nothing. Btw the table used has 2376 rows.
I have tried with another table with a different length and I also get the error after 417 rows.
Just in case it helps I am on windows 7, 64 bit, the tables are dBASE IV.
Thx in advance!
Another idea still. How about selecting 1,314 records from the table, but with a different range (for example, rows 1,000 -> 2,313)? I am wondering if the error still occur.

Categories