Java DbUtils MySQL How set param when call a stored procedures - java

I have a probleme with call a procedure, I want to set two paramams. In procedure first param have to be a int and secod time. I did this :
beans = (List) qRunner.query(conn, "call mpklocal.LCD_GetDispInfoChange_TEST(?, CURTIME() )", timtableId ,
new BeanListHandler(AnotherBusInBusStop.class));
when I close a program I see this :
Java.sql.SQLException: 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 'SELECT CURTIME())' at line 1 Query: call mpklocal.LCD_GetDispInfoChange_TEST(?,SELECT CURTIME()) Parameters: [657983]
at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:392)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:351)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:180)

Related

MariaDB SQL Syntax error from SquirelL SQL Client

I am using SQuirelL client to connect to MariaDB. My OS is Ubuntu. I have downloaded the Mariadb driver (mariadb-java-client-1.5.2.jar) to the proper location and linked it in the SQuirelL client. I have setup a database, and am able to create tables in it.
But things go south when i try creating any object where i use a DELIMITER. I have even tried with the mysql driver, mysql-connector-java-5.1.38.jar. but same error.
this is my SQL -
DELIMITER //
CREATE FUNCTION FortyTwo() RETURNS TINYINT DETERMINISTIC
BEGIN
DECLARE x TINYINT;
SET x = 42;
RETURN x;
END
//
DELIMITER ;
and this is the error -
Error: 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 'DELIMITER //
CREATE FUNCTION FortyTwo() RETURNS TINYINT DETERMINISTIC
BEGIN
DE' at line 1
Query is : DELIMITER //
CREATE FUNCTION FortyTwo() RETURNS TINYINT DETERMINISTIC
BEGIN
DECLARE x TINYINT
SQLState: 42000
ErrorCode: 1064
Error occurred in:
DELIMITER //
CREATE FUNCTION FortyTwo() RETURNS TINYINT DETERMINISTIC
BEGIN
DECLARE x TINYINT
I will greatly appreciate any help ! Thanks
DELIMITER is not a valid MariaDB SQL command.
Look at the documentation. You won't find it there.
DELIMITER is a MySQL Client command:
mysql> help
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
...
delimiter (\d) Set statement delimiter.
...
You generally don't need delimiters, since you only execute one command at a time when using JDBC.
They can be batched if needed for performance, but they should still be submitted to the JDBC driver one at a time.

MySQL: Create a trigger keeps giving me syntax error

I've been trying to create a trigger from java and it just won't work.
String trigger = String.format("CREATE TRIGGER `%s` AFTER %s ON %s BEGIN INSERT INTO `ndb_log` (`table_name`, `action`, `time`) VALUES ('%s', '%s', UNIX_TIMESTAMP()) END;",
name, this.event.toUppercase(), this.table, this.table, this.event.toLowercase());
CREATE TRIGGER `onnc_censorINSERT` AFTER INSERT ON nc_censor BEGIN INSERT INTO `ndb_log` (`table_name`, `action`, `time`) VALUES ('nc_censor', 'insert', UNIX_TIMESTAMP()); END;
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'BEGIN INSERT INTO ndb_log (table_name, action, time) VALUES ('nc_censor' at line 1
I am not running this in phpMyAdmin, a server console or anything like that. I need to run it from java in a String. I call this.mysql.update(trigger); in my class which "basically" runs it.
I have tried running the create trigger string in ssh mysql, but it also doesn't work.
Two things: (1) you're missing the FOR EACH ROW clause of the trigger declaration; (2) you don't need the BEGIN and the END, because your trigger executes just one statement. Try this instead:
CREATE TRIGGER `onnc_censorINSERT` AFTER INSERT ON nc_censor FOR EACH ROW INSERT INTO `ndb_log` (`table_name`, `action`, `time`) VALUES ('nc_censor', 'insert', UNIX_TIMESTAMP());

Java + MySQL - Syntax error but the statement is correct

