jdbc template getString on UUID not working [closed] - java

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 have this method.
I am using jdbcTemplate query and then I am trying to return the list, however I am getting this error:
Required type UUID provided String
public List<Person> getPeople() {
String sql = "SELECT * from people";
List<Person> people = jdbcTemplate.query(sql, (rs, idx) -> {
return new Person(
result.getString("id"); <-------------- says can't fetch string because it's uuid type in db
)
})
}
What do I need to use instead of getString in order for this to work?

Try using getObject and casting it to UUID: result.getObject("id", java.util.UUID.class)

Related

JDBC delete method seems to not be working in my Spring Boot service [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'm trying to create a simple JDBC method to delete from my DB, and I'm not sure if I'm going about this the correct way. This is inside one of my services.
Method:
public void deleteLocation(Integer id) {
String DELETE = "DELETE FROM locale WHERE id=?";
namedParameterJdbcTemplate.update(DELETE, new BeanPropertySqlParameterSource(id));
}
I would try changing your update line to
namedParameterJdbcTemplate.update(DELETE, id);.
If you are using spring-boot. Then you should be using spring-data-jpa to manage your database. Jdbc is Hard way of doing this.
If you are using jdbc delete to a specific row use prepared statement. You can refer this:
preparedStatement = connection.prepareStatement("DELETE * from table_name WHERE id= ?");
preparedStatement.setInt(1, id);
return !preparedStatement.execute();

Exploring with Java streams required to read and convert the format of strings [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 2 years ago.
Improve this question
I am trying Java streams to read a List of Strings, splitting the list to some format like shown below
For ex: this is in my input string -
[APPLE, MANGO]
I am trying to convert the above data to this format
('APPLE', 'MANGO')
Trying to convert this using java streams, with the below piece code
String.join(",", list.stream().map(item -> "'"+item+"'").collect(Collectors.toList()));
The output I am getting is like below
('APPLE, MANGO')
But I am expecting like
('APPLE', 'MANGO')
Can anyone correct my java code to get above required format?
Context to this issue is
Trying to create a SQL Query like
Select * from TEST_TABLE where ID IN ('APPLE', 'MANGO'))
where as IN CLAUSE Parameters is an ArrayList<String>();
But with my code i am getting the out put as
Select * from TEST_TABLE where ID IN ('APPLE, MANGO'))
IN clause is not properly formatted , ' is missing
Try something like:
List<String> list = List.of("APPLE", "MANGO");
String sql = "Select * from TEST_TABLE where ID IN";
String params = list.stream().map(s -> "'" + s + "'")
.collect(Collectors.joining(",", "(", ")"));
sql = sql + params;
System.out.println(sql);
I don't know if the second closing brace in your expected output was a typo or is correct. If you need a second brace just add it above.
Maybe you could do something like:
String.format("(%s)", list.stream().map(s -> String.format("'%s'", s)).collect(Collectors.joining(",")));
EDITED:
String.format("%s)", list.stream().map(s -> String.format("'%s'", s)).collect(Collectors.joining(",")));
EDITED ANOTHER TIME:
With these additional info this is what you need:
String query = "Select * from TEST_TABLE where ID IN (%s)"
query = String.format(query, list.stream().map(s -> String.format("'%s'", s)).collect(Collectors.joining(",")));
System.out.println(query);

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 is not working [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 6 years ago.
Improve this question
I wrote a SQL query in my hibernate application using where clause and and .
Here is method ,
public List<Comobility> getComobilityByIdComobilityItemsAndIdPatient(int idComobilityItems, int idPatient, Session session) {
Query query = session.createQuery("from Comobility where comobility_items_idcomobility_items= :idComobilityItems and patient_idpatient= :idpatinet");
query.setParameter("idComobilityItems", idComobilityItems);
query.setParameter("idpatinet", idPatient);
List<Comobility> list = query.list();
return list;
}
But this is not working . There is no any exception or error . Actually, there is no any result.
Have any ideas ?
You should try like that
Wrap your method with try catch block. I believe the query has an error then it will enter to catch block
public List<Comobility>getComobilityByIdComobilityItemsAndIdPatient(int idComobilityItems, int idPatient, Session session) {
try {
logger.debug("xxxx")
//do comething
} catch (Exception e) {
e.printStackTrace();
}
}
Make sure your method parameters are not null
Check your list is empty or not.
For checking you can use IDE debugger or put some logs in your method.
You need to check the generated SQL in the log and execute it against database. If query is compiled and no records are returned, means there are no records for the particular criteria.
To generate the SQL by Hibernate, please set property show_sql to true in hibernate configuration.

Sql to java JDBC database [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 6 years ago.
Improve this question
I have a problem with searching a particular contact using part of a name. I know how it would look like in SQL but I cant implement it using Java.
if (rs.getString(nameTable LIKE '%name1%';)
Consider adding the LIKE clause to your SQL query instead of handling it in java code:
try(PreparedStatment ps = con.prepareStatement("SELECT * " +
" FROM Contact WHERE contactName like ?")) {
ps.setString(1, "%name1%");
try(ResultSet rs = ps.executeQuery()) {
while(rs.next()) {
//process your data
}
}
} catch(Exception e) {
//deal with it
}

Categories