Jdbc从数据库返回未经检测的结果

时间:2016-05-24 17:11:59

标签: java oracle jdbc

这里的代码从数据库返回错误/意外结果。我无法弄清楚问题。这是代码

import java.sql.*;

class test1
{
 public static void main(String [] args)
 {
    try
    {
        Class.forName("oracle.jdbc.OracleDriver");    

        System.out.println("Driver loaded successfully");
    }
    catch(ClassNotFoundException ex)
    {
        System.out.println("Could not load "+ex.getMessage());
        System.exit(0);
    }
    Connection con=null; 
    try
    {
        con=DriverManager.getConnection("jdbc:oracle:thin:@//Apoorv-PC:1521/orcl","scott","tiger");
        Statement st=con.createStatement();  
        //st.setMaxRows(2);
        //System.out.println(st.getMaxRows());
        ResultSet rs=st.executeQuery("Select park_space from park_available where area='M.P.Nagar'"); 

        while(rs.next())
        {    
            String p=rs.getString(1);
            System.out.println(" "+p);
        }   
        con.close();
        System.out.println("Connection closed!");
    }
    catch(SQLException e)
    {
        System.out.println("Error in database"+e.getMessage());
    }
    if(con!=null)
    {
        System.out.println("Successful!");
    }
 }  
}

oracle中我的park_available表有5列:area(varchar2),arr_date(varchar2),arr_time(varchar2), park_space(数字),dept_time(varchar2)。

这里我试图访问第4列" park_space"它有价值" 10"对于每一行,但我的代码在与getInt()一起使用时返回0,在使用getString()时返回null。

根据我的代码,我是否必须使用rs.next()方法返回的结果是特定行的特定列,即只有一个值?

4 个答案:

答案 0 :(得分:0)

试试这个:

while(rs.next()) {    
    BigDecimal p=rs.getBigDecimal(1);
    System.out.println(" "+p);
}

我猜因为您的列是一个NUMBER,对于一个int来说它太大了(例如,请参阅Oracle关于数据taype映射的注释:https://docs.oracle.com/cd/E11882_01/java.112/e16548/datacc.htm#JJDBC28367)。

[编辑] 要导入" java.math.BigDecimal"你必须添加这样的import语句:

import java.sql.*;
import java.math.BigDecimal;

到Java文件的开头。

答案 1 :(得分:0)

要验证您的驱动程序没有问题,请参阅完整的测试程序:

public static void main(String[] args) throws Exception {
    try (Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@...", "...", "...")) {
        try {
            try (Statement stmt = conn.createStatement()) {
                stmt.executeUpdate("CREATE TABLE NUMBER_TEST ( COL1 NUMBER )");
            }
            try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO NUMBER_TEST VALUES (?)")) {
                Random rnd = new Random();
                for (int i = 0; i < 10; i++) {
                    stmt.setDouble(1, rnd.nextDouble() * 100.0);
                    stmt.executeUpdate();
                }
            }
            try (Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery("SELECT COL1 FROM NUMBER_TEST")) {
                while (rs.next()) {
                    System.out.println(rs.getInt(1) + "  " + rs.getDouble(1) + "  " + rs.getString(1));
                }
            }
        } finally {
            try (Statement stmt = conn.createStatement()) {
                stmt.executeUpdate("DROP TABLE NUMBER_TEST");
            } catch (Exception e) {
                System.out.println("DROP failed: " + e);
            }
        }
    }
}

SAMPLE OUTPUT (随机数据)

52  52.076813172962844  52.076813172962844
7  7.747368165641289  7.747368165641289
1  1.906398483271876  1.906398483271876
95  95.4035973169611  95.4035973169611
60  60.2402712112802  60.2402712112802
72  72.70235593122439  72.70235593122439
62  62.18918689642948  62.18918689642948
16  16.87108578421145  16.87108578421145
20  20.20241078545453  20.20241078545453
0  0.32630814336951364  .32630814336951364

如您所见,getIntgetDoublegetString都返回了良好的数据。

针对9.2数据库测试11.2.0.4 JDBC驱动程序。

答案 2 :(得分:0)

while(rs.next())
        {    
            String p=rs.getInt(1);
            System.out.println(" "+p);
        }   

@Apoorv singhai您正在尝试接受字符串输入,而您的列包含Number数据类型,请改用此代码。

答案 3 :(得分:-1)

Dear The above your code is write no problem.

I think May be problem in Your Connection URL Check that Once clearly...

I was Executed code in Eclipse with localhost url i got output.No Errors

See Code Bellow

    import java.sql.*;

class test1
{
 public static void main(String [] args)
 {
    try
    {
        Class.forName("oracle.jdbc.OracleDriver");    

        System.out.println("Driver loaded successfully");
    }
    catch(ClassNotFoundException ex)
    {
        System.out.println("Could not load "+ex.getMessage());
        System.exit(0);
    }
    Connection con=null; 
    try
    {
        con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521","sample1","sample1");
        Statement st=con.createStatement();  
        //st.setMaxRows(2);
        //System.out.println(st.getMaxRows());
        ResultSet rs=st.executeQuery("Select park_space from park_available where area='M.P.Nagar'"); 

        while(rs.next())
        {    
            String p=rs.getString(1);
            System.out.println(" "+p);
        }   
        con.close();
        System.out.println("Connection closed!");
    }
    catch(SQLException e)
    {
        System.out.println("Error in database"+e.getMessage());
    }
    if(con!=null)
    {
        System.out.println("Successful!");
    }
 }  
}