I want to run following query in sql server. This is hibernate query it is working in my sql. I want to run it on sql server 2005. What should I do for that ??
strQuery = "from TempData where englishLex=:englishLex and category =:category and language=:language and login=:login and domain=:domain group by (concat(englishlex,category)) ";
concat() is a valid function in HQL.
It looks like the case is incorrect on englishLex which is causing the issue.
try the following...
strQuery = "from TempData where englishLex=:englishLex and category =:category and language=:language and login=:login and domain=:domain group by (concat(englishLex,category)) ";
In Sql Server you don't have the concat function, instead you could simply use a + sign. Have you tried that?
strQuery = "from TempData where englishLex=:englishLex and category =:category and language=:language and login=:login and domain=:domain group by (englishlex + category) ";
As the official manual says, we can use || or concat to combine two Strings.
In my case the following is working in SQL Server 2012:
Query query = currentSession().createQuery("SELECT DISTINCT u FROM UserBean u WHERE CONCAT (u.name, ' ', u.surname) like :fullname");
Mind that UserBean is the Java bean class name, and as it's in Java code, must be exactly same as the class name.
Related
I am new in the database side. My Question is, how to append a piece of query as param arguments. In my logic we are trying to append query using below
GET_DATA= "SELECT [:metrics] from table name"
am passing metrics as argument like below
paramMap.put("metrics", "name,age");
when am executing the query using query runner.
ResultSet rs = queryRunner.runQuery(context, GET_DATA, paramMap, RESULT_SET_HANDLER);
The output of the query like below
SELECT 'name,age' from table name
How can i avoid single quotes from the query?
I have tried these two changes but getting SQL error
[metrics]
metrics
If we put similar syntax with a condition like where colum_name=[:abc],its work fine for me.
Expecting a better solution.
Try this:
String columns = paramMap.get("metrics");
GET_DATA = "Select" +
columns +
" FROM" +
" name";
I have a connection with oracle like this:
Connection con=getConnection();
String query="select ?, ? from my_table";
PreparedStatement p=con.prepareStatement(query);
p.setString(1, "id");
p.setString(2, "lastName ");
ResultSet rs=p.executeQuery();
Everything is ok here but I'am wondering how doing this when you don't have a fixed number of parameters you want to select.
For example, in above, I want to select only 'id' and 'lastName'. But those parameters are taken from different Java method and depends on the situation that method can return a different number of parameters which I would like to pass to my SQL select.
I don't know the parameters in advance so once I can get only 'id' and 'lastName' but next time 'id','firstName','address' and 'city'. Then I want to pass them to SQL query.
Can anyone tell me how to use parameters in sql query in such case?
When you don't know how much parameter you required. Then make a program that accept String array/list then join them comma delimiter. This should solve your problem.
e.g.
String paramsList = new String[]{"id", "first_name", "last_name"};
If you are using Java8 you can use String.join like :
String params = String.join(",", paramsList);
Then write your sql statement like
String query="select " + params + " from my_table";
I am trying to do the following query:
String query = "SELECT * FROM EMP WHERE UCASE(LAST_NAME) ";
query += "LIKE '" + lastName.toUpperCase() + "%'";
in an example of usage of an servlet to access to a database
But I am getting the error message:
Excepcion java.sql.SQLSyntaxErrorException: ORA-00904: "UCASE": invalid identifier
On the other hand, when I use the UPPER sql function, the example works but the results do not show the values of the LASTNAME column in uppercase. I do not understand what happens.
You're just comparing the upper case values, but you're selecting the actual values with select *
to get the uppercase name in your resultset you need to use UPPER in your select list, not UCASE, like this:
String query = "SELECT UPPER(LAST_NAME) AS UPPERNAME, * FROM EMP WHERE UPPER(LAST_NAME) ";
query += "LIKE '" + lastName.toUpperCase() + "%'";
What your code is doing here is building a query string named query. Once query is complete, it will be sent to the database for parsing and running.
When you are building a query to the database, you have to use the built-in database functions for the part of the query that the database is going to parse and run. So, in your example, Java is doing toUpperCase on lastName and then putting that literal into the query string that will go to the database. UPPER(LAST_NAME) is going into the query string as is, it will get passed to the database just like that and run by the database. So it needs to be a function that the database can parse and run: an Oracle function, not a Java function.
UCASE is a DB2 function & not Oracle. For Oracle, you need to use UPPER .
Second part of your question is already answered by James Z.
Having said that, I am answering because previous answers didn't pointed out SQL injection problem with the way you listed your query.
Make it a habit to always execute parametrized queries with jdbc & not by directly appending values to query string.
String query = "SELECT * FROM EMP WHERE UCASE(LAST_NAME) LIKE ? ";
Your parameter would be - lastName.toUpperCase()+"%"
SQL Injection
how to add a set parameter() metheod inside the inner query in hibernate?
I have try to do like this but already have a errors
this is my code
Query query=session.createQuery("select eq.euipmentName,eq.type from Euipment eq where eq.id in(select euipment from Quotation qt where qt. supQuotation=:ids)");
query.setParameter("ids",id);
list = (List<Euipment>)query.list();
I've done some corrections about your query:
1. qt. supQuotation has a space, I've removed
2. euipment in you sub query haven't alias, I add qt
String hql =
"select eq.euipmentName,eq.type " +
" from Euipment eq " +
" where eq.id in (select qt.euipment from Quotation qt where qt.supQuotation = :ids)";
Query query = session.createQuery(hql);
query.setParameter("ids",id);
list = (List<Euipment>)query.list();
Tell me, if it's OK
If not OK, please post here the error, and check if you have put in hibernate mappping file your classes
From Hibernate Documentation:
Execution of native SQL queries is controlled via the SQLQuery
interface, which is obtained by calling Session.createSQLQuery().
createQuery() creates Query object using the HQL syntax.
createSQLQuery() creates Query object using the native SQL syntax.
So replace createQuery with createSQLQuery for native SQL query.
Try with Criteria
Criteria c = getSession().createCriteria(Euipment.class, "e");
c.createAlias("e.quotation", "q"); // inner join by default
c.add(Restrictions.eq("q.supQuotation", id));
list = (List<Euipment>)c.list();
I am trying below query in HQL but I get no results. Can someone help me on why I don't get any results? I tried to query the DB directly (please see SQL below) and i get 12 records. But HQL gives me 0 records.
"cause" I input the following String - "'XXX1','YYY 2'"
DB I use is Pracle 11g.
String queryStr = "from DefectsTran t join t.defects d where d.releaseName=:rel and t.defectCause in :cause and t.latestRecord=:lastrec";
Query q = session.createQuery(queryStr);
q.setString("rel", release);
q.setString("cause", filter2);
q.setString("lastrec", "Y");
SQL query that works fine when I use in TOAD.
select count(*)
from QC10.defects_tran t
inner join QC10.defects on DEFECT_ID_FK_DT = RECORD_ID
where
DEFECT_CAUSE in ('Data Request Issue', 'Functioning as Expected', 'User Education Required', 'Test Script Incorrect', 'Test Specific')
and t.latest_record = 'Y'
Instead of q.setString("cause", filter2); use q.setParameterList("cause", filter2);. filter2 has to be of a Collection subtype. Please read more about other overloading available for setParameterList: http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html