最近碰到的问题,用preparestatement查询一个表时出了问题,就在这一个表中出了问题,下面时我调试的时候分析结果,还是找不到问题在哪里,请大家帮忙。
用oracle驱动,查询条件为department="hr"
public class test {
    public final static String SENIORITY="select * from hr_admin_employeeinf where department=?";
    /** Creates a new instance of test */
    public test() {
    }
    public static void main(String args [])
    {
        try
        {
//        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");       
//        Connection con = DriverManager.getConnection("jdbc:odbc:mydsn","scott","tiger");
        
        Class.forName("oracle.jdbc.driver.OracleDriver");       
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@hstslc011:1521:eltp","scott","tiger");
        
        PreparedStatement ps = con.prepareStatement(SENIORITY);
        ps.setString(1,"hr");
        ResultSet rs = ps.executeQuery();
        System.out.println("-------------------vv------------------"+rs);
        if(rs.next())
        {
            System.out.println("-------------------vv------------------");
            System.out.println("-----------------------------"+rs.getInt("EMPLOYEEID"));
        }        
        }
        catch(Exception e)
        {
            System.out.println("______________________________"+e);
        }
        
    }
}
运行结果:-------------------vv------------------oracle.jdbc.driver.OracleResultSetImpl@f4a24a
说明rs.next()为false;
将驱动换一下,用sun的驱动
public class test {
    public final static String SENIORITY="select * from hr_admin_employeeinf where department=?";
    /** Creates a new instance of test */
    public test() {
    }
    public static void main(String args [])
    {
        try
        {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");       
        Connection con = DriverManager.getConnection("jdbc:odbc:mydsn","scott","tiger");
        
//        Class.forName("oracle.jdbc.driver.OracleDriver");       
//        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@hstslc011:1521:eltp","scott","tiger");
        
        PreparedStatement ps = con.prepareStatement(SENIORITY);
        ps.setString(1,"hr");
        ResultSet rs = ps.executeQuery();
        System.out.println("-------------------vv------------------"+rs);
        if(rs.next())
        {
            System.out.println("-------------------vv------------------");
            System.out.println("-----------------------------"+rs.getInt("EMPLOYEEID"));
        }        
        }
        catch(Exception e)
        {
            System.out.println("______________________________"+e);
        }
        
    }
}
结果如下:-------------------vv------------------sun.jdbc.odbc.JdbcOdbcResultSet@2e7263
-------------------vv------------------
-----------------------------7000910
能正确的输出结果,难道真的是驱动问题?下面我把sql语句改了一下,改成查employeeid,还用oracle的驱动
public class test {
    public final static String SENIORITY="select * from hr_admin_employeeinf where employeeid=?";
    /** Creates a new instance of test */
    public test() {
    }
    public static void main(String args [])
    {
        try
        {
//        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");       
//        Connection con = DriverManager.getConnection("jdbc:odbc:mydsn","scott","tiger");
        
        Class.forName("oracle.jdbc.driver.OracleDriver");       
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@hstslc011:1521:eltp","scott","tiger");
        
        PreparedStatement ps = con.prepareStatement(SENIORITY);
//        ps.setString(1,"hr");
        ps.setInt(1,7000910);
        ResultSet rs = ps.executeQuery();
        System.out.println("-------------------vv------------------"+rs);
        if(rs.next())
        {
            System.out.println("-------------------vv------------------");
            System.out.println("-----------------------------"+rs.getString("DEPARTMENT"));
        }        
        }
        catch(Exception e)
        {
            System.out.println("______________________________"+e);
        }
        
    }
}
运行结果:-------------------vv------------------oracle.jdbc.driver.OracleResultSetImpl@1a16869
-------------------vv------------------
-----------------------------hr  
结果也正确。             

