-2

我编写此代码是为了将数据存储在数据库中并使用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);
        }
    }
}
4

2 回答 2

0

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

于 2012-07-12T08:26:19.777 回答
0

问题可能出在以下几行

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

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

于 2012-07-12T08:34:51.523 回答