Column not found in Left Join [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
"SELECT `articles`.`name`, `articles`.`description`,`articles`.`startPrice`,`articles`.`creationDate`,"
+ "`bids`.`date`, `bids`.`price`,`bids`.`userId`"
+ " FROM `articles` LEFT JOIN `bids` ON `articles`.`id` = `bids`.`articleId`"
+ " WHERE 1"
java.sql.SQLException: Column '`articles`.`id`' not found.
bids table has not records, but just wanted to try that, since I understand that the Left Join shows all the records in the table on the left although the right table has no records.
I have changed Left Join by Inner Join and the query runs without errors
with result of zero records (as expected).
MySql version is 5.7.18
articles table:
Some Test:
Example of a query that work Ok without errors:
"select `articles`.`id`, `articles`.`name`, `articles`.`description`,`articles`.`startPrice`,`articles`.`creationDate`," +
"`bids`.`date`, `bids`.`price`,`bids`.`userId`"
+ " FROM `articles` INNER JOIN `bids` ON `articles`.`id` = `bids`.`articleId` "
+ " WHERE 1"
I also try to restart my MySql server and the same thing happens
I am using java and JDBC with
statement.executeQuery()

I do not know why but it was fixed using the Java Statement class instead of PreparedStatement to execute query.

Try this:
"SELECT `articles`.`name`, `articles`.`description`,`articles`.`startPrice`,`articles`.`creationDate`,"
+ "`bids`.`date`, `bids`.`price`,`bids`.`userId`"
+ " FROM `articles` LEFT JOIN `bids` ON `bids`.`articleId` = `articles`.`id`"
+ " WHERE 1"

Related

Placing multiple variables in a String ready for SQL Prepared Statement - Cleanest way [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 months ago.
Improve this question
I have to build an SQL string for a prepared statement, it needs multiple variables inserting. What is the cleanest way to do this?
Raw SQL for MYSQL database, variables in bold
INSERT INTO output(their_sku, their_description,
net_cost) SELECT product_code, product_description, net_cost FROM import
The names come from Enums that could possibly need modifying, hence no hard coding table names or columns.
All my attempts look messy, and coming from Python I am missing the fStrings. Attempts below.
String sql = String.format("INSERT INTO %s(%s, %s, %s) SELECT product_code, product_description, net_cost FROM %s", Tables.OUTPUT, OutputTableFields.THEIR_SKU, OutputTableFields.THEIR_DESCRIPTION, OutputTableFields.NET_COST, Tables.IMPORT);
Quite unreadable
String sql = "INSERT INTO " + Tables.OUTPUT + "(" + OutputTableFields.THEIR_SKU + ", " + OutputTableFields.THEIR_DESCRIPTION + ", " + OutputTableFields.NET_COST + ") SELECT product_code, product_description, net_cost FROM " + Tables.IMPORT"
At least the SQL and Enums are in order but it looks awful
Is there a best practice way of setting this out?
Thankyou.
I'm not sure this helps too much in your case, but you might also consider using text blocks available since JDK15. A text block makes it neater to specify a long string which can be broken up into many lines.
You can combine with s.formatted(args...) which is equivalent of String.format(s, args...) as follows:
String sql = """
INSERT INTO %s(%s, %s, %s)
SELECT product_code, product_description, net_cost FROM %s
"""
.formatted(Tables.OUTPUT, OutputTableFields.THEIR_SKU, OutputTableFields.THEIR_DESCRIPTION, OutputTableFields.NET_COST, Tables.IMPORT);
=> sql is:
"INSERT INTO blah(blah, blah, blah)
SELECT product_code, product_description, net_cost FROM blah
"
Note that the above introduces line break, if not wanting these you can escape by adding a trailing \ like this:
String sql = """
INSERT INTO %s(%s, %s, %s) \
SELECT product_code, product_description, net_cost FROM %s\
"""
.formatted(Tables.OUTPUT, OutputTableFields.THEIR_SKU, OutputTableFields.THEIR_DESCRIPTION, OutputTableFields.NET_COST, Tables.IMPORT);
=> sql is:
"INSERT INTO blah(blah, blah, blah) SELECT product_code, product_description, net_cost FROM blah"
You could also use static imports to shorten the formatted(...) part or use resource bundle files for the text.

JDBC - java.sql.SQLException: ORA-00933: SQL command not properly ended [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to execute the below query in Oracle DB through JDBC but its throwing an exception. The exception is:
java.sql.SQLException: ORA-00933: SQL command not properly ended
Please suggest what needs to be changed ?
String questionQuery = "SELECT PCN_SURVEY_DEFINITION.ID, PCN_SURVEY_DEFINITION.NAME, PCN_QUESTIONS.ID, PCN_QUESTIONS.SURVEY_ID, PCN_QUESTIONS.LABEL, "
+ "PCN_QUESTIONS.TYPE, PCN_QUESTIONS.REQUIRED, PCN_QUESTIONS.COMMENTS, PCN_QUESTIONS.DISPLAY_ORDER "
+ "FROM PCN_SURVEY_DEFINITION, PCN_QUESTIONS "
+ "WHERE PCN_SURVEY_DEFINITION.ID = PCN_QUESTIONS.SURVEY_ID "
+ "AND PCN_SURVEY_DEFINITION.NAME=? "
+ "ORDER BY PCN_QUESTIONS.DISPLAY_ORDER ASC";
Correct the condition in WHERE clause and check the quotes(") properly where to start and where to end.
"WHERE PCN_SURVEY_DEFINITION.ID = " + PCN_QUESTIONS.SURVEY_ID + " AND PCN_SURVEY_DEFINITION.NAME=? " + "ORDER BY PCN_QUESTIONS.DISPLAY_ORDER ASC";
What do you do with the question mark? Maybe you meant to put a column name here of the other table? The one before the ORDER BY? Or are you using a prepared statement afterwards? Try removing this condition temporarily for testing purposes: "AND PCN_SURVEY_DEFINITION.NAME=? "
"WHERE PCN_SURVEY_DEFINITION.ID = PCN_QUESTIONS.SURVEY_ID " + "AND PCN_SURVEY_DEFINITION.NAME=? " + "ORDER BY
Oracle does not suport question mark "?". For variable binding oracle uses ":name" or ":1"
https://docs.oracle.com/cd/B10501_01/appdev.920/a96584/oci05bnd.htm
I had something similar and had to add the following property in my application.properties file (since I am using Spring Boot), this resolved the issue for me without having to change any of my SQL
spring.jpa.database=oracle

swing apps with jdbc connectivity and mysql [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
stmt.executeUpdate("update fees set term_1 = "+hm.get("term_1").toString()+" term_2 ="+hm.get("term_1").toString()+"total = "+hm.get("total").toString()+"id = "+std_id);
Why it is not working when it is connected to JDBC?
Your update statement is invalid, you are missing comma(,)
Correct SQL Update statement should be
update fees set term_1 = 'something', term_2='something', total='something' where id = something;
So your final Java statement will be like:
stmt.executeUpdate( " Update fees set term_1 = '"+hm.get("term_1").toString()+"',"
+ " term_2 ='"+hm.get("term_1").toString()+"',"
+ " total = "+hm.get("total").toString()+"'"
+ " where id ="+std_id);
Note : Assuming all columns apart from id are of String type (i.e. term_1,term_2,total)

Hibernate HQL setMaxResults does not work?

I'm building a small program in Java Hibernate handling a subpart of the DBLP database (parsed from XML into a SQL db).
I've queries manipulating big chuncks of data so I want to limit the output result to 10 so it goes faster.
Query query = this.session.createQuery("select A.author_id "
+ "from publication as P "
+ "join P.authors as A "
+ "where P.year <= 2010 and P.year >= 2008 "
+ "group by A.author_id "
+ "having count(distinct P.year) = 3");
query.setMaxResults(10);
this.authors = query.iterate();
That piece of code is supposed to retrieve all the authors who published at least once every year between 2008 and 2010 included.
My problem is that the line "query.setMaxResults(10);" simply does not have effect, the SQL command generated by Hibernate is
select author2_.Author_id as col_0_0_ from publication publicatio0_ inner join author_publication authors1_ on publicatio0_.Publication_ID=authors1_.publication_id inner join author author2_ on authors1_.author_id=author2_.Author_id where publicatio0_.Year<=2010 and publicatio0_.Year>=2008 group by author2_.Author_id having count(distinct publicatio0_.Year)=3
limit ?
Remarque the "limit ?" at the end.
So my question is simple, how do I use properly setMaxResults to get a correct SQL Limit ?
EDIT: all the limit stuff works fine, I misunderstood the use of limit in SQL, what I'm looking for is a way to stop the execution of the query after a given number of rows corresponding to the conditions is found, so that it does not take days to get thousands useless rows but simply returns the 10 first found rows for example.
Thanks in advance !

Specific Query runs fine directly or when any change to it is made but in current state takes longer to run

Major Update after a couple days of debugging:
I run a few queries similar to :
SELECT RTRIM(part) as part
FROM tableP pm
LEFT join tableS s on pm.id = s.id
INNER JOIN tableC cm ON cm.id = pm.id
WHERE name = 'NGW' AND status NOT IN ('NL', 'Z')
GROUP BY RTRIM(part), isnull(s.value,0)
ORDER BY isnull(s.value,0)
It is run in Java like so:
PreparedStatement select = con.prepareStatement(
"SELECT RTRIM(part) as part" +
"FROM tableP pm " +
"LEFT JOIN tableS s ON pm.id= s.id " +
"INNER JOIN tableC cm ON cm.id= pm.id " +
"WHERE name =? AND status NOT IN ('NL', 'Z') " +
"GROUP BY RTRIM(part), isnull(s.value,0) " +
"ORDER BY isnull(s.value,0) " );
select.setString(1, name);
ResultSet rs = select.executeQuery();
while(rs.next()){
... Data is Loaded...
The queries have been running fine inside of a Java application. Suddenly 3 or 4 queries of this form went from taking less then a second to over a minute.
I have copied the exact query from SQL Profiler and when run directly on the database it preforms in less then a second. I started to make changes to the query and found any change to the query would return it to 1 second performance even adding a single space between a statement. But as soon as I returned it to its original exact state it would take 60+ seconds.
Core Question:
So, I have a fix, but what could cause a query to run differently even with just a change as small as whitespace?
Is it possible that the execution plan is corrupted? Can you try explicitly clearing the plan cache?
http://msdn.microsoft.com/en-us/library/aa175244(v=sql.80).aspx

Categories