Neon Eclipse에 EJB 및 Servlet 프로젝트를 포괄하고 Wildfly 10 서버에 배포하는 Enterprise Appilcation Solution이 있습니다.
Servlet은 접근 DB를 위해 EJB를 호출합니다.
JDBC 드라이버를 Servlet 프로젝트의 WEB-INF 폴더에 넣으면 모든 것이 잘 진행되지만 코드를 EJB로 이동하고 드라이버를 외부 JAR로 연결하면 (스크린 샷 참조) 오류가 발생합니다.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from [Module "deployment.NeoflexBank.ear.Neoflex.jar:main" from Service Module Loader]
다음은 DB에 액세스하는 데 사용하는 코드입니다.
public String test() {
String output = "";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
e.printStackTrace();
}
String url="jdbc:mysql://localhost:3306/neoflex";
String username="root";
String password="";
String query="select * from clients";
Connection conn;
try {
conn = (Connection) DriverManager.getConnection(url, username, password);
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
output += rs.getInt("id");
output += rs.getString("username");
output += rs.getString("birth_date");
output += rs.getString("name");
output += rs.getString("surename");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
return e.toString();
}
return output;
}
Java EE 서버에서이를 수행하는 방법은 DataSource를 정의한 다음 JDBC 연결을 가져 오는 데 사용하는 것입니다.
mysql-connector-java-5.1.40-bin.jar을 standalone\deployments
WildFly 설치 디렉터리로 이동합니다.
WildFly 서버를 시작하고 http : // localhost : 9990 에서 관리 콘솔을 엽니 다 . 여기에서 약간의 보안을 설정하려면 몇 가지 지침을 따라야 할 수 있습니다.
온 클릭 Configuration
탭을 클릭 한 다음 클릭 :
ㅏ. 서브 시스템
비. 데이터 소스
씨. 비 XA
파란색 Add
버튼을 클릭하고 지시를 따릅니다.
이제 EJB의 코드는 다음과 같습니다.
@Stateless
public class SomeEJB {
@Resource(name="java:/MySqlDS")
private DataSource ds;
public String test() {
String output = "";
String query="select * from clients";
try {
try (Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query)) {
while(rs.next())
{
output += rs.getInt("id");
output += rs.getString("username");
output += rs.getString("birth_date");
output += rs.getString("name");
output += rs.getString("surename");
}
}
} catch (SQLException e) {
return e.toString();
}
return output;
}
}
이것은 데이터베이스 구성에서 애플리케이션을 분리합니다. DataSource 구성은 명령 줄 스크립트에서도 수행 할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다