我用javabean调用了一个类,但是出了点问题,请大家帮忙解决一下,很多论坛上都没解决我这个问题,不知道是太简单了还是别的什么。急急急

xiaoduzi 2002-05-13 09:58:28
希望帮帮我们这些新手。


ConnectionFactory.java

package com.pingod.conn;

import java.sql.*;

public class ConnectionFactory {
private String error;
private Connection conn;

private ConnectionFactory() {}

public Connection getConnect() throws ClassNotFoundException,SQLException,Exception {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:dishui","sa","");
} catch(ClassNotFoundException cnfe) {
error = "ClassNotFoundException: Could not locate DB driver.";
throw new ClassNotFoundException(error);
} catch(SQLException cnfe) {
error = "SQLException: Could not connect to database.";
throw new SQLException(error);
} catch(Exception cnfe) {
error = "Exception: An unknown error occurred while connecting to database.";
throw new Exception(error);
}
return conn;
}

public void disconnect() throws SQLException{
try {
if (conn != null) {
conn.close();
}
} catch (SQLException sqle) {
error = "SQLException: Unable to close the database connection.";
throw new SQLException(error);
}
}

}

connect.java
package com.pingod.conn;

import java.sql.*;
import java.util.*;

public class Connect {
private String error="";
private String mysql="";
private ConnectionFactory connectFactory = null;
private Connection con = null;

public Connect() {}

public void openConnect() throws SQLException,Exception {
con = connectFactory.getConnect();//我把Exception去掉后,编译时提示“.”有错误。
}

public ResultSet viewQuery(String s) throws SQLException,Exception {
ResultSet rs = null;
try {
mysql = s;
Statement stmt = con.createStatement();
rs = stmt.executeQuery(mysql);
} catch(SQLException sqle) {
error = "SQLException: Could not execute to the query.";
throw new SQLException(error);
} catch(Exception e) {
error = "An exception occurred while retrieving database.";
throw new Exception(error);
}
return rs;
}

}

我在jsp页面调用connect.openConnect();


在程序中,编译时,发现如下问题:
public void openConnect() throws SQLException,Exception {
try{
con = connectFactory.getConnect();
}catch(Exception ex)
{ // 处理异常. } //加上判断异常时,就会显示这个异常错误,我感觉好像是空句柄方面的错误。
}
...全文
64 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
saintKnight 2002-05-13
同意楼上的
回复
coinicon 2002-05-13
private ConnectionFactory connectFactory = null;
改为
private ConnectionFactory connectFactory = new ConnectionFactory();

或在构造函数中实例化connectFactory
public Connect()
{
connectFactory = new ConnectionFactory();
}
回复
ken_henderson 2002-05-13
再改public void openConnect() throws SQLException,Exception {
try{
con = ConnectionFactory.getConnect();//static method
}catch(Exception ex)
{ // 处理异常. }
回复
ken_henderson 2002-05-13
改为public static Connection getConnect() throws ClassNotFoundException,SQLException,Exception {
回复
zfluo 2002-05-13
private ConnectionFactory connectFactory = null;
你的connectFactory是null的
回复
xmvigour 2002-05-13
public class Connect {
private String error="";
private String mysql="";
private ConnectionFactory connectFactory = null;
改为:
public class Connect {
private String error="";
private String mysql="";
private ConnectionFactory connectFactory ;
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告