我一直在使用这个JDBC连接在我的所有类中必须运行查询,但我创建了一个新的类,我不希望构造函数与从JDBC类DConnection的参数(主数据库类)。 但我一直在获取NullPointExceptions。反正可以找出那个问题可能是什么。 谢谢。对所有类使用一个jdbc连接类java
public class UsersDao {
// associating the Database Connection objekt
private DConnector connector;
private final Connection myConn;
// Constructor
public UsersDao() throws CZeitExceptionHand,SQLException {
myConn = connector.getConnenction();
}
public boolean updateUsers(String mitarb, int mid) throws SQLException{
// PreparedStatement myStmt = null;
Statement stmt = myConn.createStatement();
try {
String myStmt = "SELECT Bly "
+ "" + mid + ";";
return stmt.execute(myStmt);
} finally {
close(stmt);
}
}
实施例这样的方法,其工作,但在不同类
String[][] getAllTheWorkers(DConnector connector) throws CZeitExceptionHand {
try {
Connection connect = connector.getConnenction();
Statement stmt = connect.createStatement();
ResultSet result = stmt.executeQuery("SELECT ");
result.last();
int nt = result.getRow();
result.beforeFirst();
}
return results;
} catch (SQLException e) {
throw new CZeitExceptionHand("Error: " + e);
}
}
你可以在这里定义你的连接器:'private DConnector connector;'但是你永远不会初始化它,因此当你尝试在这里使用它时它是空的'myConn = connector.getConnenction();' –
它,因为是一个被称为或关联的对象。所以我不知道可能会给我一个例子。 – wandaga1
我的意思是将它设置为等于某个东西,或者使用'new'关键字来创建一个新的对象。例如:'private DConnector connector = new DConnector();' –