'Data Source Explorer'에 해당되는 글 3

  1. 2009.07.26 Eclipse Data Source Explorer에 Derby 설정
  2. 2009.07.26 Eclipse, WTP, Derby로 웹 애플리케이션 구현하기
  3. 2009.07.23 Eclipse에서 OpenJPA 사용하기

Eclipse Data Source Explorer에 Derby 설정

더비를 네트워크 서버로 실행시켰는데 Eclipse Data Source Explorer에서는 접속이 안됨.
데이터베이스 경로를 data/airlinesDB이 아닌 절대경로로 맞출 것.
가니메데에서는 10.2 만 지원해서 그런지.
(더비는 10.5 인데 클라이언트도 버전을 타는지 잘 모르겠음)


Eclipse, WTP, Derby로 웹 애플리케이션 구현하기

http://www.ibm.com/developerworks/kr/library/dm-0509cline/
  1. Import LowFareAir.war
  2. Import derbyclient.jar, jstl.jar, standard.jar
  3. Import /data/airlinesDB/
  4. Import /devlib/derby.jar, derbynet.jar, derbyrun.jar
  5. build.xml : Derby를 서버로 구동시키는 Ant 스크립트, derby.properties
  6. web.xml 수정 : 데이터베이스 경로, 사용자/암호
  7. com.ibm.sample.DerbyDatabase.java 수정 : 데이터베이스 경로, 사용자/암호
  8. Data Source Explorer에 Derby 설정
  9. Tomcat 시작
- web.xml
<context-param>
	<param-name>javax.servlet.jsp.jstl.sql.dataSource</param-name>
	<param-value>
		jdbc:derby://localhost:1527/data/airlinesDB;user=USER;password=PASSWORD,
		org.apache.derby.jdbc.ClientDriver
	</param-value>
</context-param>

Eclipse에서 OpenJPA 사용하기

- 이클립스 유로파로 웹 개발하기, Part 1: 이클립스를 위한 Java EE (한글)

- Data Source Explorer 사용하기
  • Database 접속 설정하기
  • 테이블 생성
  • SQL 실행
  • 데이터 입력하고 저장하기
- OpenJPA 설정하기

- /baseball/src/org/developerworks/baseball/Player.java
@Entity
@Table(schema="baseball", name = "players")
public class Player implements Serializable {
	@Id
	@GeneratedValue(strategy=IDENTITY)
	private int id;

	@Column(name="First_Name")
	private String firstName;

	@Column(name="Last_Name")
	private String lastName;
        
	//setter and getter
}

- /baseball/src/META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
	<persistence-unit name="baseball">
		<class>org.developerworks.baseball.Player</class>
		<properties>
			<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/baseball"/>
			<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
			<property name="openjpa.ConnectionUserName" value="team"/>
		    <property name="openjpa.ConnectionPassword" value="1234"/>
		    <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
		</properties>
	</persistence-unit>
</persistence>

- 테스트 코드
EntityManagerFactory factory = Persistence.createEntityManagerFactory("baseball");
EntityManager em = factory.createEntityManager();
List<Player> players = em.createQuery("SELECT p FROM Player p").getResultList();
for(Player player : players){
	System.out.println(player.getFirstName());
}

- 필요한 라이브러리
  • openjpa-1.2.1.jar : User Libraries로 추가
  • geronimo-jpa_3.0_spec-1.0.jar : JPA 구현체
  • geronimo-jta_1.1_spec-1.1.jar
  • serp-1.13.1.jar
  • commons-collections-3.2.jar
  • commons-lang-2.1.jar
- 참고
Java Persistence API
Implementation of Java Persistence API Downloads : 이 구현체는 toplink-essentials.jar, toplink-essentials-agent.jar 임