连接到oracle并从中获取一些数据的java程序

时间:2015-04-03 23:10:14

标签: java jdbc oracle11g

我使用eclipse作为我的IDE编写java程序。它通过JDBC驱动程序连接到oracle数据库并打印出一些数据。该程序的工作方式类似于魅力,但在ORDER DATE和SHIPDATE之后输出为零。

他们来自哪里以及如何删除它们?

想要执行Q1(1)或Q2(2)或(X)退出:

ORDER       ORDER DATE      SHIPDATE        COMPANY
====        ==========      ========        =======
10440       2007-02-10 00:00:00     2007-02-28 00:00:00     Save-a-lot Markets
10441       2007-02-10 00:00:00     2007-03-14 00:00:00     Old World Delicatessen

这是我的代码:

private static void doQ2(Connection conn)
    {
        try
        {
            conn = DriverManager.getConnection(dbURL, user, password);
            conn.clearWarnings();
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from VQ2");
            System.out.print("ORDER     ORDER DATE      SHIPDATE        COMPANY\n"
                        + "====     ==========      ========        =======");
            System.out.println("");
            while(rs.next())
            {
                System.out.print(rs.getString("ORDERID"));
                System.out.print("      ");
                System.out.print(rs.getString("ORDERDATE"));
                System.out.print("      ");
                System.out.print(rs.getString("SHIPPEDDATE"));
                System.out.print("      ");
                System.out.print(rs.getString("COMPANYNAME"));
                System.out.println("");
            }
            rs.close();
        }
        catch(SQLException e)
        {
            System.err.println(e.getMessage());
        }   
    }

2 个答案:

答案 0 :(得分:0)

您可以执行rs.getObject(“ORDERID”)并将其转换为oracle.sql.DATE。

然后致电:

toText

使用格式,尝试“YYYYmmdd”

可以找到格式here

答案 1 :(得分:0)

只需转换ORDERDATE和SHIPPEDDATE to_char' YYYY-MM-DD'

ResultSet rs = stmt.executeQuery("SELECT ORDERID, TO_CHAR (ORDERDATE, 'YYYY-MM-DD') ORDERDATE, "
                        + "TO_CHAR (SHIPPEDDATE, 'YYYY-MM-DD') SHIPPEDDATE, COMPANYNAME FROM VQ2");