Getting "SQL command not properly ended" error - java

Why i am getting below error while running this query in eclipse?
java.sql.SQLException: ORA-00933: SQL command not properly ended
Code:
String policy = "select p.policy_id,i.insurance_type,c.reason,i.insured_amount,i.max_claim_amount,c.claim_status from claim as c join policy as p on c.policy_id=p.policy_id join insurance as i on p.insurance_id=i.insurance_id where c.user_id=?";
PreparedStatement policyst = con.prepareStatement(policy);
policyst.setString(1, userId);
ResultSet policyrs = policyst.executeQuery();

Oracle does not support as for table aliasing; you should remove them:
SELECT p.policy_id,
i.insurance_type,
c.reason,
i.insured_amount,
i.max_claim_amount,
c.claim_status
FROM claim c
JOIN policy p ON c.policy_id = p.policy_id
JOIN insurance i ON p.insurance_id = i.insurance_id
WHERE c.user_id = ?

Remove word "AS" from your statement

Related

How can I use join in SQL to delete record?

I hava a problem with my program I'm trying to delete a record from a table using a join with Java this is my code:
try{
String sql ="DELETE f FROM facture f INNER JOIN client c ON f.idClient=c.id WHERE c.nom= ? ORDER BY idFact DESC LIMIT 1";
PreparedStatement pr = conn.prepareStatement(sql);
pr.setString(1,nom);
pr.executeUpdate();
System.out.println("supprimer");
}catch (SQLException e){
e.printStackTrace();
}
and this is the error :
java.sql.SQLSyntaxErrorException: 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 'ORDER BY idFact DESC LIMIT 1' at line 1.
In MySQL/MariaDB you have a choice:
You can use ORDER BY and LIMIT and the FROM can only refer to one table.
You can have a FROM that refers to multiple tables.
The solution? Rephrase the query:
DELETE FROM facture f
WHERE EXISTS (SELECT 1
FROM client c
WHERE f.idClient = c.id AND c.nom = ?
)
ORDER BY f.idFact DESC
LIMIT 1;
Or you can use a subquery to get the row to delete:
DELETE f
FROM facture f JOIN
(SELECT f.idFact
FROM facture f JOIN
client c
ON f.idClient = c.id AND c.nom = ?
ORDER BY f.idFact DESC
LIMIT 1
) ff
ON ff.idFact = f.idFact

No output while executing SQL query in Java

When I run the below code in Oracle SQL, it shows output. But when I execute it through PreparedStatement in Java, there's no output in ResultSet.
select p.table_name, 'is parent of ' rel, c.table_name
from all_constraints p
join all_constraints c on c.r_constraint_name = p.constraint_name
and c.r_owner = p.owner
where p.table_name = '<table-name>'
union all
select c.table_name, 'is child of ' rel, p.table_name
from all_constraints p
join all_constraints c on c.r_constraint_name = p.constraint_name
and c.r_owner = p.owner
where c.table_name = '<table-name>' ;
Sorry, Actually all_constraints table can be accessed only with SYSDBA privileges. So, I gave SYSDBA privilege and executed. It worked fine.
Thank You for all your responses. Cheers..

could not read column value from result set in Hibernate Native Query

SELECT
CONCAT(CREG.FIRSTNAME, ' ', CREG.LASTNAME) AS NAME,
CASE WHEN CR.CAMPAIGNTYPE = 'NPS'
THEN NPSSCORE
ELSE CSATSCORE END AS SCORE,
IFNULL(cast(CC.TEXT AS CHAR(255)), '') AS COMMENTS,
CREG.ID AS CLIENTID,
CR.ID AS CAMPAIGNRESPONSEID,
CI.ID AS ISSUEID
FROM CUSTOMER_ISSUES CI INNER JOIN (SELECT DISTINCT ISSUEID
FROM ISSUE_DEPARTMENT_MAPPING
WHERE CUSTOMERUSERID = 91 AND ISSUE_STATUS = 'New') IDM
ON CAST(CI.FEEDBACK_DATE AS DATE) BETWEEN '2016-06-05' AND '2016-06-11' AND IDM.ISSUEID = CI.ID
INNER JOIN CAMPAIGN_RESPONSE CR ON CR.ID = CI.CAMPAIGN_RESPONSE_ID
INNER JOIN CLIENT_REGISTRATION CREG ON CREG.ID = CR.RESPONSECUSTOMERID
LEFT OUTER JOIN CAMPAIGN_COMMENTS CC ON CC.CAMPAIGN_RESPONSE_ID = CR.ID;
The above query is running in the mysql-console properly ,but when I am integrating with the Hibernate,following error is thrown by Hibernate.
[BigIntegerType] could not read column value from result set: ID; Column 'ID' not found.
Try getting rid of the alias names in your SQL query.
Basically what happens here is when you run
SELECT
CONCAT(CREG.FIRSTNAME, ' ', CREG.LASTNAME) AS NAME,
CASE WHEN CR.CAMPAIGNTYPE = 'NPS'
THEN NPSSCORE
ELSE CSATSCORE END AS SCORE,
IFNULL(cast(CC.TEXT AS CHAR(255)), '') AS COMMENTS,
CREG.ID AS CLIENTID,
CR.ID AS CAMPAIGNRESPONSEID,
CI.ID AS ISSUEID
in JDBC it returns the column as CREG.ID instead of ClientID.
So try running the query without the aliases, typically, there is a problem in JDBC with this. If you still insist on using aliases,add the following entry to JDBC URL in configuration file
[useOldAliasMetadataBehavior=true]

Hibernate SQLQuery neglecting aliasname

When i try to execute the following query in mysql , it works perfectly. But when we try it via Hibernate(3.2) ,hibernate is not differentiating m.NAME and o.NAME. It is returning the same result for both as m.NAME.
SELECT m.NAME, m.SCREENNAME, rm.ADDRESS, o.NAME FROM remoteunit rm LEFT JOIN mo m ON rm.MOID = m.ID JOIN overallcustomfields ocf ON m.ID = ocf.MOID LEFT JOIN organization o ON ocf.ORGID = o.ID WHERE m.DOMAINID = 2
Iam using the following code
Transaction transaction = session.beginTransaction();
SQLQuery query = session.createSQLQuery(queryString);
query.setLong("customId", customId);
remoteUnitList = (ArrayList<Object[]>)query.list();
transaction.commit();
Note: Forced to use SQLQuery in Hibernate since the columns are dynamically populated and more constraints.
Did you try SELECT m.NAME as m_name, o.NAME as o_name.... I faced the same problem and it went of with this change.

Syntax Error for HQL JOIN Statment

I'm trying to create a HQL query to join a result of a select with another table. What I have currently is
SELECT e FROM Experience e JOIN (SELECT f FROM Follow f WHERE f.username = :username AND f.contentType = :contentType) AS A ON (e.experienceId = A.contentId);
Its SQL equivalent that currently works is
SELECT t_experiences.* FROM t_experiences JOIN (SELECT * FROM t_follows WHERE t_follows.username = :username AND t_follows.content_type = :contentType) AS A ON (t_experiences.experience_id = A.content_id);
Where t_experiences.experience_id is equivalent to Experience.experienceId, etc. The current error in the HQL is on the first ( with unexpected token: ( error.
Any help would be greatly appreciated!
Why don.t you try this:
SELECT e.experienceId FROM Experience e, Follow f WHERE f.username = :username AND f.contentType = :contentType AND (e.experienceId = f.contentId);
I think this should work for you.
Note: Replace e.experienceId by parameters which you want.

Categories