I have a DB2 MERGE INTO statement that gives me a sql syntax exception when I try to execute it from Java, but when I put in the parameters in my SQL tool it runs just fine. I've run this repeatedly both in my SQL tool and in Java, and it always works in the former but fails in the latter. This is the SQL (table and column names have been changed to protect the innocent):
merge INTO sales_table AS target
USING (VALUES ( CAST('2020' AS CHAR(4)), CAST('12' AS CHAR(2)), CAST('AB01' AS CHAR(4)), CAST
(0555.0 AS
DECIMAL(9)), CAST(0.000 AS DECIMAL(9)), CAST('DP079616' AS CHAR(8)) )) AS input_data (
year, month, location, no, orig_no, last_upd_userid )
ON ( target.year = input_data.year
AND target.month = input_data.month
AND target.location = input_data.location )
WHEN matched THEN
UPDATE SET no = input_data.no,
orig_no = input_data.orig_no,
last_upd_userid = input_data.last_upd_userid,
last_upd_tmstmp = CURRENT TIMESTAMP
WHEN NOT matched THEN
INSERT ( year,
month,
location,
no,
orig_no,
creation_userid,
creation_tmstmp,
last_upd_userid,
last_upd_tmstmp )
VALUES ( input_data.year,
input_data.month,
input_data.location,
input_data.no,
input_data.orig_no,
input_data.last_upd_userid,
CURRENT TIMESTAMP,
input_data.last_upd_userid,
CURRENT TIMESTAMP )
In the query above I have replaces the ?s with hard-coded parameters that Java sets with the set... methods.
I get this exception in Java:
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-270, SQLSTATE=42997, SQLERRMC=null, DRIVER=4.21.29
This is the relevant Java part:
req.setString(i++, roaa.getYear());
req.setString(i++, roaa.getMonth());
req.setString(i++, roaa.getLocatoin());
req.setFloat(i++, Float.parseFloat(roaa.getSalesNumber()));
req.setFloat(i++, Float.parseFloat(roaa.getOrigSalesNumber()));
req.setString(i++, userId);
req.executeUpdate();
Any thoughts on why I'd get a syntax exception on something that works in my SQL tool? Obviously the syntax is right if it works. I've Googled the SQL code -270, and various results say it's because it violates a constraint somewhere, but again, it works in the SQL tool, so I can't help thinking I'm overlooking something simple that should be obvious to me.
Related
While executing Sybase procedure from JDBC, I am getting below error:
Execute cursor is declared on a procedure which contains a non-SELECT
or a SELECT with COMPUTE clause. for the declaration of this cursor to
be legal it should have a single select statement without a compute
clause
I am using JCONN4 sybase jar. Does sybase has such restrictions on procedure to not have select statement with compute clause?
Also I searched in Sybase documentation but couldn't get proper answer.
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.39996_1250/html/svrtsg/svrtsg348.htm
I cannot post the procedure here, but I can post the sample
create proc sample (#value_date datetime = null) as
begin
if #value_date is null
select #value_date = some_column from some_table
select a,b,c,d into #ad
from
table_a where a='something'
select a,b,c,d case when a=0 then 0 else b/a
from #ad
end
The above procedure is called using sybStatemt.executeQuery function
Looks like its Sybase bug. steps to reproduce the issue
Create a procedure having select with compute clause as i described above
write jdbc program and use belew method
statement.setFetchSize(1000);
Execute the program and you will see the error
now the question is does Sybase really has these kind of restrictions or it is specific to their Driver only and we can say its driver issue ?
You must use CallableStatement when calling store procedure
If you execute a stored procedure in a CallableStatement object that represents parameter values as question marks, you get better performance than if you use both question marks and literal values for parameters. Also, if you mix literals and question marks, you cannot use output parameters with a stored procedure.
The following example creates sp_stmt as a CallableStatement object for executing the stored procedure MyProc:
CallableStatement sp_stmt = conn.prepareCall( "{call MyProc(?,?)}");
The two parameters in MyProc are represented as question marks. You can register one or both of them as output parameters using the registerOutParameter methods in the CallableStatement interface.
In the following example, sp_stmt2 is a CallableStatement object for executing the stored procedure MyProc2.
CallableStatement sp_stmt2 = conn.prepareCall( {"call MyProc2(?,'javelin')}");
Run your sp from sybase command prompt.
If it gives result it should work with sybase driver.
I have used ado.net driver in c# it can run similar queries
https://marketplace.visualstudio.com/items?itemName=CDATASOFTWARE.SybaseADONETProvider
Your Sp looks simple. But i think your sp had some runtime issue.
I think this line
if #value_date is null
select #value_date = some_column from some_table
should be
if #value_date is null
select #value_date = some_column from some_table where col1='kkk' so that only
one value comes
I have a PreparedStatement intended to be run both on ORACLE and on MYSQL.
But I cannot figure out how to handle the CAST(NULL AS ...)
On Oracle the following works (but not on Mysql):
SELECT TIMB_INS,
CAST(NULL AS TIMESTAMP) AS TIMB_CLO
FROM TOPS
On Mysql the following works (but not on Oracle):
SELECT TIMB_INS,
CAST(NULL AS DATETIME) AS TIMB_CLO
FROM TOPS
(Please note that the first column selected, "TIMB_INS", returns the correct data type for target database type in both cases, i.e. TIMESTAMP for Oracle and DATETIME for MySql.)
There is a way to put it so that it works for both?
I.E. Can i make it db-indipendent in some way?
Thanks
Marco
Based on the tags I can see you're calling this statement from some java code. There are several ways doing so:
Use the DAO pattern. I.e. for each SQL flavor provide a java file that contains the SQL-s.
Use an ORM like Hibernate or JPA. That will take care of this kind of differences.
As a quick hack, you can edit the SQL manually, like in the snippet below. But then you have to determine somehow if the underlying database is Oracle or MySQL
String SQL_PATTERN = "... CAST(NULL AS %s) AS TIMB_CLO ...";
String SQL = String.format(SQL_PATTERN, isOracle ? "TIMESTAMP" : "DATETIME");
I ran the query in both sql Workbench and in the executeUpdate() method in java:
in Workbench:
INSERT INTO recentsearches (name) VALUES ("blah");
in java:
statement.executeUpdate("INSERT INTO recentsearches (name) VALUES (\""+name+"\""));
Assuming name = "blah". But I get a syntax error from running the query in java, I've already checked the string value for name. It definitely comes up as "blah", and I didn't forget the speech marks around string values, yet I still get a syntax error.
The error I get in my console is:
check the manual that corresponds to your MySQL server version for the
right syntax to use near '' at line 1
Try to use:
"INSERT INTO recentsearches (name) VALUES("+name+")";
My advice, use PreparedStatement because it has:
-Precompilation and DB-side caching of the SQL statement leads to overall faster execution and the ability to reuse the same SQL statement in batches.
-Automatic prevention of SQL injection attacks by builtin escaping of quotes and other special characters. Note that this requires that you use any of the PreparedStatement setXxx() methods to set the values
How to make query like this in Java and get the results:
SELECT filedata.num,st_area(ST_Difference(ST_TRANSFORM(filedata.the_geom,70066),filedata_temp.the_geom))
FROM filedata, filedata_temp
Where filedata.num=filedata_temp.num
Or, I think will be better if I create procedure in Postgres from this query.
CREATE OR REPLACE FUNCTION get_geom_difference()
RETURNS void AS
$$
BEGIN
SELECT filedata.num,st_area(ST_Difference(ST_TRANSFORM(filedata.the_geom,70066),filedata_temp.the_geom))
FROM filedata, filedata_temp
Where filedata.num=filedata_temp.num
end;
$$
LANGUAGE 'plpgsql'
and call it
Connection ce_proc= null;
ce_proc = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgis","postgres","123456");
java.sql.CallableStatement proc = ce_proc.prepareCall("{get_geom_difference()}");
proc.execute();
proc.close();
ce_proc.close();
But how to get results from this procedure in Java?
UPDATE
I tried this SP
DROP FUNCTION get_geom_difference();
CREATE OR REPLACE FUNCTION get_geom_difference()
RETURNS integer AS
$$
DECLARE
tt integer;
BEGIN
SELECT filedata.num INTO tt
FROM filedata
Where filedata.num=1;
RETURN tt;
END;
$$
LANGUAGE 'plpgsql'
and call
Class.forName("org.postgresql.Driver");
Connection connect= null;
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgis","postgres","123456");
java.sql.CallableStatement proc = connect.prepareCall("{?=call get_geom_difference()}");
proc.registerOutParameter(1, java.sql.Types.INTEGER);
proc.executeQuery();
ResultSet results = (ResultSet) proc.getObject(1);
and got an error:
org.apache.jasper.JasperException: An exception occurred processing
JSP page /commit_changes.jsp at line 25in lineproc.executeQuery();
root cause javax.servlet.ServletException:
org.postgresql.util.PSQLException: No results were returned by the
query
But query
SELECT filedata.num
FROM filedata
Where filedata.num=1;
returns 1.
Where is mistake?
You can largely simplify the function. (Keeping simplistic function for the sake of the question.)
CREATE OR REPLACE FUNCTION get_geom_difference()
RETURNS integer AS
$BODY$
SELECT num
FROM filedata
WHERE num = 1
LIMIT 1; -- needed if there can be more than one rows with num = 1
$BODY$ LANGUAGE SQL;
Though, technically, what you have in the question would work, too - provided the data type matches. Does it? Is the column filedata.num of type integer? That's what I gather from the example. On your other question I was assumingnumeric for lack of information. At least one of them will fail.
If the return type of the function doesn't match the returned value you get an error from the PostgreSQL function. Properly configured, your PostgreSQL log would have detailed error messages in this case.
What do you see, when you create the above function in PostgreSQL and then call:
SELECT get_geom_difference(1);
from psql. (Preferably in the same session to rule out a mixup of databases, ports, servers or users.)
Calling a simple function taking one parameter and returning one scalar value seems pretty straight forward. Chapter 6.1 of the PostgreSQL JDBC manual has a full example which seems to agree perfectly with what you have in your question (My expertise is with Postgres rather than JDBC, though).
There are quite a few different CallableStatement constructors, but only two of them let you get results back.
A ResultSet is returned by CallableStatement.executeQuery(). There's a good complete example in the link above.
I don't know if getting a scalar result back from a CallableStatement is legal. I'd expect PgJDBC to translate it to a rowset of one row, though, so it should work.
Your query example is typical. So what you will need is
Java Database Connectivity (JDBC)
and everything needed to serv it, is in package java.sql
So at this point I recoemnd you to read some tutorial and if you have some particular problem write about it on SO.
You will need JDBC to do that. You should be able to find all JDBC related information here.
Take a look here for a more detailed tutorial on how to connect your Java application to your PostgreSQL.
works 100% java 7 and postgres pgadmin 2016, Use createNativeQuery In your transaction write this
and change myschema.mymethodThatReturnASelect
for the scheme and the name of your function.
#Override
public List<ViewFormulario> listarFormulario(Long idUsuario) {
List<ViewFormulario> list =null;
try {
Query q = em.createNativeQuery("SELECT * FROM myschema.mymethodThatReturnASelect(?);");
q.setParameter(1, idUsuario);
List<Object[]> listObject = (List<Object[]>) q.getResultList();
if (listObject != null && !listObject.isEmpty()) {
list = new ArrayList<>();
for (Object o[] : listObject) {
ViewFormulario c = new ViewFormulario();
c.setIdProyecto(o[0] != null ? Long.valueOf(o[0].toString()) : -1L);
...etc...etc.
I have a stored procedure that adds an object to the database and returns the generated ID number.
For reference, its basic structure goes something like this:
ALTER PROCEDURE [dbo].[myProc]
#Name nvarchar(50),
#Creator nvarchar(50),
#Text nvarchar(200),
#Lat float,
#Lon float,
#myID int OUTPUT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO myTable --# blah blah blah
SELECT #myID = scope_identity(); --# grab the auto-inc key from myTable
INSERT INTO anotherTable --# blah blah blah
END
I ran this in SQL Server Management Studio and verified that it worked correctly.
Now I want to call that stored procedure from Java. I wrote this code to do it:
CallableStatement cs = con.prepareCall("EXEC myProc "
+ "#Name = ?, #Creator = ?, #Text = ?, #Lat = ?, #Lon = ?, #myID = ? OUTPUT");
cs.setString(1, aString);
cs.setString(2, anotherString);
cs.setString(3, yetAnotherString);
cs.setFloat(4, aFloat);
cs.setFloat(5, anotherFloat);
cs.registerOutParameter(6, java.sql.Types.INTEGER);
cs.execute();
But the execute() call throws an exception:
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Error converting data type nvarchar to int.
What is going wrong here? Why is it even trying to convert an nvarchar to an int? I'm not even trying to fetch the return value via getInt() yet (that comes on the next line).
What I've tried:
Building the query by string manipulation to check whether the problem could possibly be in the input parameters. Same exception. At least that narrows the problem down.
Changing the type of the output parameter to Types.NVARCHAR, just in case. But that's not even supported by the JdbcOdbcDriver which I am using.
Messing around with the call syntax (I haven't used SQL Server with Java before). Always ended up with syntax errors. This included trying to make myID a return value instead of an output parameter.
Searching Google. A lot. Ended up with a bunch of unhelpful forum threads and EE "answers".
Now I'm stumped. Is it really this hard or am I just missing the obvious?
Your code looks ok. The only thing that is wrong is that "float" in SQL Server maps to double in Java. "real" in MS SQL maps to float in Java. But this does not produce the problem.
The problem is the buggy ODBC Bridge. You should use a type 4 JDBC driver for MS SQL. If you want to use the ODBC Bridge then you can test setting a value for the 6th parameter:
cs.setInt(6, 0);
But I does not know if this will work. The message means that the driver is using the data type NVARCHAR for the last parameter. It look like the ODBC Bridge does not forward the type with registerOutParameter.
Can't test your code since I don't have SQL Server, but you can try changing to String since this is Java equivalent for NVARCHAR.