Java Spring JdbcTemplate where Clause not working - java

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..... :-(

Related

Sqlite-JDBC update with LIMIT clause?

I am trying to use the update query with the LIMIT clause using sqlite-JDBC.
Let's say there are 100 bob's in the table but I only want to update one of the records.
Sample code:
String name1 = "bob";
String name2 = "alice";
String updateSql = "update mytable set user = :name1 " +
"where user is :name2 " +
"limit 1";
try (Connection con = sql2o.open()) {
con.createQuery(updateSql)
.addParameter("bob", name1)
.addParameter("alice", name2)
.executeUpdate();
} catch(Exception e) {
e.printStackTrace();
}
I get an error:
org.sql2o.Sql2oException: Error preparing statement - [SQLITE_ERROR] SQL error or missing database (near "limit": syntax error)
Using
sqlite-jdbc 3.31
sql2o 1.6 (easy database query library)
The flag:
SQLITE_ENABLE_UPDATE_DELETE_LIMIT
needs to be set to get the limit clause to work with the update query.
I know the SELECT method works with the LIMIT clause but I would need 2 queries to do this task; SELECT then UPDATE.
If there is no way to get LIMIT to work with UPDATE then I will just use the slightly more messy method of having a query and sub query to get things to work.
Maybe there is a way to get sqlite-JDBC to use an external sqlite engine outside of the integrated one, which has been compiled with the flag set.
Any help appreciated.
You can try this query instead:
UPDATE mytable SET user = :name1
WHERE ROWID = (SELECT MIN(ROWID)
FROM mytable
WHERE user = :name2);
ROWID is a special column available in all tables (unless you use WITHOUT ROWID)

How do I write join in Ebean Java ORM language

Currently my code is
int customerId = 4;
String sql = "select id from coupon as A join coupon_use "
+ "as B on A.id=B.coupon where B.customer=" + customerId
+ " and B.like_at is not null;";
RawSql rawSql = RawSqlBuilder.parse(sql).create();
Query<Coupon> query = Ebean.find(Coupon.class);
query.setRawSql(rawSql);
List<Coupon> list = query.findList();
return ok(Json.toJson(list));
How do I avoid writing manual sql query but still have the ORM generate that query and return me the result?
Ebean will add appropriate joins based on the paths/properties used in the where and order by etc.
where couponUse.likeAt is not null.
Assuming couponUse.likeAt is the correct expression path ... Ebean will add a join to support the expression automatically.

querying a view in oracle using java code with a where condition on a date field

I need to connect to a view in oracle view which contains information about certain matrix population in other tables.
The structure of the view (v_matrix) is:
PERIOD_START_TIME NOT NULL DATE,
MATRIX_TABLE VARCHAR2(20),
MATRIX_NAME VARCHAR2(20),
COMPLETION_TIME DATE
I am using a java code using jdbc to connect to the view.
The query which i Have is:
String query = "Select * from v_matrix where PERIOD_START_TIME LIKE \'2016-04-18 17:%\' AND MATRIX_TABLE = \'" + tableName + "\' AND MATRIX_NAME IN (" + inClauseStr.toString() + ")";
The problem is that the query fetches nothing while I can see around 200 records in the view with PERIOD_START_TIME in order of 17th hour on 18th April 2016.
The same query works fine in SQL Developer
I tried googling bud could not find any tutorial on how to filter records on a date field using java code.
I tried changing the query to
String query = "Select * from v_kpi_availability where TO_CHAR(PERIOD_START_TIME,\'YYYY-MM-DD HH24:MI:SS\') LIKE \'2016-04-18 17:%\' AND KPI_TABLE = \'" + tableName + "\' AND KPI_NAME IN (" + inClauseStr.toString() + ")";
But the query still does not fetch any records
PS: The database is Oracle 11g (11.2.0.4)
Surprising the code works in pre-prod environment with the following query:
String query = "Select * from v_kpi_availability where TO_CHAR(PERIOD_START_TIME,\'YYYY-MM-DD HH24:MI:SS\') LIKE \'2016-04-18 17:%\' AND KPI_TABLE = \'" + tableName + "\' AND KPI_NAME IN (" + inClauseStr.toString() + ")";
The reason its not working in lab because lab is on Oracle 10g where the implementation of date is a bit different.

Number of characters in sql query result

I am using Spring 3.1.1 with Hibernate 4 and a MSSQL database. Finally, I have been able to query my database with joins in my table that returns the correct answers. Although, it seems that it does not return the entire strings of my messages, but cuts at 29/30 digits. Here is my query:
SQLQuery sql = this.getCurrentSession().createSQLQuery(
"SELECT event.id as eventid, CAST(event_type.type_name AS varchar) as eventtype, event.check_date, event.event_date, event.status, CAST(event_message.message AS varchar) as eventmessage " +
"FROM event_log event " +
"LEFT JOIN event_type " +
"ON (event.event_type_id = event_type.id) " +
"LEFT JOIN event_message " +
"ON (event.event_message_id = event_message.id) " +
"WHERE event.event_type_id = " + jobId +
"ORDER BY eventid");
The result can be:
4506 Database 2014-01-15 14:14:15.02 2014-01-15 14:14:15.02 false Database-connection cannot be
Where the columns are id, task_name, check_date, event_date, status and the message-string at the end. .
The result goes to a ArrayList<Object[]> where I read row[0] etc. to get the entities in the row. The string message gets cut after 29 digits, which is disturbing. Why does it cut the string and how can I fix this? In my database the string exists in it's full and is entitled 400 characters max.
I know this is probably not the best way to query my database, but it will do for my application since it works.
You are using varchar without a length. Never do this!
Replace:
CAST(event_type.type_name AS varchar)
With something like:
CAST(event_type.type_name AS varchar(255))
In some contexts, the default length is 32. In some it is 1. In general, though, always specify the length.
EDIT:
Actually, you probably don't even need the cast. Why not just have event_type.type_name in the select list?

Getting org.hibernate.hql.ast.QuerySyntaxException though it works fine sql server?

i have below code snippet. It throws the exception at line 3 but query works fine managemnt studio(sql server 2005)
String query = "select * from user where userId=" + profileId
+ " and spaceName='" + spaceName + "'";
Session session = HibernateUtil.getSession();
List<PersonDetailsData> personDetailsData = new ArrayList<PersonDetailsData>(
session.createQuery(query).list()); //line 3
Here is the exception
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near
line 1, column 8 [select * from user where userId=216 and
spaceName='DIG']
I am not able to figure out what's the problem with query when it is running fine in management sudio?
It's native query, not hql.
If you have mapped table field to class fields you need
session.createSQLQuery(query, PersonDetailsData.class).list();
or create hql type query -
select p from PersonDetailData p where p.userId = :userId and p.spaceName =:spaceName
and use parameters in query, not direct values.
As you are using sql query so you have to create a sql query such as
sess.createSQLQuery("SELECT * FROM CATS").list();
see the source source

Categories

Resources