Get Where Condition From SQL Query in java - sql

how can i get exact Condition From SQL Query like below :
select * from users where name = "ned stark" order by id ;
select * from users where name = "rob stark" group by id ;
i want to get ' name = "ned stark" ' and ' name = "rob stark" ';
is there way to do this ?

You can solve your problem using simply substring:
String aa = "select * from test where a='aa' and b='bb' order by aa";
int startIndext = aa.indexOf("where") + 5;
int endIndex = aa.indexOf("group");
if(endIndex<0) endIndex = aa.indexOf("order");
if(endIndex<0) endIndex = aa.length();
String whereCondition = aa.substring(startIndext, endIndex);
System.out.println("whereCondition: " + whereCondition);

Related

how to access the string containing "=" and "AND" operator in where clause in oracle

My query is like but it' not working and giving error "SQL command not properly ended" and the string:HDR.TRX_DT = DTL.TRX_DT AND HDR.BU_TYPE = DTL.BU_TYPE AND HDR.BU_CODE = DTL.BU_CODE
AND HDR.TRX_NO = DTL.TRX_NO AND HDR.RGSTR_NO = DTL.RGSTR_NO AND HDR.TRX_TYP_CD in ('COS') is value in column . i want use that value in where clause of select statement .How will do you that .plz suggest
select * from mdbat.migration_ctrl_all where addition_condition='HDR.TRX_DT = DTL.TRX_DT AND HDR.BU_TYPE = DTL.BU_TYPE AND HDR.BU_CODE = DTL.BU_CODE
AND HDR.TRX_NO = DTL.TRX_NO AND HDR.RGSTR_NO = DTL.RGSTR_NO AND HDR.TRX_TYP_CD in ('COS')';
Escape the ' delimiting the COS string, as said by Alex Larionow. But escape each one with another '
select * from mdbat.migration_ctrl_all where addition_condition='HDR.TRX_DT = DTL.TRX_DT AND HDR.BU_TYPE = DTL.BU_TYPE AND HDR.BU_CODE = DTL.BU_CODE
AND HDR.TRX_NO = DTL.TRX_NO AND HDR.RGSTR_NO = DTL.RGSTR_NO AND HDR.TRX_TYP_CD in (''COS'')';

Eclipselink NamedNativeQuery pass column name as parameter and not a value

Trying to pass column name as parameter but JPA sets it as a value surrounding it with single quotes.
#NamedNativeQueries({
#NamedNativeQuery(
name = "Genre.findAllLocalized",
query = "SELECT "
+ " CASE "
+ " WHEN ? IS NULL THEN genre_default"
+ " ELSE ? "
+ " END localized_genre "
+ "FROM genre ORDER BY localized_genre")
})
Then:
List<String> res = em.createNamedQuery("Genre.findAllLocalized")
.setParameter(1, colName)
.setParameter(2, colName)
.getResultList();
The problem is that the column names being passed are taken as values so the result will return result list with repeated values of "col_name" instead of selecting the value of the column passed as parameter.
Is this achievable?
Basically it makes no sense to create a prepared query like this, how would you name that query anyway: "*"? So the short answer is: no.
But you could create named queries dynamically if this matches your requirement:
String colName = "colName";
String query = "SELECT WHEN " + colName + " IS NULL THEN genre_default";
Query query = entitymanager.createQuery(query);
Probably using a criteria builder is more the way you want to use JPA (code from https://en.wikibooks.org/wiki/Java_Persistence/Criteria):
// Select the employees and the mailing addresses that have the same address.
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
Root employee = criteriaQuery.from(Employee.class);
Root address = criteriaQuery.from(MailingAddress.class);
criteriaQuery.multiselect(employee, address);
criteriaQuery.where( criteriaBuilder.equal(employee.get("address"), address.get("address"));
Query query = entityManager.createQuery(criteriaQuery);
List<Object[]> result = query.getResultList();

how to use ">" or "<" as variable in javafx sqlite query

I have created a combobox in javafx and I want to query the sqlite db for data which are greater or less than what is selected from the combobox.
Combobox have ObservableList "10,20,30,40,50"
My query is " Select * From table Where age ( xxx ) ?"
xxx can be (" >=" or "<=")
this is my query
String qry_age = "Select * From table Where age (>=) ?";
PreparedStatement ps_age = connect.prepareStatement(qry_age);
ps_age.setInt(1,15);
ResultSet rs_age = ps_age.executeQuery();
while (rs_age.next()) {
System.out.println(rs_age.getString("age"));
}
You could simply use string concatenation to construct the query:
String ageCompareOperator = ">="; // or something else e.g. value from ComboBox
String qry_age = "Select * From table Where age " + ageCompareOperator + " ?";
PreparedStatement ps_age = connect.prepareStatement(qry_age);
...

Smarter way of constructing query

In my node.js module, I have some data in an array which I need to construct a query:
var valueClause = "(fieldA = '" + data.fieldA + "' AND fieldB = '";
var whereClause = ' WHERE ';
var hasAdded = false;
data.accounts.forEach(function (account) {
whereClause += valueClause + account.fieldB + "') OR ";
hasAdded = true;
})
if (hasAdded) {
// remove OR
whereClause = whereClause.substring(0, whereClause.length - 3);
// use the whereClause in the query
...
}
At the end of the above codes, if I have 2 accounts, I have whereClause:
' WHERE (fieldA = 'abcde' AND fieldB = '0003') OR (fieldA = 'abcde' AND fieldB = '0002') OR
I always have to remove the last ' OR' bit.
Is there a smarter way to construct the above?
Since the value of fieldA seems to be fix in the loop you could write this query
WHERE fieldA = 'abcde' AND fieldB IN ('0002','0003')
Since you have no prepared SQL Statement, where you could use the Array directly, you have to join the values similar to yout approach.
If guaranteed at lest one value exists I concatenate in the loop always with leading comma and use substr(1) of the value

Solution to this error

I have written this query:
String sql = "Update db " +
"SET LName = '"+txtLName.getText()+"'," +
"ATC_Code = '"+txtATCcode.getText()+"'," +
"ATC_Name= '"+txtATCname.getText()+"'," +
"Course_Name = '"+txtCourseName.getText()+"'," +
"Course_Fee = '"+txtCourseFee.getText()+"'," +
"Where FName = '"+txtFName.getText()+"' ";
And I got an error like:
Malformed SQL Statement: Expected ',', found 'Anuja'`.
Statement:Update db SET LName = 'df',ATC_Code = '323',ATC_Name= 'sd',Course_Name = 'd',Course_Fee = '534',Where FName = 'Anuja'
Remove last , for Set statement:
String sql = "Update db " +
"SET LName = '"+txtLName.getText()+"'," +
"ATC_Code = '"+txtATCcode.getText()+"'," +
"ATC_Name= '"+txtATCname.getText()+"'," +
"Course_Name = '"+txtCourseName.getText()+"'," +
"Course_Fee = '"+txtCourseFee.getText() + //here does not need '
"Where FName = '"+txtFName.getText()+"' ";
On a side note, this kind of sql command generation(concatenating strings that contains some values) are suspect to SQL injection attacks, to prevent this type of attacks, use paramaters and set the parameters values instead. See SQL injection for more information.
Update db
SET LName = 'df',
ATC_Code = '323',
ATC_Name= 'sd',
Course_Name = 'd',
Course_Fee = '534',
Where FName = 'Anuja'
Only will change remove last comma ','
Update db
SET LName = 'df',
ATC_Code = '323',
ATC_Name= 'sd',
Course_Name = 'd',
Course_Fee = '534'
Where FName = 'Anuja'