내가있어 클래스는 더 JDBC 작업 연결 개체를 반환합니다. 이 메서드의 결과가 "반환"인 경우 전역 연결 필드를 만들 가능성이 있습니까? 나는이 분야를 내가 필요한 곳에 사용하고 싶다.정적 전역 객체
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 커넥션 객체는 이런 식으로 사용하도록 만들어지지 않았다. – Perception