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.
Related
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:
Call stored procedure with table-valued parameter from java
(5 answers)
How to pass Table-Valued Parameters (Array-like Parameter) to Stored Procedure in Microsoft SQL Server 2008 R2 using JDBC? [duplicate]
(3 answers)
Closed 3 years ago.
I want to call sql server stored procedure by passing array_list.
String[][] array1=new String[2][2];
array1[0][0]="a";
array1[0][1]="b";
array1[1][0]="c";
array1[1][1]="d";
String name="TEST";
String SPsql="EXEC SP_name ?,?,?";
PreparedStatement stmt=con.preparedStatement(SPsql);
stmt.setString(1, name);
stmt.setArray(2,array1);
The last line is giving error. not able set array there.
I tried with list also but no use.
You should try to pass comma separated string instead of array and then use it in query.
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 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?