解决方案 »

  1.   

    貌似没错啊!输出rs.hasNext()的值看看
      

  2.   

    ps.setString(1,"hr");换成:ps.setString(0,"hr");
      

  3.   

    ?,resultset有hasnext()方法么?
      

  4.   

    select * from hr_admin_employeeinf where employeeid=?"; 在问号后面加1试试:employeeid=?1"; 
      

  5.   

    回7楼select * from hr_admin_employeeinf where employeeid=?"; 是可以输出正确结果的
      

  6.   

    3楼,我记得好像是从1开始的哦
    package com.shop.db;
    import java.sql.*;
    import java.io.*;/**************************************************
     * author:East(张栋芳)
     * date:2008-6-13
     * note:打开数据库的连接,和关闭连接
     **************************************************/public final class DataBaseOperator {

        public DataBaseOperator() {
        }
        
        /***
         *连接数据库用户指定driver,url,user,pwd
         */
        public static Connection createConnection(String driver,String url,String user,String pwd){
            Connection con = null;
            try{
                Class.forName(driver);
                con = DriverManager.getConnection(url,user,pwd);
            }catch(ClassNotFoundException ce){
                ce.printStackTrace();
            }catch(SQLException se){
                se.printStackTrace();
            }
            return con;
        }
        /***
         *连接数据库用户用默认的SQL server 2000的纯驱动
         */
        public static Connection createConnection(){
            String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
            String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=pubs";
            String user = "sa";
            String pwd = "";
            return createConnection(driver,url,user,pwd);
        }
        /***
         *创建一个连接通过配制文件
         */
        public static Connection createConnection(String fileName){
         String driver="";
         String url="";
         String user="";
         String pwd="";
         BufferedReader in = null;
         
         try{
          in =  new BufferedReader(new FileReader(fileName));
          driver = in.readLine();
          url = in.readLine();
          user = in.readLine();
          pwd = in.readLine();
          in.close(); 
         }catch(IOException ioe){
          ioe.printStackTrace();
          
         }
         return createConnection(driver,url,user,pwd);
        }
        /***
         *创建一个连接桥驱动
         */
        public static Connection createConnection(String driver,String url){
          driver = "sun.jdbc.odbc.JdbcOdbcDriver";
          url = "jdbc:odbc:myodbc";
         String user = "sa";
         String pwd = "";
         return createConnection(driver,url,user,pwd);
        }
        /***
         *关闭连接。con,pstmt,rs;
         */
        public static void closeAll(Connection con,PreparedStatement pstmt,ResultSet rs){
            try{
                    if(rs != null)rs.close();
                if(pstmt != null) pstmt.close();
                if(con != null) con.close();
            }catch(SQLException se){
                se.printStackTrace();
            }finally{
                rs = null;
                pstmt = null;
                con = null;        }
        }
        
        /***
         *关闭连接。con,pstmt;
         */ 
        public static void closeAll(Connection con,PreparedStatement pstmt){
            try{
                if(pstmt != null) pstmt.close();
                if(con != null) con.close();
            }catch(SQLException se){
                se.printStackTrace();
            }finally{
                pstmt = null;
                 con = null;
            }
        }
        /***
         *关闭连接。con
         */ 
        public static void closeAll(Connection con){
            try{
                if(con != null) con.close();
            }catch(SQLException se){
                se.printStackTrace();
            }finally{
                con = null;
            }
        }
        /**
         *关闭一个连接con,stmt,rs
         */
        public static void closeAll(Connection con,Statement stmt,ResultSet rs){
         try{
          if(con != null) con.close();
          if(stmt != null) stmt.close();
          if(rs != null) rs.close();
         }catch(SQLException se){
          se.printStackTrace();
         }
        }
        /**
         *关闭一个连接con,stmt
         */
        public static void closeAll(Connection con,Statement stmt){
         try{
          if(con != null) con.close();
          if(stmt != null) stmt.close();
         }catch(SQLException se){
          se.printStackTrace();
         }
        }}
      

  7.   

    对于这个oracle 10g时,用的要是jdbc14.jar吧,对于oracle 9i的驱动,要求更严啦,那就要根据自己使用的版本而定啦!
      

  8.   

    我用的是oracle9,驱动是class111,换成class12还是一样
      

  9.   

    谢谢大家啦,问题找出来了,问题出在表里,hr的类型是char(25),他把字段长度给设定死了,不到25个字符自动补空格,晕,所以怎么也找不到‘hr’,改成varchar2就可以了。