use MS JDBC Driver in Tomcat / GlassFish

Tomcat與GlassFish用法大致相同,只有jar包的儲存位置有差異,步驟如下

●下載JDBC Driver
Microsoft JDBC Driver 4.0 for SQL Server下載sqljdbc4.jar

●把JAR包放到lib資料夾
 ◎Tomcat
       放至Tomcat Server安裝位置的lib資料夾,如果Tomcat安裝在C:\Tomcat\,那就放到C:\Tomcat\lib
 ◎GlassFish
       放至GlassFish Server其domain下的ext資料夾,如果GlassFish安裝在C:\glassfish3\,且domain名稱為domain1,那就放到C:\glassfish3\glassfish\domains\domain1\lib\ext

●定義persistence.xml
< ?xml version="1.0" encoding="UTF-8" ?>
< 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_2_0.xsd"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    < persistence-unit name="jpa unit name" transaction-type="RESOURCE_LOCAL">
        < provider>org.eclipse.persistence.jpa.PersistenceProvider</ provider>
        < exclude-unlisted-classes>false</ exclude-unlisted-classes>
        < properties>
            < property name="javax.persistence.jdbc.driver"
                value="com.microsoft.sqlserver.jdbc.SQLServerDriver" / >
            < property name="javax.persistence.jdbc.url"
                value="jdbc:sqlserver://sqlserver的位址:1433;databaseName=資料庫名稱" / >
            < property name="javax.persistence.jdbc.user" value="資料庫帳號" / >
            < property name="javax.persistence.jdbc.password" value="資料庫密碼" / >
        </ properties>
    </ persistence-unit>
</ persistence>

沒有留言: