I'm currently using HSQLDB to save java data. Within theses datas, there are some Double, and some of them can be of values of NaN (described as 0.0/0.0 in the javadoc). HSQLDB know how to handle these values in setDouble and setFloat of PreparedStatement. The thing is, I have to use a Statement object, not a precompiled stored procedure, and I just can't find a way to make it work.
If you had the tinyest hint, it would be most welcome :)
Thanks.
EDIT : Here's the bunch of code I'm using :
stmt.executeUpdate("insert into Mesh(Id, name, dimension, meshtype, totalVolume, NumberOfCoarseCell) values (identity(), "
+ "'" + name_ + "',"
+ dimension_ + "," // this value can be NaN
+ "'" + type_.toString() + "',"
+ totalVolume_ + "," // this value can be NaN
+ numberOfCoarseCells_ + ")");
You mean you need a way to write a NaN within a SQL statement? The following works for the H2 database:
select sqrt(-1) from dual
However, it doesn't work for Apache Derby and PostgreSQL (I didn't test other databases). For HSQLDB, it no longer works in version 2.1 and newer, unless you set SET DATABASE SQL DOUBLE NAN FALSE.
With HSQLDB 1.8.x you can use (0.0e1/0.0e1) as an expression that returns NaN.
For example:
create table t (d double)
insert into t values (0.0e1/0.0e1)
For HSQLDB 2.1 and above, an property must be specified with an SQL statement:
SET DATABASE SQL DOUBLE NAN FALSE
Or as a connection property:
hsqldb.double_nan=false
Related
I'm working on a project that uses a PostgreSQL database.
There's a few locations in the project which build queries like this:
query += " AND " + String + " #> " + String;
I'm not familiar with the #> symbol, and neither is anyone currently working on the project. Also googling it doesn't work, presumably as it's an odd symbol.
Also, I'm not sure if this symbol is a postgresql thing or a sql thing.
P.S. The application in written in java.
It's "contains".
If the column is array and use #> the column should contain the value.
More here: https://www.postgresql.org/docs/current/static/functions-array.html
I used the statement below to create a Derby database table with auto-increment primary column.
CREATE TABLE \"table\" (\n"
+ " \"id\" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL,\n"
+ " \"path\" VARCHAR(2000) DEFAULT NULL,\n"
+ " \"downloaded\" BOOLEAN DEFAULT false NOT NULL,\n"
+ " \"retried_times\" SMALLINT DEFAULT 0 NOT NULL,\n"
+ " \"name\" VARCHAR(40),\n"
+ " \"downloaded_date\" TIMESTAMP DEFAULT NULL,\n"
+ " PRIMARY KEY (\"id\")\n"
When I insert a row through Spring JDBC, it increments by 100. Is there any error in my statement?
This is due to pre-allocation of values for auto-increment columns. Derby being an in-memory database, caches auto-increment values when the database is first loaded into the memory. Then, future values of the auto-increment columns are generated using the cache instead of querying the database again and again. If the database is not shut down properly, unused values from the cache are lost forever.
You have two options to address this:
Add ;shutdown=true to the JDBC URL. This will shut the database down when the application ends.
Set the derby.language.sequence.preallocator property to 1 (its default value is 100). This will ensure that the column value is never cached.
Note that most databases behave similarly for sequences. For example, H2 has the exact same behaviour but uses a cache size of 32 instead of 100 like Derby does.
Thanks to #manish the second option worked for me.
In order to implement the 2nd solution, add the following code line where you set your database connection as shown in the below example.
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
String URL = "jdbc:derby:testDB;create=true;";
conn = DriverManager.getConnection(URL);
System.setProperty("derby.language.sequence.preallocator", "1"); // Add this line
Thanks to answer provided by #xfocus99, I was able to know how to implement the 2nd solution.
Is there a way in JPA 2.0 to set parameter of a query with special characters? I mean, if I do: query.setParameter(field, "%" + value.toString() + "%");, it takes % as special (wildcard) character? It seems to me it's not true in this case, beacuse my search doesn't work with truncated words...
Can You help me??
If you are using
LIKE :feild
for
query.setParameter(field, "%" + value.toString() + "%");
Then value remains free from the '%' sign. You can try to use % in your query and send value from setParameter() method.
You can also try CreteriaBuilderInterface of JPA.
http://www.objectdb.com/java/jpa/query/jpql/string.
you have to escape the query. try to use "\\%" instead
what ever you pass in as a query parameter that will pass on to query and back to database. So to the choice of escape character of the query parameter will depend on database. Like in mysql it is "\'" for "'" and "\%" for "%" so if I want to send "john's 10% commission" as query parameter to mysql, i will try query.setParameter(field, "john\\'s 10\\% commission"); in your case query.setParameter(field, "\\%" + value.toString() + "\\%"); should work. But check the documentation of the database to find out escape character for special cases.
For this Java code:
stmt.addBatch(
"INSERT INTO Bills (BillDateTime, Table, Item, NoAttended, Service, Payment, Total) " +
"VALUES('" + billDateTime + "', " + Integer.parseInt(createTableNumberOutput.toString()) + ", '" + null + "', '"
+ Integer.parseInt(createGuestNumberOutput.toString()) + "', " + "5" + ", '" +
createPaymentTypeOutput.toString() + "', '" + "')");
I get the following error:
java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Table, Item, NoAttended, Service, Payment, Total) VALUES('2012-03-26 11:15:8', 1' at line 1
The issue is not apparent to me, as MySql requires the format 'YYYY-MM-DD HH:MM:SS' for dateTime, which I have, right?
Table is reserved keyword in mysql . use backticks(`) around it.
Like below:
stmt.addBatch("INSERT INTO Bills (BillDateTime,`Table`, Item,NoAttended,Service,Payment,Total..........")
Even when you fix the reserved keyword table issue, it still won't work. You are trying the insert date.toString() in the date field. (when concatenated, toString() gets called all non-string objects)
Instead, you should use a PreparedStatement and call stm.setDate(..). Get more familiar with the prepared statement, as it is most often the better way to go. (it also has addBatch(), which works in a slightly different way - you set all parameters, then add, then set again.
I'm trying to insert values into a SQL database from within Java. This works fine, except for some of the values. Eg, when I insert "foo" it appends null at the start so it becomes "nullfoo". If I insert the same statement in SQL Server Management Studio this doesn't happen.
To be sure: I print the string before inserting it and it reads "foo".
My insert code:
statement.execute("INSERT INTO " + settings.getProperty("table") + " VALUES ('" + value1+ "', '" + value2 + "', '" + value3 + "')");
You're concatenating values into the SQL statement. If any of those references (value1, value2 etc) are null, then those will be converted into the string "null" as part of concatenation.
The correct fix for this is not to change the way you're doing validation - it's to stop putting the values into the SQL statement itself. Use PreparedStatement with parameterized SQL and set parameter values instead.
Benefits:
You won't get "null" inserted any more
You won't be vulnerable to SQL injection attacks any more (you are now)
When inserting non-text data you won't need to worry about problematic conversions (this is particularly relevant for date/time fields)
Your code will be clearer, as you'll be separating the code (SQL) from the data (parameter values)
Your prepared statement query plan can be cached by the server, so it may perform faster
You should use variable binding in your SQL
http://decipherinfosys.wordpress.com/2007/08/29/bind-variables-usage-parameterized-queries-in-sql-server/
It's easier to check for errors.
In your case you are probably adding null+"foo" so you get nullfoo.