从数据库中检索数据..

时间:2012-07-12 08:11:28

标签: java database

我编写此代码是为了将数据存储在数据库中,并使用 NetBeans 程序查看结果。但没有输出,也没有编译错误。我能知道这是什么问题吗?是否有任何丢失的jar文件

package database;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.*;

public class Database {

    static Connection conn;

    public Database() {
    }

    public static Connection getConnection() {
        return conn;
    }

    public static void main(String[] args) throws SQLException {

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("Could not load the driver");
        }
        String user = "SYSTEM"; //readEntry("Enter userid:  ");
        String pass = "mevooo";//readEntry("Enter password: ");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", user, pass);

        String query = "select Ref, Title,Risk_Rating, Root_cause, Impact, Likelihood, Efforts, Finding, Implication, Recommendation from  Lack_Of_Defense ";
        PreparedStatement myStatement = conn.prepareStatement(query);
        myStatement.clearParameters();
        ResultSet myResult = myStatement.executeQuery(query);

        while (myResult.next()) {
            String Ref = myResult.getString(1);
            String Title = myResult.getString(2);
            String Risk_Rating = myResult.getString(3);
            String Root_cause = myResult.getString(4);
            String Impact = myResult.getString(5);
            String Likelihood = myResult.getString(6);
            String Efforts = myResult.getString(7);
            String Finding = myResult.getString(8);
            String Implication = myResult.getString(9);
            String Recommendation = myResult.getString(10);

            System.out.println(" =============================================================");
            System.out.println(" ===================== Lack of defense Table ==================");
            System.out.println(" Ref  : " + Ref);
            System.out.println(" Title " + Title);
            System.out.println(" Risk Rating " + Risk_Rating);
            System.out.println(" Root cause " + Root_cause);
            System.out.println(" Impact " + Impact);
            System.out.println(" Likelihood " + Likelihood);
            System.out.println(" Efforts " + Efforts);
            System.out.println(" Finding " + Finding);
            System.out.println(" Implication " + Implication);
            System.out.println(" Recommendation " + Recommendation);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您的结果集似乎是空的,您是否尝试在sql客户端应用程序中执行查询?

答案 1 :(得分:0)

问题可能出在下面的行

        myStatement.clearParameters();
        ResultSet myResult = myStatement .executeQuery(query);  

clearParameters()真的需要吗?另外,从executeQuery()方法中删除查询参数。这不是必需的,因为您已经在PreparedStatement中设置了它。