Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MySQL subquery in JDBC?

Example query:

SELECT country 
FROM data 
WHERE city LIKE 
(SELECT LEFT ('jakartada',7));

Example in JDBC:

String sql = " SELECT country FROM data WHERE city LIKE (SELECT LEFT ('?',7)) ";
PreparedStatement ps = koneksi.prepareStatement(sql);
ps.setString(1,     city    );
ResultSet rs = ps.executeQuery();

Why this doesn't work properly?

like image 639
reza Avatar asked Jan 15 '13 01:01

reza


People also ask

How does subquery work in MySQL?

Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements along with expression operator. It could be equality operator or comparison operator such as =, >, =, <= and Like operator. A subquery is a query within another query. The outer query is called as main query and inner query is called as subquery.

How do you write a sub query in Java?

Subqueries must be surrounded by parentheses. Subqueries may contain EXISTS, ALL, and ANY expressions. EXISTS expressions: The [NOT] EXISTS expression is used with a subquery and is true only if the result of the subquery consists of one or more values and is false otherwise.

Can subquery join tables?

A subquery is a query that is nested inside a SELECT , INSERT , UPDATE , or DELETE statement, or inside another subquery. Joins and subqueries are both used to combine data from different tables into a single result.


2 Answers

There is no parameter within the prepared statement, however the code attempts to set a parameter. Try adding a parameter to the statement.

String sql = " SELECT country FROM data WHERE city LIKE (SELECT LEFT (?,7)) ";
PreparedStatement ps = koneksi.prepareStatement(sql);
ps.setString(1,     city    );
ResultSet rs = ps.executeQuery();

Or try removing the statement setting the parameter:

String sql = " SELECT country FROM data WHERE city LIKE (SELECT LEFT ('jakartada',7)) ";
PreparedStatement ps = koneksi.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
like image 100
Kevin Bowersox Avatar answered Sep 30 '22 18:09

Kevin Bowersox


I believe you're making this harder than it needs to be, and at the same time you're missing something. Is this what you're trying to do?

 SELECT country FROM data WHERE city LIKE 'jakarta%'

That is, are you looking for the country column from every row where the city name starts with 'jakarta'? If so, don't forget the % sign. If you don't include the % sign, then

 SELECT country FROM data WHERE city LIKE 'jakarta'

and

 SELECT country FROM data WHERE city = 'jakarta'

mean exactly the same thing as each other, and the LIKE operator is pointless; you may as well use the = operator.

So, it seems to me the MySQL query you want is

 SELECT country FROM data WHERE city LIKE CONCAT(LEFT('jakartada',7),'%')

to add the % sign. You don't need the subselect in this case.

Like you pointed out, the Java code you need then is:

 String sql = "SELECT country FROM data " . 
              "WHERE city LIKE CONCAT(LEFT(?,7),'%')";

 PreparedStatement ps = koneksi.prepareStatement(sql);
 ps.setString(1,     city    );
 ResultSet rs = ps.executeQuery();
 ... process the rs records ...
 rs.close();  /* please don't forget to close your result sets */
like image 22
O. Jones Avatar answered Sep 30 '22 20:09

O. Jones