Sunday, July 20, 2008

[JDBC] issue with select IN (?) query

Hi

I am facing an issue with following select query

        pm = con.prepareStatement("SELECT EMP_NAME FROM EMP where EMP_ID IN (?) ");
        pm.setString(1, "2,5,7");        //created many employees and id with 2, 5 and 7
        rs = pm.executeQuery(); // query is not returning any values

Is it a bug?

i was using postgresql-8.3-603.jdbc3.jar

==================================================
the code
===================================================

     private static void test()
    {    
      Connection con = null;
      PreparedStatement pm = null;
      ResultSet rs =  null;
      try
      {
        String driver = "org.postgresql.Driver";
        String dburl="jdbc:postgresql://localhost/test";
        Class.forName(driver);
        con = DriverManager.getConnection(dburl, "postgres", "postgres");         
      
        pm = con.prepareStatement("SELECT EMP_NAME FROM EMP where EMP_ID IN (?) ");
        pm.setString(1, "2,5,7");        //created many employees and id with 2, 5 and 7
        rs = pm.executeQuery(); // query is not returning any values
        if (rs != null && rs.next()) //not returning any values
        {
               System.out.println(rs.getString(1));
        }
        else
          System.out.println("Nothing...");
        }
      catch (Exception ex)
      {
         ex.printStackTrace();
      }
      finally
      {
            try {
                rs.close();
                pm.close();
                con.close();
            } catch (Exception ex) {
                 ex.printStackTrace();
            }
        }
    }


======================================================

No comments: