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?
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:
Improve INSERT-per-second performance of SQLite
(12 answers)
Closed 8 years ago.
I am new to Database.
I am tying to insert 22000 word to a sqlite DB but it is taking forever to complete
for (int i=0;i<s.size();i++){
String sql = "INSERT INTO WORD (word) VALUES ("+"'"+s.get(i)+"'"+");";
statement.executeUpdate(sql);
}
Table name is WORD and field name is word(which is a String)
Here s is an arrylist and s.get(i) returns a string... I have made the connection and trying to update
the table.
It is very slow. What am I doing wrong?
Its slow because you are firing 22000 queries to the database. You should insert in batches. That will fix the problem. Even if a single insert takes 10ms total time is like 3.5 minutes!!
A simple insert query with batches of 2 will look like this
"INSERT INTO WORD (word) VALUES ("+"'"+s.get(i)+"'"+"), ("+"'"+s.get(i+1)+"'"+");";
This will reduce the time by half. Now you will have to programatically insert more of these values.
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.
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?