저는 현재 실행중인 간단한 웹 서비스 (이미 완료되었습니다)가 있습니다. 저는 웹 서비스에 익숙하지 않으므로 어떤 접근 방식을 먼저 코딩했는지 코딩했습니다. 이것은 내가 DB에 연결 내가 함께 할 수있는 뭔가가 모든 시간을 어떻게웹 서비스에서 데이터베이스 연결 구현?
public class DBConfig {
/* Database Configuration Variables =====================================*/
private static final String HOST_NAME = "127.0.0.1";
private static final String PORT = "3306";
private static final String DB_NAME = "myDB";
private static final String DB_USERNAME = "root";
private static final String DB_PASSWORD = "";
/**
* Opens a connection based on the database details set internally
* and returns the active connection.
*
* @return the active connection with the database
*/
public static Connection openConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = (Connection) DriverManager.getConnection(
"jdbc:mysql://" +
DBConfig.HOST_NAME + ":" +
DBConfig.PORT + "/" +
DBConfig.DB_NAME,
DBConfig.DB_USERNAME,
DBConfig.DB_PASSWORD);
}
catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
:
현재, 내가 좋아하는 뭔가를 보이는 DBConfig 클래스가 있습니다. 이 연결은 이와 같은 호출 메소드에 의해 닫힙니다.
public class addSomething(){
...
Connection con = DBConfig.getConnection();
...
...
con.close();
}
참고 : 이것은 REST 웹 서비스
질문입니다 :이 그냥 OK인가? (나는이 단지 충분, 오히려 거기에 더 나은 구현을 많이 알고 있지만?)
또 다른 질문 : 수출 내 웹 서비스, 이제 something.war (1 개 파일), 내가 어떻게 연결 속성을 변경할 수있는 경우 ? 소스 코드를 변경 한 다음 재 엑스포트해야합니까? 이 작업을 수행하는 더 좋은 방법이 있습니까? 제발 제안 해주세요. 고맙습니다.
코드를 어디에서 실행하고 있습니까? Tomcat 또는 일부 java-ee 서버에서? (나는 그 태그를 알아 차렸다.) 예를 들어 Jboss에서 실행중인 경우 데이터베이스와 연결하기위한 잘 정의 된 접근 방식이 있습니다. – bjedrzejewski