Hashtable<String, String> ht = new Hashtable<String, String>();
ht.put(Context.INITIAL_CONTEXT_FACTORY, "");
ht.put(Context.PROVIDER_URL, url);
ht.put(Context.SECURITY_PRINCIPAL, "");
ht.put(Context.SECURITY_CREDENTIALS, "");
Context initialCtx = (Context)new InitialContext(ht);
javax.sql.DataSource ds = (javax.sql.DataSource)initialCtx.lookup("");
Connection conn = ds.getConnection();
String sql = "";
PreparedStatement ps = conn.prepareStatement(sql);
Statement stmt = conn.createStatement();
ResultSet rs1 = stmt.executeQuery(sql);我通过配置上面数据源获取到Connection,但是用conn.prepareStatement(sql)时返回的PreparedStatement是null,而用Statement又完全没问题,难道用数据源得到的Connection不能使用PreparedStatement吗?

解决方案 »

  1.   

    PreparedStatement是null 说明没查出东西
    1.确定你库里有对应记录
    2.打印你的sql语句,然后再库中查询 看能否查到结果
      

  2.   


    数据是肯定有的哈,我不是说了我用Statement查同样的sql就能查出来吗
      

  3.   

    把你用PrepareStatement的代码贴上来啊
    用statement没问题 明显不是数据源的问题  肯定是查询出错了
      

  4.   


    PreparedStatement ps = conn.prepareStatement(sql);
    Statement stmt = conn.createStatement();
    ResultSet rs1 = stmt.executeQuery(sql);我贴出来了的啊,就这里conn.prepareStatement(sql)返回的PreparedStatement 是null。
      

  5.   

    ~~~
    sql语句是怎么写的呢?有没有?哦
      

  6.   

    sql语句是肯定没问题的,我是查整张表,都用Statement查出来了啊