I'm facing an issue executing a very long query using spring jdbcTemplate. The query contains multiple DECLARE statements, multiple CREATE temp tables and in the end a SELECT statement which gets data by joining multiple tables as well as DROP statements. When I run this query directly in SQL Management studio, the query runs perfectly fine and returns me the data. However when I run the exact same query using spring's jdbc template there is no data returned.
Edit: I can not post exact query I'm using. But below is the template of how the query may look.
public class SpringJdbcExample{
public static final String sql = "DECLARE "+
"#XYZ DATETIME='1/31/2016', " +
"#ABC UNIQUEIDENTIFIER='', " +
"#var1 VARCHAR (1)='F', " +
"#var2 UNIQUEIDENTIFIER = NULL " +
"DECLARE #id1 INT, #id2 INT, #id3 Int "+
"SELECT #id1=(SELECT id1 FROM table1 WHERE XYZ=#XYZ) "+
"SELECT #id2=(SELECT id2 FROM table2 WHERE ABC=#ABC) " +
"SET NOCOUNT ON "+
"SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED "+
"CREATE TABLE #temp1 (var1 INT, var2 INT, var2 INT, var4 varchar(20) ) "+
"CREATE TABLE #temp2 (var1 INT,var2 INT, var3 DATETIME, var4 varchar(20)) "+
-- More Create temp tables
-- Insert into temp tables from various DB tables
-- single Select statement from temp tables
-- Drop temp tables
"; // end of the string
private JdbcTemplate jdbcTemplate;
public SpringJdbcExample (JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
public List<String> getData(){
// MapSqlParameterSource
MapSqlParameterSource params = new MapSqlParameterSource("someParams", "someValues");
jdbcTemplate.query(sql,params);
}
Related
This is my database:
dragons
id, key, name, age, creation_date
users
id, name, user, pass
users_dragons
user_id, dragon_id
So this is my code for deleting dragons from the database that have a bigger key that the passed and belongs to a determination user. The SQL query works perfectly for deleting them but not for returning the array of keys from the deleted elements.
I tried using PreparedStatement but later I checked, as far as I know, that this class doesn't return arrays, and the CallableStatement is only for executing processes in the db, and I don't know how they return arrays.
String query = "" +
"DELETE FROM dragons " +
"WHERE id IN (SELECT d.id FROM dragons d, users u, users_dragons ud" +
" WHERE d.key > ?" +
" AND ud.dragon_id = d.iD" +
" AND ud.user_id in (select id from users where id = ?)) RETURNING key INTO ?";
CallableStatement callableStatement = connection.prepareCall(query);
int pointer = 0;
callableStatement.setInt(++pointer, key);
callableStatement.setInt(++pointer, credentials.id);
callableStatement.registerOutParameter(++pointer, Types.INTEGER);
callableStatement.executeUpdate();
return (int []) callableStatement.getArray(1).getArray();
The code is giving me the error, but is obvious because the CallableStatement needs a postgres function to run and not a simple SQL query
org.postgresql.util.PSQLException: This statement does not declare an OUT parameter.
Use { ?= call ... } to declare one.
at org.postgresql.jdbc.PgCallableStatement.registerOutParameter
.......
It would be really helpful how would be the correct JDBC algorithm to delete the elements from the database and return the array of keys of the deleted items.
You treat such a statement like a normal SELECT statement: use java.sql.PreparedStatement.executeQuery() or java.sql.Statement.executeQuery(String sql) to execute the statement and get a result set.
java.sql.CallableStatement is for calling Procedures (but you don't need it in PostgreSQL).
I'm struggling writing a simple sql statement using JdbcTemplate. The where clause is simply not working.
private static final String SELECT_CLAUSE =
"SELECT count(*) " +
"FROM AUSSCHUETTUNG_AUD " +
"WHERE rev = 20008907 ";
Integer count = jdbcTemplate.queryForObject( SELECT_CLAUSE, Integer.class );
The result of the count is 0, instead of 1.
Without where clause, the count is 1200.
The column rev has a value of 20008907.
When I do change the where clause as follow "where 1=1", then 1200 is returned.
The DB is Oracle (oracle driver version 8).
Here is the output when running the sql in "Oracle SQL DEVELOPER"
I found the reason why the where clause was not working...... I was connecting to a different DB from within the application. My mistake..... :-(
I am trying to run an SQLquery in a java application. The sqlquery connects two databases (not on the same server). Is it correct what I am doing here:
the public function has:
private DatabaseData externaldb = new DatabaseData("com.mysql.jdbc.Driver",
"...", "...", "...");
private DatabaseData localdb = new DatabaseData("com.mysql.jdbc.Driver",
"...", "...", "...");
private Connection externalconnection = null;
private Connection localconnection = null;
function(externalconnection, c_id, u_d);
the called function is:
private int function(Connection externalconnection, String c_Id, String u_Id)
throws SQLException{
String query ="SELECT A.v_id, COUNT(I.v_id) AS v_count "
+ "FROM externaldb.video_interaction I"
+ " INNER JOIN localdb.video_additional A ON A.v_id = I.v_id"
+ " WHERE I.c_id='" + c_id + "' AND I.user'" + u_Id + "';";
Statement stmt = externaldb.createStatement();
ResultSet rs = stmt.executeQuery(query);
int counter = 0;
if (rs.next()){
counter = rs.getInt("video_count");
}
return counter;
}
Thank you!
You have two connections, you have two databases. Server local must have access to external and external to local. Then, create FEDERATED table, for example:
CREATE TABLE federated_table (
id INT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(32) NOT NULL DEFAULT '',
other INT(20) NOT NULL DEFAULT '0',
PRIMARY KEY (id),
INDEX name (name),
INDEX other_key (other)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://fed_user#remote_host:9306/federated/test_table';
(Before MySQL 5.0.13, use COMMENT rather than CONNECTION.)
Source from: MySQL Cross Server Select Query
More informations: https://dev.mysql.com/doc/refman/5.7/en/federated-create-server.html
I have to do remove the row (containing the userId) in the table "USERS". This is my query:
#SqlUpdate("delete from USERS where userId = :userId ")
void removeUser(#Bind("userId") String userId);
But first I want to remove that user from the table "USERS_DATA" (that is a daughter of USERS) which also contain the "userId". How can I do? I've tried this:
#SqlUpdate("delete from USERS_DATA where userId = :userId " +
" and delete from USERS where userId = :userId")
void removeUser(#Bind("userId") String userId);
but console tell me: java.sql.SQLSyntaxErrorException: ORA-00936: missing expression
Unlike some other RDBMS, Oracle does not allow you to pass two statements in the same SQL command (this helps to prevent SQL injection).
You can try using wrapping both queries in an anonymous PL/SLQ block:
BEGIN
delete from USERS_DATA where userId = :userId;
delete from USERS where userId = :userId;
END;
/
This will allow you to execute both DML statements together as they are part of the singular containing PL/SQL block.
Unfortunately, I am not familiar with that annotation syntax in Java so I cannot help you convert it to Java but I would guess at:
#SqlUpdate("BEGIN " +
"delete from USERS_DATA where userId = :userId; " +
"delete from USERS where userId = :userId; " +
"END;")
void removeUser(#Bind("userId") String userId);
Alternatively, you can create a procedure in Oracle:
CREATE OR REPLACE PROCEDURE delete_user(
in_userID USERS_DATA.USERID%TYPE
)
AS
BEGIN
DELETE FROM USERS_DATA WHERE userId = in_userId;
DELETE FROM USERS WHERE userId = in_userId;
END;
/
And you can then just call this procedure.
In my current project, I have a function with argument (e.g., int badgID in the following code snippet). This function connects with Apache Derby database, creates table (e.g., FIRSTTABLE), then query to FIRSTTABLE table. The query statement uses function argument for query (e.g., ID = $badgeID ). My question:
Is ID = $badgeID the right way from a syntax point of view?. I have tried this case, but it is not working.
public void getprofile (int badgeID) {
// Create connection with Apache-Derby Database.
// Create table in Apache Derby datbase.
String createString = " CREATE TABLE FIRSTTABLE "
+ "(ID INT PRIMARY KEY, "
+ "PREF INT, "
+ " NAME VARCHAR(12))";
// SQL query on table
querystmt = "SELECT * FROM FIRSTTABLE WHERE ID = $badgeID"
}
that's php syntax...
in java you would write
String querystmt = "SELECT * FROM FIRSTTABLE WHERE ID = " + badgeID;