My program executes INSERT query. When I run it, I get an error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'INSERT INTO words(lang1, lang2, category, lang2_code, user) SELECT 'ahoj', 'hell' at line 1
I tried to print the actual statement to the stdout:
SET #lang:='Angličtina', #categ:='Nová';
INSERT INTO words(lang1, lang2, category, lang2_code, user)
SELECT 'ahoj', 'hello', c.id, l.id, 1 FROM categories c, languages l
WHERE c.name = #categ AND l.name = #lang;
As you can see, the statement is altered in the log. 'hell' instead of 'hello'. When I copy that into the mysql command line and execute, it works just fine so I assume the problem is in the JDBC somewhere.
That's not one statement. If you want to use more than one statement at a time, don't use a PreparedStatement but for example addBatch but it seems that here you should simply issue 2 JDBC statements, one after the other.

Pagination is not working when calling MySQL Stored Procedure using Hibernate

I have a MySQL Stored Procedure click here for stored procedure and calling the Procedure using Hibernate
Hibernate Code:
int ps=5;
SQLQuery query=session2.createSQLQuery("CALL AbsentReportproc(:_fromdate,:_todate)");
query.setParameter("_fromdate", fromdate);
query.setParameter("_todate", todate);
query.setFirstResult(ps*(pno-1));
query.setMaxResults(ps);
List<Object[]> empList=query.list();
when I execute above code I'm displayed with the following Error Message:
org.hibernate.exception.SQLGrammarException: could not execute query
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'limit 5' at line 1
Note:If i remove the below statement from the code,I'm displayed with all records in a single jsp Page
query.setMaxResults(ps);
Could any one give me the solution what was the Problem?
thanks...
It is clearly a jdbc driver error that you are using;
Also try this with setFetchSize , and if this doesn't work also then,
I would suggest not to get the details out of the query in chunks if you want to display them all and there is not much data and you should rather store all the data without calling this method query.setMaxResults(ps); into a collection.
And when you want to display that data in pages, then get subList in case you are using List, to break the data at the application level and then display it on your view that is jsp in this case.

JDBC call to a synonym to Microsoft SQL Server stored procedure

Using the JDBC driver provided by Microsoft (sqljdbc4.jar) I am unable to call a stored procedure using a synonym defined for it.
I.e. for a synonym defined as:
CREATE SYNONYM dbo.synonym_name for dbo.procedure_name
when running the callable statement created by:
CallableStatement callStmt = conn.prepareCall("{ call [dbo].[synonym_name] (?,?,?,?,?,?) }");
I get an exception:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Parameter param_name was not defined for stored procedure [dbo].[synonym_name].
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerCallableStatement.findColumn(SQLServerCallableStatement.java:1217)
at com.microsoft.sqlserver.jdbc.SQLServerCallableStatement.setString(SQLServerCallableStatement.java:1563)
at testmssql.main(testmssql.java:53)
Even though the parameters are correctly set (if I call the procedure directly (bypassing the synonym) everything works fine).
Further more, if I replace Microsoft's driver with JTDS, everything works fine.
How can one run a CallableStatement using a synonym for a stored procedure with Microsoft SQL Server's JDBC driver?
SQL Server Synonyms do not have query-able metadata. Judging by the error, JDBC is trying to confirm that the parameters declared in the Java code match the parameters declared on the stored procedure. That fails because of the missing metadata.
The only way around this is to create a passthrough stored procedure instead of the synonym.
So if you have this procedure:
CREATE PROCEDURE dbo.RealProcedure
#p1 INT,
#p2 INT
AS
BEGIN
RAISERROR('TODO: implement me',16,10);
END
And you have this synonym:
CREATE SYNONYM dbo.myProcedure FOR dbo.realProcedure;
Drop the synonym and create this procedure instead:
CREATE PROCEDURE dbo.myProcedure
#p1 INT,
#p2 INT
AS
BEGIN
EXEC dbo.realProcedure #p1,#p2;
END
There is a similar issue described here: http://social.msdn.microsoft.com/Forums/en-us/sqldataaccess/thread/dcdfee17-a926-4b57-8641-ed86fec989f2

Categories