This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
java PreparedStatement
Can I make the prepared statement
SELECT * FROM STUDENTS WHERE STUDENT_ID IN ?
into
SELECT * FROM STUDENTS WHERE STUDENT_ID IN (1,2,3)
Given that the collection of student ids is a string array.
I think your best solution is going to be generate the in clause dynamically: IN (?,?,?,?) , and then calling preparedStatement.setInt(i + 1, myValues[i]) for each value in your array/collection. The bad news is you will end up with a different preparedStatement for each time you have a different number of values.
See duplicate question: PreparedStatement IN clause alternatives?
Related
This question already has answers here:
Select random row from a sqlite table
(7 answers)
Closed 3 years ago.
I want to randomize some values using ID in sqlite query.
* am using quiz app and want to randomize our question. In database there is a column for category where category id is used to select proper cat and I want randomize only question with limit *
String query = "select * from quiz where id =" +id ;
SELECT *
Says to select all non-hidden columns from the table. The correct syntax for retrieving specific columns is to code the specific column names (or if there are potential ambiguities tablename.columnname e.g. quiz.question).
So you would want
SELECT question FROM quiz .....
To select a random question, assuming that question is the required column name, then you could use :-
String query = "SELECT question FROM quiz ORDER BY random();";
If you wanted to limit the number of rows returned you could use
String query = "SELECT question FROM quiz ORDER BY random() LIMIT 10;";
This question already has answers here:
PreparedStatement IN clause alternatives?
(33 answers)
Closed 3 years ago.
i have this query in spring:
private String SQL_Clear_Deleted_Options = "DELETE FROM vote_votes WHERE poll_id=? AND option_id NOT IN ?"
my problem is with second ?. the correct form would be (id1,id2,id3,...). how can i pass a string like cl="0,1,2,3,6" to this query?
i'm using jdbcTemplate. so it would be
jdbcTemplate.update(SQL_Clear_Deleted_Options, id,cl)
what should be cl?
You can use setArray of PreparedStatement to set multiple values.
https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setArray(int,%20java.sql.Array)
And the array should be of the correct type, so you could create it with the Connection.createArrayOf function:
https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#createArrayOf(java.lang.String,%20java.lang.Object[])
You can see a lot of discussions about that topic on SO already, e.g.:
How to use an arraylist as a prepared statement parameter
Edit: Forgot to mention: The JdbcTemplate should set the Parameter correctly when the Array is given as parameter.
This question already has answers here:
Hibernate Criterion IN Clause 1000 break up
(3 answers)
Java Oracle exception - "maximum number of expressions in a list is 1000"
(7 answers)
Closed 5 years ago.
I am constructing a hibernate query and I pass a list of String values into the IN clause. That list sometimes happens to be more than 1000 values so I receive an error. I have looked up some solutions like breaking that clause into smaller ones or making temporary table, but none of them showed how actually it is better to work with variable list.
So if I had something like:
SELECT * FROM MY_TABLE WHERE NAME IN (list).
What would be the best way to handle this?
This question already has answers here:
JDBC prepareStatement doesn't work
(4 answers)
Closed 7 years ago.
So I'm trying to create an unspecified table name with an unspecified columns, however, I'm new to prepareStatement and I'm not exactly sure what to do.
This is how I'm thinking about it
I know I'll need a loop for multiple entries of "line" which is the table name
but how can I deal with the columns? I think I'm specifying the number of columns in here (4). How can I do this without specifying? and what should I put for setString if the value will differ based on the table name?
I'm kind of confused and I was hoping that someone will explain this to me
loop start ....
line = kb.next();
sql = "Create TABLE " + line + "(?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,?);
pstmt.executeUpdate();
loop end
It's unlikely that you'll be able to get this to work on any database.
For Oracle-specific reasoning, see Why cannot I use bind variables in DDL?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cannot use a Like query in a JDBC prepared statement?
Is it possible to prepare a SQL statement like this:
SELECT COUNT(?) FROM TABLE WHERE COLUMUN_1 LIKE '%?%'
and in Java,
preparedStmt.setString(1, "COLUMN_2");
preparedStmt.setString(2, "FORD");
Nope. As a general rule, you can only use placeholders for values, not identifiers like table and column names.