fetch value from SQL query list - java

So i just wrote down this SQL query and i am trying to capture the value of rest_id in query.list(). However, this is giving the value as [1] . I want just 1 without the braces. How do i do it? Please check the code below for reference:
String sql1 = "select rest_id from rest_details where rest_name = '" + nameclicked + "' and rest_location = '" +locclicked + "'" ;
SQLQuery query1 = session.createSQLQuery(sql1);
System.out.println("sql1 " + query1.list());

Use below code to get the element inside list:
System.out.println("sql1 " + query1.list().get(0));
This always returns only the first element from the list.

Replace
System.out.println("sql1 " + query1.list());
By :
for(String id : query1.list() ) System.out.println("sql1 " + id);

Related

No Value specified for parameter 2

I am getting this error when trying to run my Spring boot.
java.sql.SQLException: No value specified for parameter 2
My code is this:
public UserTemp findHistoryByID(Integer Patient_Number) {
String sql = "select Col1\n" +
"from (\n" +
" select Past_Diagnoses_1 as Col1\n" +
" from patienthistory\n" +
" where Patient_Number = ?\n" +
" union\n" +
" select Past_Diagnoses_2 as Col1\n" +
" from patienthistory" +
" where Patient_Number = ?" +
" ) as T;";
return jdbcTemplate.queryForObject(sql, new Object[]{Patient_Number}, (rs, rowNum) ->
new UserTemp(
rs.getString("Col1")
));
}
As in the comments, you are having 2 placeholders in the SQL query. So you have to pass patient_number 2 times.
Coming to your second question, it depends on your requirement.
If you need a single result, you need to fix it on the DB side as it's a data issue or the query used is not proper.
If more than one result is allowed, you can use jdbcTemplate.queryForList() instead of jdbcTemplate.queryForObject(). And change the return type of findHistoryByID() to List<Map<String,Object>> and all callers of this function.
Note: Here key for each Map in List is column names returned from DB.
More information on jdbcTemplate.queryForList() is in official documentation

Java - Search Oracle DB for a row with spaces (ORA-00920)

I am trying to execute a select query from Java, the tricky part is that both the condition and the column have a String with spaces :
String query = "select rule_name "+
" from rules " +
" where rule_name = " m_rule; //rule_name = str tf //m_rule = str tf
So the query output :
select rule_name
from rules
where rule_name = str tf
The error code - ORA-00920 - I belive it is because of the spaces.
Is there any way to fix this?
The error that you are facing basically means :
ORA-00920: invalid relational operator
This is the blank that is causing the issue. Your Java code should enclose the conditional parameter in quotes:
String query = "select rule_name "+
" from rules " +
" where rule_name = '" + m_rule + "'";
Hope this will help!

select array element PostgreSQL

I have a column in my database called: "CIElabOne" which is of the type numeric [] ("CIElabOne" numeric[]) and thus contains values like: {9.766934377517181,0.0011685082518947398,-0.0023119569625251746}
I cant access the values independently, when executing the following SQL query:
SELECT "fileName" FROM "clothItems" WHERE "CIElabOne[1]" = '9.766934377517181'
The result is : ERROR: column "CIElabOne[1]" does not exist
Selecting CIElabOne as a whole is not a problem but I need to evaluate each of the elements of the array. I don't know why this happens I am following the guide http://www.postgresql.org/docs/9.1/static/arrays.html but I don't seem to find the error
This is my real sql query in java:
sqlTwo = "SELECT \"fileName\" FROM \"clothItems\" WHERE \"CIElabOne[1]\" = '"
+ inputColorOneCIELAB[0]
+ "' AND \"CIElabOne[2]\" = '"
+ inputColorOneCIELAB[1]
+ "' AND \"CIElabOne[3]\" = '"
+ inputColorOneCIELAB[2]
+ "' and \"gender\" = '"
+ inputGender
+ "' AND \"shape\" <> '"
+ inputShape
+ "'";
inputColorOneCIELAB[] is an array of doubles
The array subscript operator must be outside the quoted identifier name, otherwise it's treated as part of the identifier.
"CIElabOne[1]"
means "the column named CIElabOne[1]." You want:
"CIElabOne"[1]
which means "the first array element of the column CIElabOne.

UPSERT Query gives ORA Missing Expression Exception

