I am trying to add row numbers to a sql query to get a return resultset, but the JDBC does not support BIGINT it says. I look up https://db.apache.org/derby/docs/10.9/ref/rreffuncrownumber.html and https://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.jdbc_pg.doc/ids_jdbc_141.htm.
The code:
String query = new StringBuilder("SELECT ROW_NUMBER() OVER() AS id, * FROM "+tableName).toString();
Error:
[Informix JDBC Driver][Informix]The data type bigint is not supported for current client/server configuration.
The IBM solution tells you to use getBigSerial() to get the BIGINT after the insert. However, I want to find a way to be able to add some auto increment numbers when it queries the table without creating an actual column. Is there a way?
You can cast it to VARCHAR in the query.
You can cast that String into BigInteger in Java code if you use it for something more than present it to the client.
Casting is good:
String query = "SELECT CAST(ROW_NUMBER() OVER() AS INT) AS id, * FROM "+tableName;
I test it with my informix database and it works.
Related
I'm trying to create a stored procedure in a MySQL database using the contents of a text file:
USE myDatabase;
DROP PROCEDURE IF EXISTS myStoredProcedure;
DELIMITER $$
CREATE PROCEDURE myStoredProcedure
(
_description VARCHAR(50),
_value INT
)
BEGIN
INSERT INTO myTable
(
description,
value
) VALUES (
_description,
_value
);
SELECT
id,
description,
value
FROM myTable
WHERE id = LAST_INSERT_ID();
END;
$$
DELIMITER ;
I execute the SQL using a native query:
Query query = entityManager.createNativeQuery(queryText);
...
query.executeUpdate();
But it gets an error on the DROP PROCEDURE
I commented out the DROP PROCEDURE and then it gets an error on the DELIMITER
Basically, it gets an error on any line after the first semicolon.
It seems as if JPA hibernate is parsing my query and telling me there's a problem with it rather than passing the unadulterated text onto MySQL.
The sql runs in MySQL without error.
I can't find anything in Google about creating a stored procedure with JPA, only calling one.
Does anyone have any insight on what I might be doing wrong? Or if this is even possible.
This can be possible if you mention the following property in the url
spring.datasource.url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true
The allowMultiQueries will instruct the driver to sent delimited queries to the database.
Please note that if you are using native queries be-aware of sql injection attack.
You dont need to put the delimiter(DELIMITER) explicitly.The sql statement
The following query works
SET myDatabase;
DROP PROCEDURE IF EXISTS myStoredProcedure;
CREATE PROCEDURE myStoredProcedure ( _description VARCHAR(50), _value INT )
BEGIN
INSERT INTO
myTable ( description, value )
VALUES ( _description, _value );
SELECT id, description, value
FROM myTable
WHERE id = LAST_INSERT_ID();
END;
com.datastax.driver.core.Session
for example:
//This works
session.execute ("select * from table");
//This returns a nullpointer
session.execute ("create table testtable ( number int, string varchar)");
Do I have to use some sort of schema builder?
NOTE: im connected to the cassandra instance and can query it no problem. I just want to be able to create tables from the datastax driver
What error do you get when trying to CREATE a table?
As for some quick things to try, it's possible that your user might be missing the CREATE permission.
session.execute ("create table testtable ( number int, string varchar)");
Another thing I noticed about this statement, was that you don't seem to be specifying a PRIMARY KEY. All tables in Cassandra must have a primary key.
Try altering your CQL to this, and see if it helps:
create table testtable ( number int, string varchar, PRIMARY KEY (number))
I have a query which I am trying to test. The query should update the data if it finds data in the table with existing primary key. If it doesn't then insert into the table.
The Primary key is of type int and in the properties I can see Identity is set to "True" which I assume it means that it will automatically set the new id for the primary if it is inserted.
MERGE INTO Test_table t
USING (SELECT 461232 ID,'Test1-data' Fascia FROM Test_table) s
ON (t.ID = s.ID)
WHEN MATCHED THEN
UPDATE SET t.Fascia = s.Fascia
WHEN NOT MATCHED THEN
INSERT (Fascia)
VALUES (s.Fascia);
The issue here is this query doesn't work and it never inserts the data or updates. Also, query gets compiled and I don't get any compilation error
Also the reason I want this query is to work because then I will use Java prepared statement to query the database so I am assuming I can do
SELECT ? ID,? Fascia FROM Test_table
So that I can pass the values with set methods in java.
Please let me know if there is something wrong in my query.
You are selecting from the target table as your source.
You either need to remove your FROM Test_table or have at least 1 row in Test_table prior to your merge.
rextester demo: http://rextester.com/XROJD28508
MERGE INTO Test_table t
USING (SELECT 461232 ID,'Test1-data' Fascia --FROM Test_table
) s
ON (t.ID = s.ID)
WHEN MATCHED THEN
UPDATE SET t.Fascia = s.Fascia
WHEN NOT MATCHED THEN
INSERT (Fascia)
VALUES (s.Fascia);
I am working on an application which updates data in a database(IBM DB2 v 9.7) via JDBC . Here's The table schema:
Column name Data Type Length
1)INDEX BIGINT -
2)USER_NAME VARCHAR 30
3)SRC VARCHAR 30
4)STATUS VARCHAR 150
5)RT_COUNT BIGINT -
And my Code is as:
String cmd1="Update ANALYTICS SET RT_COUNT = 1 WHERE USER_NAME = ? AND STATUS = ?";
PreparedStatement process=connection.prepareStatement(cmd1);
process.setString(1, Source);
process.setString(2, Content);
if(process.executeUpdate()==0)
{....
But it fails,can anyone help me ?
Thanks and regards
According to IBM's site, the SQLSTATE of 42818 is 42818 "The operands of an operator or function are not compatible or not comparable.". This means you set the wrong data type to your prepared statement.
You might need to bring the RT_COUNT out and do a setInt or setLong or something to make it work.
Also, I am assuming Source and Content are Strings? If they are not, that may contribute to this as well.
Edit
To do the RT_COUNT thing I was talking about you would have to modify your query to do the following:
String cmd1="Update ANALYTICS SET RT_COUNT = ? WHERE USER_NAME = ? AND STATUS = ?";
PreparedStatement process = connection.prepareStatement(cmd1);
process.setLong(1, new Long(1));
process.setString(2, source);
process.setString(3, content);
if(process.executeUpdate()==0)
{....
And that might actually be your problem, cause I'm not sure how the driver is interpreting the "1". It needs to be Long to map to bigint in DB2. See for what data types in Java map to what data types in DB2.
In addition to #Chris Aldrich's answer of casting in Java, if you know what types the parameters should be, you can also cast them in SQL, like so:
UPDATE ANALYTICS
SET RT_COUNT = CAST(? AS BIGINT)
WHERE USER_NAME = CAST(? AS VARCHAR(30))
AND STATUS = CAST(? AS VARCHAR(150))
Is there a general, cross RDMS, way I can have a key auto generated on a JDBC insert? For example if I have a table with a primary key, id, and an int value:
create table test (
id int not null,
myNum int null
)
and do an insert
PreparedStatement statement = connection.prepareStatement("insert into test(myNum) values(?)", Statement.RETURN_GENERATED_KEYS);
statement.setInt(1, 555);
statement.executeUpdate();
statement.close();
I get an java.sql.SQLException: Cannot insert the value NULL into column 'id'.
I have a feeling this is entirely RDMS dependent. We are using using SQL Server 2005 and I have set
CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 1) ON [PRIMARY]
in the table with no luck.
This is completely database dependent. There are two main options: 1 - DBMSs that allow an auto-increment keyword next to the primary key definition and 2 - DBMSs that provide sequence generators (that you then can use to generate the new values for the PK, for instance by writing a "before insert" trigger that automatically inserts the new value in the column before completing the insertion ).
As far as I know:
Firebird uses sequences
DB2 allows to define a column as "GENERATED BY
DEFAULT AS IDENTITY";
Interbase uses sequences (called generators)
MySQL has the "AUTO_INCREMENT" clause
Oracle uses sequences
PostgreSQL uses sequences
SQLServer has the "IDENTITY(1,1)" clause
You need to set the id column in the test table to autocreate an identity. In the case of SQL Server, you need to set the IDENTITY() property on the ID column.
This is database dependant. Oracle requires a SEQUENCE to be created and on MySQL you just set the column as auto increment.
You could always use Hibernate.
As far as I know it's database dependent. Likewise with inserting timestamps; some will insert the current time when you insert a null.