sql « JDBC « Java Database Q&A





1. What is the best-practice for nesting PreparedStatements?    stackoverflow.com

I have several instances where that a section of legacy sql statements is based on a dependency. for example.

if (x !=null)
{
  SQL = "SELECT z WHERE x > y";
}
else
{
 ...

2. Are there any illegal characters when using named parameters in JDBC?    stackoverflow.com

I'm using named parameters in a query to match fields in a map-like data structure. The data structure can have fields, or another map-like data structure. This nested structure is repeatable ...

3. Using JDBC, how can I substitute multiple IDs into "DELETE FROM T WHERE id IN (?)"    stackoverflow.com

I have some code that produces a set of primary key values that I want to delete from a database table.

long[] keysToDelete = { 0, 1, 2, 3 };
and I'd like ...

4. SQL exception when trying make an prepared statement    stackoverflow.com

I'm trying to pass the following String to a PreparedStatement:

private static final String QUICK_SEARCH = "select * from c where NAME like '% ? %'";
However, I get an SQL exception that ...

5. JDBC generation of SQL in PreparedStatement    stackoverflow.com

I had a really huge problem recently which took me a lot of time to debug. I have an update statement which updates 32 columns in table. I did that with ...

6. Why is this Java PreparedStatement throwing ArrayIndexOutOfBoundsException 0 with parameterIndex = 1?    stackoverflow.com

The following method, when called with something like String val = getCell("SELECT col FROM table WHERE LIKE(other_col,'?')", new String[]{"value"}); (this is SQLite), throws a java.lang.ArrayIndexOutOfBoundsException: 0 at org.sqlite.PrepStmt.batch(PrepStmt.java:131). Can anyone ...

7. Prepared Statement Failing (With an Error Message!)    stackoverflow.com

I am getting this error when trying to insert data into a data table

Error Saving data. [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect
I looked at the appropriate data table and there does ...

8. How to get the status of a Java PreparedStatement.executeQuery(..)    stackoverflow.com

I have a SQL query running using something like this:

PreparedStatement pstmt = dbConn.prepareStatement(sqlQuery);
ResultSet rs = null;
..
.. Set parms here ..
..
rs = pstmt.executeQuery();
It works fine when I use a local database, but ...

9. Using Prepared Statements to set Table Name    stackoverflow.com

Im trying to use prepared statements to set a table name to select data from but i keep getting an error when i execute the query. The error and sample code is ...





10. Preventing SQL injection without prepared statements (JDBC)    stackoverflow.com

I have a database log appender that inserts a variable number of log lines into the database every once in a while. I'd like to create an SQL statement in a way ...

11. Java - Prepared statements and arrays    stackoverflow.com

How can I handle an array in a prepared statement? i.e, I want to do a query and one of the parameters I get is an array of strings which I ...

12. How does a PreparedStatement avoid or prevent SQL injection?    stackoverflow.com

I know that PreparedStatements avoid/prevent SQL Injection. How does it do that? Will the final form query that is constructed using PreparedStatements will be a string or otherwise?

13. How do I sanitize SQL without using prepared statements    stackoverflow.com

For some sql statements I can't use a prepared statment, for instance: SELECT MAX(AGE) FROM ? For instance when I want to vary the table. Is there a utility that sanitizes sql in ...

14. How can I get the SQL of a PreparedStatement?    stackoverflow.com

I have a general Java method with the following method signature:

private static ResultSet runSQLResultSet(String sql, Object... queryParams)
It opens a connection, builds a PreparedStatement using the sql statement and the parameters in ...

15. Get query from java.sql.PreparedStatement    stackoverflow.com

In my code I am using java.sql.PreparedStatement. I then execute the setString() method to populate the wildcards of the prepared statement. Is there a way for me to retrieve (and print out) the ...

16. PreparedStatement.setString() method without quotes    stackoverflow.com

I'm trying to use a PreparedStatement with code similar to this:

SELECT * FROM ? WHERE name = ?
Obviously, what happens when I use setString() to set the table and name field ...





17. Variable column names using prepared statements    stackoverflow.com

I was wondering if there was anyway to specify returned column names using prepared statements. I am using MySQL and Java. When I try it:

String columnNames="d,e,f"; //Actually from the user...
String name = ...

18. Using SQL function in parameterized statement    stackoverflow.com

Considering this query:

insert (a, b, update_time) values (1, 1, now() - interval 10 second)
Now, I need to convert it to a parameterized statement:
insert (a, b, update_time) values (?, ?, ?) 
The ...

19. SQL Prepared Statment to Create Table    stackoverflow.com

I wanted to know of some way to create table on the fly based on user input(SQL Prepared Statement)

CREATE TABLE ? (
  First_Name char(50),
  Last_Name char(50)
)
What should ...

20. java.sql.SQLException: Missing IN or OUT parameter at index::4 error occurs with preparestatement    stackoverflow.com

strInsertQuery="INSERT INTO SYNPACKAGEFREEUSAGEMAPPING (PACKAGEID,SERVICEID,PRIORITY,MAPPINGID,ATTRIBUTEVALUE,FREEUSAGE,UOM,PARAMSTR1,UNITCREDITPOLICYDETAILID) VALUES (?,?,?,?,?,?,?,?,?)";
newPstmt=newCon.prepareStatement(strInsertQuery);
newPstmt.setString(1,strProductId);
              newPstmt.setString(2,strUPGServiceId);
            ...

21. Java PreparedStatement Syntax    stackoverflow.com

Is this the correct syntax for a prepared statement in java:

INSERT INTO table (id, version, data)
  VALUES (?, ?, ?)
  ON DUPLICATE KEY UPDATE 
    data ...

22. How can I find the error in my SQL prepared statement?    stackoverflow.com

try {
    pst=con.prepareStatement("insert into stud values(?,?,?,?,?,?,?,?,?)");
    pst.setString(1,s1);
    pst.setString(2,s2);
    pst.setString(3,s3);
    pst.setString(4,s4);
    pst.setString(5,s5);
   ...

23. PreparedStatement with subquery returning empty result set    stackoverflow.com

I've got a query that works fine when I run it in SQL Developer, but returns an empty result set when run as a prepared statement. I'm not sure if my ...

24. Does the preparedStatement avoid SQL injection?    stackoverflow.com

I have read and tried to inject vulnerable sql queries to my application. It is not safe enough. I am simply using the Statement Connection for database validations and other insertion ...

25. How do you write the SQL for a PreparedStatement using a WHERE x IN clause?    stackoverflow.com

I have a query that looks like this:

SELECT last_name,
       first_name,
       middle_initial
FROM   names
WHERE  last_name IN ('smith', 'jones', ...

26. Java Prepared Statement Error    stackoverflow.com

Hi Guys the following code throws me an error i have an insert statement created once and in the while loop i am dynamically setting parameter , and at the end ...

27. Java Using SQL Execute method but being able to access results    stackoverflow.com

Rather than having complex SQL queries, tries, catch and finals everywhere in the code I have a method execute(SQL, up to three inputs) however if trying to access the ResultsSet this ...

28. How to get parameters from PreparedStatement?    stackoverflow.com

I'm writing generic logger for SQLException and I'd like to get parameters that were passed into PreparedStatement, how to do it ? I was able to get the count of them.

ParameterMetaData ...

29. Do we have some placeholders in java for string as we have (?) in SQL prepared statement    stackoverflow.com

Can i have placeholder for String java like we have in sql prepared statements? Eg Conside that i have string St = "akkk ? la,ala ? " , now i want to set ...

30. Java preparedstatement using two single quotes for empty string paramenter    stackoverflow.com

I am using a preparedsatatement with sql such as:

String sql = "insert into foo (a,b,c) values (?,?,?)";
 ps = conn.prepareStatement(sql);

  ps.setString(psIndex++, a);
  ps.setString(psIndex++, b);
  ps.setString(psIndex++, c);
But if any ...

31. PreparedStatement not executing!    stackoverflow.com

So odd! :P

connection = appDatabase_.getDatabase().getConnection();
            PreparedStatement updateStmt = connection.prepareStatement
            ...

32. Java PreparedStatement equivalent for OraclePreparedStatement registerReturnParameter    stackoverflow.com

OraclePreparedStatement has a registerReturnParameter that works with a "Returning into" sql clause. What's the Java equivalent (and a SQL statement) that would do the same? I believe getGeneratedKeys() only ...

33. PreparedStatement.setString() call behaving oddly    stackoverflow.com

This might be really simple, but I do not understand what is going wrong. As background, I use PreparedStatements with template queries for efficiency. I invoke setString(index, argument) to populate portions ...

34. How do I properly get a SQL Datetime back into my Java application?    stackoverflow.com

I'm having trouble with retrieving queries from my SQL database. I can get the blasted things added to the database, but I'm having an inordinate amount of difficulties performing the reverse. ...

35. how to use prepared statement    stackoverflow.com

someone have suggested me to use prepared statement i dont know how to use it what changes i have to do in my code my code is:

 try
    ...

36. How to prevent SQL injection if I don't have option to use "PreparedStatement" in Java/J2EE    stackoverflow.com

I have one application In which I can’t user “PreparedStatement� on some of places. Most of SQL queries are like…. String sql = "delete from " + tableName; So I like to know ...

37. Converting Dynamic SQL Query to a Prepared statement in Java    stackoverflow.com

I'm wanting to write a program that converts a SQL Dynamic Query into a prepared statement in Java. So if given a string like "SELECT * FROM EMPLOYEE_TABLE WHERE FIRST_NAME='BOB';" I'd like ...

38. What's the correct way to use PreparedStatement in java?    stackoverflow.com

Specifically if I have three queries should I do

PreparedStatement singleQuery ...
and "share" the one object. Or should I do
PreparedStatement query1 ...
PreparedStatement query2 ...
PreparedStatement query3 ...

39. How to set a formula in a JDBC PreparedStatement    stackoverflow.com

I have a PreparedStatement that I use to insert a bunch of rows with a lot of column data. However, for one of the columns (a Timestamp), if the value is ...

40. invalid column index error with Prepare statment    stackoverflow.com

I am getting invalid column index for following prepared statement.
Here is my code

         // Excluding some unnecessary code
    ...

41. Will prepared statements prevent sql injection attacks?    stackoverflow.com

Consider a hypothetical case where I have to retrieve some details from the database based on the userId and the sample code is given below

private String getpassword(String username) {

PreparedStatement statement = ...

42. parameter validation in PreparedStatement    stackoverflow.com

I am working on a java app that utilizes PreparedStatement.

SELECT FIELD1, FIELD2 FROM MYTABLE WHERE (FIELD1='firstFieldValue' OR FIELD1='' or 'firstFieldValue'='');
firstFieldValue is a parameter. And it is necessary to check that its ...

43. jdbc preparedStatements for multiple value inside IN    stackoverflow.com

How to set values for sql with IN which can hold variable numbers like, ... WHERE ...status IN (4, 6,7 ); ?

PreparedStatement ps = con.prepareStatement(
       ...

44. Do I get a memory leak by not closing my JDBC PreparedStatements?    stackoverflow.com

I'm working with java.sql PreparedStatements, and I was wondering about the following: In Java is Pass-by-Value, Dammit!, the following is given as an example of Java's Pass-By conventions:

public void foo(Dog ...

45. PreparedStatement with CONTAINS query    stackoverflow.com

I have a query that will need to run 28 000 times in a row, so I thought using a preparedStatement probably is a clever idea. Here is my query :

String ...

46. SQL : how to use IN keyword with prepared statement for range replacement using Java    stackoverflow.com

String sql = "select * from file_repo_index where id in (?)";

PreparedStatement ps = conn.prepareStatement(sql);

ps.setString (1, toCommaSeparatedList(repoIdList));     

ResultSet result = ps.executeQuery();

public static String toCommaSeparatedList(Collection col);

I have to use ...

47. Slow calls to java.sql.DriverManager.getConnection on Weblogic due to extra drivers    stackoverflow.com

Weblogic server. I've profiled the code and java.sqlDriverManager.getConnection iterates through all the drivers that were registered with a call to DriverManager.registerDriver(driver). My application uses an appache connection pool library. There must ...

48. Java standard library to escape strings (without prepared statements)    stackoverflow.com

I'm searching for a Java standard library to escape strings before using them in a SQL statement. This is mainly to prevent errors and to prevent SQL injection, too. Unfortunately, I will ...

49. JDBC PreparedStatement Data Truncation Exception    stackoverflow.com

I am trying to insert the values into DB2 Database table where the table has huge number of columns However 5 columns in specific

Column1 - CHAR - 1 (fieldSize in db)
Column2 - DECIMAL ...

50. How to fill a query sql with multiple optional parameter in PreparedStatement?    stackoverflow.com

I explain myself... I have a form with fill the query (eg.):

SELECT * 
FROM table 
WHERE id=? AND name=? AND sex=? AND year=? AND class=?
but only the "id" is mandatory, all ...

51. Rewriting SQL statements to prepared statements in Java    stackoverflow.com

Due to circumstances outside our control (using code written by a 3rd party, in other words), we find ourselves needing to rewrite SQL statements with inline values into prepared statements. Right ...

52. Can't find CachedRowSet into javax.sql    coderanch.com

53. javax.sql.RowSet, CachedRowSet    coderanch.com

54. PreparedStatement in SQL    coderanch.com

Hello all, Could anyone please tell me if I use a statement like this i.e. insertMarketDayTrade = connection.prepareStatement(insertMarketDayTradeString); insertMarketDayTrade.setString(1, marketRef); insertMarketDayTrade.setInt(2,tradeVolume); insertMarketDayTrade.setInt(3,tradePrice); insertMarketDayTrade.executeUpdate(); insertMarketDayTrade.close(); more than once will the database try to compile the statement again or will it already be aware it has one?? Is there anywhere in SQL where I can view what statements have been compiled? Thanks for ...

55. Viewing SQL from PreparedStatements    coderanch.com

56. SQL built-in functions in PreparedStatement?    coderanch.com

[8/4/05 12:12:42:481 CDT] 36aa36aa SystemErr R java.sql.SQLException: [SQL0418] Use of parameter marker not valid. [8/4/05 12:12:42:496 CDT] 36aa36aa SystemErr R at java.lang.Throwable.(Throwable.java:54) [8/4/05 12:12:42:496 CDT] 36aa36aa SystemErr R at java.lang.Throwable.(Throwable.java:68) [8/4/05 12:12:42:496 CDT] 36aa36aa SystemErr R at java.sql.SQLException.(SQLException.java:51) [8/4/05 12:12:42:496 CDT] 36aa36aa SystemErr R at com.ibm.as400.access.JDError.throwSQLException(JDError.java:520) [8/4/05 12:12:42:496 CDT] 36aa36aa SystemErr R at com.ibm.as400.access.AS400JDBCStatement.commonPrepare(AS400JDBCStatement.java:1363)

57. sql of prepared statement    coderanch.com

Jiju, If you search this forum for DebuggableStatement, you will find a tool that shows you the values of the bound parameters. However, it is more likely that there are fields in the table that are not being referenced from the insert. These would default to null, which may not be allowed.

59. Obtaining The SQL from a prepared Statement    coderanch.com

I have a Prepared Statement insert into person(firstname,lastname) values(?,?); and I am setting the firstname and lastname via Preparedstmt.set(1,"ade"); Preparedstmt.set(2,"yomi"); and the last step is Preparestmt.executeUpdate(); The question i have is i am trying to obtain the SQL executed against the Database,after doing the set and executing the executeUpdate command which method can i use?

61. PreparedStatement problem; Doesn't happen with plain SQL    coderanch.com

Hi, I have spent a day or more trying to figure out the answer to this problem. I wanted to see if anyone has had a similar problem. When I execute this code, I get a result set that contains records (this is just test code to get it working, so no looping through RS): StringBuffer sb = new StringBuffer(); sb.append("SELECT ...

64. How to fetch the sql query from the prepared statement at runtime?    coderanch.com

Hi All, Below there is an snippet of code , Assume con,ps,rs are defined properly. String sqll = "select id, userName, password, firstName, lastName, phoneNumber, phoneExtension, email, email2, userLevel, companyId, creatorId, activated, monday, tuesday, wednesday, thursday, friday, saturday, sunday, month, browserInfo from userInfo where userName = (?) and activated='Y' order by userName" ; ps1 = createPreparedStatement(con, sqll); String value = "xab";// ...

66. sql rownum usage in preparedstatement    coderanch.com

67. Dynamic SQl - PreparedStatement    coderanch.com

Hi All, I have the below requirement in my project. The project uses dynamic sql in its queries in the following way. sqlQuery = sqlQuery + " AND GROUP IN ("+ userRole.toString().trim() + ") "; userRole is StringBuffer whose value would be 'admin','inq' Now the query hard codes the value, i want the value to be set at run time using ...

70. print preparedstatement sql with values    forums.oracle.com

hi I have replaced the values using preparedstatment and its setString methods. i want to print the statement with the values replaced in PS. its for our auditing purpose. using database itself is not an option, has to be done in my java application so how can i print the statement which is being executed along with the replaced values? Thanks ...

71. SQL Error using cachedrowSet.populate.    forums.oracle.com

crs2.populate(rs2); crs3.populate(rs3); is throwing an sql error. I ran the query in my query analyzer tool and it runs fine. But when I try to populate it in cachedrowset it throws sql error. I have more queries in the same java which I am passing to the jsp as cachedrowset (for example cr3 in the above code) And they all are ...

72. regarding java.sql.PreparedStatement    forums.oracle.com

73. CachedRowSet SQL variable    forums.oracle.com

******** //tbl_itdontaddupRowSet.setCommand("SELECT ALL tbl_itdontaddup.ID, \n tbl_itdontaddup.Hype, \n tbl_itdontaddup.Fact, \n tbl_itdontaddup.Dev_Title, \n tbl_itdontaddup.Dev_Verse, \n tbl_itdontaddup.Dev_Text, \n tbl_itdontaddup.Note_Text, \n tbl_itdontaddup.Note_Ref, \n tbl_itdontaddup.Headline, \n tbl_itdontaddup.Article \nFROM tbl_itdontaddup\nWHERE tbl_itdontaddup.ID = 1 "); ******** //tbl_itdontaddupRowSet.setCommand("SELECT ALL tbl_itdontaddup.ID, \n tbl_itdontaddup.Hype, \n tbl_itdontaddup.Fact, \n tbl_itdontaddup.Dev_Title, \n tbl_itdontaddup.Dev_Verse, \n tbl_itdontaddup.Dev_Text, \n tbl_itdontaddup.Note_Text, \n tbl_itdontaddup.Note_Ref, \n tbl_itdontaddup.Headline, \n tbl_itdontaddup.Article \nFROM tbl_itdontaddup ");