I am trying to update and insert into Oracle Database by using this below query. But when I was trying to insert into oracle database by using this below query, I always get the exception as-
java.sql.SQLException: ORA-00936: missing expression
This is the below UPSERT Query- Is there anything wrong with this below query?
// Update and Insert both
public static final String UPSERT_SQL = "MERGE INTO " +DATABASE_TABLE+ " USING ( SELECT ? AS ID, " + // We will maybe add this record
" ? AS CGUID, " +
" ? AS PGUID, " +
" ? AS SGUID, "+
" ? AS USERID, "+
" ? AS ULOC, "+
" ? AS SLOC, "+
" ? AS PLOC, "+
" ? AS ALOC, "+
" ? AS SITEID, "+
" FROM dual ) maybe "+
// Checking whether ID got matched, if matched then we will update the database table (ULOC, SLOC, PLOC, ALOC)
" ON (maybe.ID = "+DATABASE_TABLE+".ID) "+
" WHEN MATCHED THEN "+
// We only need update the fields that might have changed
" UPDATE SET " +DATABASE_TABLE+ ".ULOC = maybe.ULOC, " +DATABASE_TABLE+ ".SLOC = maybe.SLOC, " +DATABASE_TABLE+ ".PLOC = maybe.PLOC, " +DATABASE_TABLE+ ".ALOC = maybe.ALOC "+
// If not matched then we will Insert new records in the database.
" WHEN NOT MATCHED THEN "+
// Insert new record
" INSERT VALUES (maybe.ID, maybe.CGUID, maybe.PGUID, maybe.SGUID, maybe.USERID, maybe.ULOC, maybe.SLOC, maybe.PLOC, maybe.ALOC, maybe.SITEID)";
And I am trying to insert like this-
LnPDataConstants.PSTMT = LnPDataConstants.DB_CONNECTION.prepareStatement(LnPDataConstants.UPSERT_SQL);
LnPDataConstants.PSTMT.setInt(1, (int) ind);
LnPDataConstants.PSTMT.setString(2, LnPDataConstants.CGUID_VALUE);
LnPDataConstants.PSTMT.setString(3, LnPDataConstants.PGUID_VALUE);
LnPDataConstants.PSTMT.setString(4, LnPDataConstants.SGUID_VALUE);
LnPDataConstants.PSTMT.setString(5, LnPDataConstants.UID_VALUE);
LnPDataConstants.PSTMT.setString(6, LnPDataConstants.ULOC_VALUE);
LnPDataConstants.PSTMT.setString(7, LnPDataConstants.SLOC_VALUE);
LnPDataConstants.PSTMT.setString(8, LnPDataConstants.PLOC_VALUE);
LnPDataConstants.PSTMT.setString(9, LnPDataConstants.ALOC_VALUE);
LnPDataConstants.PSTMT.setString(10, LnPDataConstants.SITEID_VALUE);
LnPDataConstants.PSTMT.executeUpdate();
And when I tried to print the query- I got like this on the console-
MERGE INTO LNPDATA USING ( SELECT ? AS ID, ? AS CGUID, ? AS PGUID, ? AS SGUID, ? AS USERID, ? AS ULOC, ? AS SLOC, ? AS PLOC, ? AS ALOC, ? AS SITEID, FROM dual ) maybe ON (maybe.ID = LNPDATA.ID) WHEN MATCHED THEN UPDATE SET LNPDATA.ULOC = maybe.ULOC, LNPDATA.SLOC = maybe.SLOC, LNPDATA.PLOC = maybe.PLOC, LNPDATA.ALOC = maybe.ALOC WHEN NOT MATCHED THEN INSERT VALUES (maybe.ID, maybe.CGUID, maybe.PGUID, maybe.SGUID, maybe.USERID, maybe.ULOC, maybe.SLOC, maybe.PLOC, maybe.ALOC, maybe.SITEID)
" ? AS SITEID, "+
" FROM dual ) maybe "+
remove comma.
You still need to list the columns for your insert statement
WHEN NOT MATCHED THEN
INSERT (col1, col2 ... )
VALUES (val1, val2 ... )

Quick Java SQL problem

My code:
String sql = "SELECT Publisher.Name, Book.Title, ShopOrder.OrderDate, SUM(OrderLine.Quantity) AS No_Books, "
+ "SUM(OrderLine.UnitSellingPrice * Orderline.Quantity) AS Total_Price"
+ "FROM Publisher, Book, OrderLine, ShopOrder"
+ "WHERE OrderLine.BookID = Book.BookID AND ShopOrder.ShopOrderID = OrderLine.ShopOrderID AND Publisher.PublisherID = Book.PublisherID AND Publisher.PublisherID = " + id
+ "GROUP BY book.title, publisher.name, ShopOrder.OrderDate"
+ "ORDER BY ShopOrder.OrderDate, Book.Title";
Resulting error:
syntax error at or near "Publisher" at char position 166 (Just after the FROM clause)
Theres spaces missing
Your strings is
...S Total_PriceFROM Publisher, Book, OrderLine, ShopOrderWHERE O...
You should use:
String sql = "SELECT Publisher.Name, Book.Title, ShopOrder.OrderDate, SUM(OrderLine.Quantity) AS No_Books, "
+ " SUM(OrderLine.UnitSellingPrice * Orderline.Quantity) AS Total_Price"
+ " FROM Publisher, Book, OrderLine, ShopOrder"
+ " WHERE OrderLine.BookID = Book.BookID AND ShopOrder.ShopOrderID = OrderLine.ShopOrderID AND Publisher.PublisherID = Book.PublisherID AND Publisher.PublisherID = " + id
+ " GROUP BY book.title, publisher.name, ShopOrder.OrderDate"
+ " ORDER BY ShopOrder.OrderDate, Book.Title";
When you concatenate those strings there is no space between Total_Price and FROM.
And in other lines similarly. I always end and start a quoted SQL fragment with a space.
You need spaces either at the end of your lines or at the beginning. The resulting string would look like: ...AS Total_PriceFROM Publisher...

Categories

Resources