无法从其他类

时间:2018-01-12 15:05:59

标签: java jdbc

我试图创建一个帮助从数据库访问数据的库,并将在未来用于另一个应用程序。但是我认为我搞砸了某个地方而且我无法将类数据库中定义的Connection导入类功能。它抛出的错误是

  

默认构造函数无法处理Implicit构造函数抛出的异常类型Exception。必须定义一个显式的构造函数。

1)

public class database {

    public static String databaseName;
    public static String username;
    public static String password;
    public String tableName;
    public String schema;
    public String query;
    ResultSet rs;
    public static Connection dbconnection;

    public database(String databaseName, String username, String password) throws Exception {
        this.databaseName = databaseName;
        this.username = username;
        this.password = password;
    }

    public database() throws Exception{
        databaseName = "database";
        username = "admin";
        password = "12345";
        // TODO Auto-generated constructor stub
    }

    // Connect to selected Cache Database
    public static Connection connect() throws Exception {
        try {
            // Default port is 1972
            String url = "jdbc:Cache://127.0.0.1:1972/" + databaseName;
            Class.forName("com.intersys.jdbc.CacheDriver").newInstance();
            dbconnection = DriverManager.getConnection(url, username, password);
            System.out.println("Connected to " + databaseName + " database.");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dbconnection;
    }
}

2)

public class features {
    database db = new database();
    Connection conn = db.connect();
    ResultSet rs;
    Statement st = conn.createStatement();
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您的database类已经有连接和结果集变量,为什么在类features中放置另一个连接和结果集?,您应该只在一个类中处理连接,没有必要两个连接,因为您只连接到一个数据库

此外,每种方法都不需要throws Exception,我认为这对您有用:

<强>数据库

public class database {

    public static String databaseName;
    public static String username;
    public static String password;
    public String tableName;
    public String schema;
    public String query;
    ResultSet rs;
    public static Connection dbconnection;
    public Statement st;

    public database(String databaseName, String username, String password) {
        this.databaseName = databaseName;
        this.username = username;
        this.password = password;
    }

    public database(){
        databaseName = "database";
        username = "admin";
        password = "12345";
        dbconnection=connect();
        st= dbconnection.createStatement();
    }

    // Connect to selected Cache Database
    public static Connection connect() {
        try {
            // Default port is 1972
            String url = "jdbc:Cache://127.0.0.1:1972/" + databaseName;
            Class.forName("com.intersys.jdbc.CacheDriver").newInstance();
            dbconnection = DriverManager.getConnection(url, username, password);
            System.out.println("Connected to " + databaseName + " database.");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dbconnection;
    }
}

功能

public class features {
    public static database db;
    public ResultSet rs;

    public features (){
    db= new database();
    }

}

我对您的代码进行了一些更改:

  • 从您的班级conn中删除了变量features,因为已经在班级database中建立了连接
  • 将Statement变量st移至类database,因为您将处理该类中的connectiong
  • 通过添加构造函数修改了类features,因为您在声明变量并使用构造函数立即初始化它们

我做的修复应该删除错误,我没有时间检查代码中的语法错误,这取决于你,但我认为它应该工作得很好,错误是因为你在哪里抛出异常在类database的方法中并且不在类features中处理它们,有关此错误的更多信息,您可以检查另一个用户询问有关同一错误的此问题:

Why default constructor cannot handle exception type Exception?