私が持っているクラスは、さらにJDBC操作のためのConnectionオブジェクトを返します。このメソッドの結果 "return"でグローバル接続フィールドを作成する可能性はありますか?私はこのフィールドを私が必要としている場所で使用したいと思っています。あなたがこれを行うことができます静的グローバルオブジェクト
public class Connection
{
public static Connection makeConnection() throws IOException, SQLException
{
try
{
Class.forName("org.postgresql.Driver");
Properties props = new Properties();
FileInputStream in = new FileInputStream("dataBase.properties");
props.load(in);
in.close();
String drivers = props.getProperty("jdbc.drivers");
if(drivers != null) System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
return DriverManager.getConnection(url, username,password);
}
catch (ClassNotFoundException e)
{
return null;
}
catch(IOException e)
{
return null;
}
catch(SQLException e)
{
return null;
}
}
}
あなたは 'class Foo {static Connection conn = bar();のようになります。静的な接続バー(){...}} '? –
あなたがグローバルであなたのクラスに静的を意味するならば、はい。私はこれが本当に悪い設計だと指摘しなければならないが、JDBC Connectionオブジェクトはこのように使用するようには構築されていない。 – Perception