Parameter « JDBC « Java Database Q&A

1. In JDBC, why do parameter indexes for prepared statements begin at 1 instead of 0?

Everywhere else in Java, anything with an index starts at 0. Is there a reason for the change here or is this just bad design?

2. Prepared statement. Can I miss out parameter?

I use queries like


3. Clever way to add the same parameter to mulitple locations in a prepared-statement

I have a JDBC query like

select * from table1 where col1 between x and y
union all
select * from table2 where col1 between x and y
union all
select * from table3 where col1 ...

5. Using PreparedStatement when the number of parameters will only be known at runtime

How do you use PreparedStatement for queries in which you do not how many parameters you will need until runtime. Say, you have 3 optional parameters, you dont know which ones or how many the user will specify until runtime, but if they are set they have to be included. This is how I am doing it now. _____________________________________________ /* These ...

6. How to pass parameter to CachedRowSet

7. passing parameters using Prepared Statement

public List getKw(String kw) throws SQLException { if(kw == null || kw.length() == 0) throw new SQLException(ResourceManager.getString("")); Connection connection = getConnection(); PreparedStatement pstmt = connection.prepareStatement("select * from xyz where ItemName like('%?%');"); pstmt.setString(1,kw); ResultSet rs =pstmt.executeQuery(); List list = new ArrayList(); InfyCrDao ic = null; if( { ic = new InfyCrDao(rs.getInt("Sno")); ic.setSNo(rs.getInt("Sno")); ic.setItemName(rs.getString("ItemName")); ic.setItemDescription(rs.getString("ItemDescription")); ic.setDateOfInitiation(rs.getDate("DateOfInitiation")); ic.setCurrentStatus(rs.getString("CurrentStatus")); ic.setPendingWith(rs.getString("PendingWith")); ic.setRemarks(rs.getString("Remarks")); list.add(ic); } rs.close(); pstmt.close(); ...

8. Set parameter not working in the PreparedStatement , why?

Not sure why my SQL is not working properly after I set parameters in the SQL instead of put variable . The following is my new code. String HOLDINGS_QUERY = "select holding_name, holding_value, as_of_date " + "from fund_grp_holding " + "where holding_type = ? " + " and fund_grp = ? order by ? " ; stmt = conn.prepareStatement(HOLDINGS_QUERY); stmt.setInt(1, type); ...

9. PreparedStatement, addBatch() and setting parameters

Hi, I am trying PreparedStatement's addBatch(String sql) method. I am trying to execute two(2) Insert statement. But I am a little confuse how would I set the values for parameters in two insert statement. Trying to google it but I did not see any example that uses more than one insert statement in one preparedstatement. Here is what I am trying ...

10. Prepared Statement invalid parameter index