Java将单元格转换为字符串

时间:2011-10-21 14:26:33

标签: java sql

我有以下Java代码:(参数是登录数据以及数据库的查询)

public static String connectDB(String configFile, String query) throws FileNotFoundException, IOException, SQLException, ClassNotFoundException {
    Properties p = new Properties();
    p.load(new FileInputStream(configFile));

    String serverName = (p.getProperty("RMS_DBServerName"));
    String portNumber = (p.getProperty("DB_PortNumber"));
    String sid = (p.getProperty("RMS_SID"));
    String url = "jdbc:oracle:thin:@//" + serverName + ":" + portNumber + "/" + sid;
    String username = (p.getProperty("RMS_Username"));
    String password = (p.getProperty("RMS_Password"));

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection connection = DriverManager.getConnection(url, username, password);

    try {
        Statement stmt = connection.createStatement();

        try {
            ResultSet rset = stmt.executeQuery(query);
            try {
                while (rset.next()) {
                    System.out.println(rset.getString(1));
                }
            } finally {
                try {
                    rset.close();
                } catch (Exception ignore) {
                }
            }
            return rset;
        } finally {
            try {
                stmt.close();
            } catch (Exception ignore) {
            }
        }
    } finally {
        try {
            connection.close();
        } catch (Exception ignore) {
        }
    }
}

代码应该从数据库中获取一个单元格。将结果提供给控制台时,查询将起作用。但我想将方法​​中的单元格返回到另一种方法,以便自动化该过程。但是返回的代码是这样的:jdbc:oracle:OracleDriver#........所以任何人都可以帮助解决这个问题,我只想返回一个包含数字的单元格。

3 个答案:

答案 0 :(得分:2)

不是打印字符串,而是返回它。现在你要返回结果集的字符串表示。

答案 1 :(得分:1)

更改

while (rset.next())
         System.out.println(rset.getString(1));
}

if(rset.next()) {
     return rset.getString(1);
}

并删除

return rset;

答案 2 :(得分:0)

我解决了这个问题我得到的代码

public static String connectDB(String configFile, String query) throws FileNotFoundException, IOException, SQLException, ClassNotFoundException{
    Properties p = new Properties();
    p.load(new FileInputStream(configFile));

    String serverName = (p.getProperty("RMS_DBServerName"));
    String portNumber = (p.getProperty("DB_PortNumber"));
    String sid = (p.getProperty("RMS_SID"));
    String url = "jdbc:oracle:thin:@//" + serverName + ":" + portNumber + "/" + sid;
    String username = (p.getProperty("RMS_Username"));
    String password = (p.getProperty("RMS_Password"));

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection connection = DriverManager.getConnection(url,username,password);
    String setr = null;
    try {      
        Statement stmt = connection.createStatement();

        try {ResultSet rset = stmt.executeQuery(query);
            try {
                while(rset.next())   
                    setr = rset.getString(1);
                    return setr;  
            }        
            finally {
                try { rset.close(); 
                } 
                catch (Exception ignore) {}

            }
        } 
        finally {
            try { stmt.close(); 
            } 
            catch (Exception ignore) {}
        }
    } 
    finally {
        try { connection.close(); 
        } 
        catch (Exception ignore) {}

    }
}