如何为SQL Server配置persistence.xml

时间:2022-09-05 11:50:41

How can I configure persistence.xml file to connect to my SQL Server. Some information is connected as: Host, DatabaseName, UserName, Password, Port, ...

如何配置persistence.xml文件以连接到我的SQL Server。一些信息连接为:Host,DatabaseName,UserName,Password,Port,...

2 个解决方案

#1


0  

Setting up a connection pool to MySQL database using a configuration .xml file can be pretty troublesome to someone who has just started like you.

使用配置.xml文件为MySQL数据库建立连接池对于刚开始喜欢你的人来说非常麻烦。

In my opinion, you should take a look at this short guide. You simply need to start Glassfish server, open the browser and surf to http://yourdomain.com:4848 to access the Admin panel. After that, just follow the instruction in that article and to create the JDBC Connection pool and JDBC Resource. At this point, you only need to open the persistence.xml file with your IDE (NetBeans, etc.) and set the Data Source property with name of your JDBC Resource and you're done.

在我看来,你应该看看这个简短的指南。您只需启动Glassfish服务器,打开浏览器并浏览到http://yourdomain.com:4848即可访问“管理”面板。之后,只需按照该文章中的说明操作,即可创建JDBC连接池和JDBC资源。此时,您只需要使用IDE(NetBeans等)打开persistence.xml文件,并使用JDBC资源的名称设置数据源属性,您就完成了。

One thing to note is that you must download the latest MySQL Connector/J and copy the file

需要注意的一点是,您必须下载最新的MySQL Connector / J并复制该文件

mysql-connector-java-<version>-bin.jar 

into the folder

进入文件夹

<GlassFish-install-folder>\glassfish\domains\domain1\lib\ext

Otherwise, you will run into the expcetion Class name is wrong or classpath is not set for : com.mysql.jdbc.jdbc2.optional.MysqlDataSource if you try to ping the database after creating the JDBC Connection pool.

否则,您将遇到expcetion类名错误或类路径未设置为:com.mysql.jdbc.jdbc2.optional.MysqlDataSource如果您尝试在创建JDBC连接池后ping数据库。

#2


0  

First of all donwload jdbc driver for sqlserver and put that in glassfish lib directory and boot that. Using Glassfish admin console:

首先为sqlserver下载jdbc驱动程序并将其放在glassfish lib目录中并启动它。使用Glassfish管理控制台:

  1. define a jdbc connection pool and specify Host, DatabaseName, UserName, Password, Port, ...
  2. 定义一个jdbc连接池并指定Host,DatabaseName,UserName,Password,Port,...

  3. define a jdbc resource and select connection pool name that you have specified in step 1
  4. 定义jdbc资源并选择您在步骤1中指定的连接池名称

  5. then grab the name of jdbc resource and put it in your persistence.xml file : <jta-data-source>YOUR-JDBC-RESOURCE-NAME</jta-data-source>
  6. 然后获取jdbc资源的名称并将其放在persistence.xml文件中: YOUR-JDBC-RESOURCE-NAME

and now you can connect to data base.

现在您可以连接到数据库。

If you are using Netbeans, it has some nice wizards to produce connection pool and jdbc resource in glassfish.

如果您使用Netbeans,它有一些很好的向导来生成glassfish中的连接池和jdbc资源。

Since you are using JEE6 this is another way using annotation:

由于您使用的是JEE6,这是另一种使用注释的方法:

    import javax.annotation.sql.DataSourceDefinition;

@DataSourceDefinition(name = "java:app/env/myDatasource",
        minPoolSize = 0,
        initialPoolSize = 0,
        className = "your.driver.class",
        serverName="localhost",
        user = "admin",
        password = "admin",
        databaseName = "test"
)

public class DbConfiguration {
}

For more info take a look at here.

有关详细信息,请查看此处。

#1


0  

Setting up a connection pool to MySQL database using a configuration .xml file can be pretty troublesome to someone who has just started like you.

使用配置.xml文件为MySQL数据库建立连接池对于刚开始喜欢你的人来说非常麻烦。

In my opinion, you should take a look at this short guide. You simply need to start Glassfish server, open the browser and surf to http://yourdomain.com:4848 to access the Admin panel. After that, just follow the instruction in that article and to create the JDBC Connection pool and JDBC Resource. At this point, you only need to open the persistence.xml file with your IDE (NetBeans, etc.) and set the Data Source property with name of your JDBC Resource and you're done.

在我看来,你应该看看这个简短的指南。您只需启动Glassfish服务器,打开浏览器并浏览到http://yourdomain.com:4848即可访问“管理”面板。之后,只需按照该文章中的说明操作,即可创建JDBC连接池和JDBC资源。此时,您只需要使用IDE(NetBeans等)打开persistence.xml文件,并使用JDBC资源的名称设置数据源属性,您就完成了。

One thing to note is that you must download the latest MySQL Connector/J and copy the file

需要注意的一点是,您必须下载最新的MySQL Connector / J并复制该文件

mysql-connector-java-<version>-bin.jar 

into the folder

进入文件夹

<GlassFish-install-folder>\glassfish\domains\domain1\lib\ext

Otherwise, you will run into the expcetion Class name is wrong or classpath is not set for : com.mysql.jdbc.jdbc2.optional.MysqlDataSource if you try to ping the database after creating the JDBC Connection pool.

否则,您将遇到expcetion类名错误或类路径未设置为:com.mysql.jdbc.jdbc2.optional.MysqlDataSource如果您尝试在创建JDBC连接池后ping数据库。

#2


0  

First of all donwload jdbc driver for sqlserver and put that in glassfish lib directory and boot that. Using Glassfish admin console:

首先为sqlserver下载jdbc驱动程序并将其放在glassfish lib目录中并启动它。使用Glassfish管理控制台:

  1. define a jdbc connection pool and specify Host, DatabaseName, UserName, Password, Port, ...
  2. 定义一个jdbc连接池并指定Host,DatabaseName,UserName,Password,Port,...

  3. define a jdbc resource and select connection pool name that you have specified in step 1
  4. 定义jdbc资源并选择您在步骤1中指定的连接池名称

  5. then grab the name of jdbc resource and put it in your persistence.xml file : <jta-data-source>YOUR-JDBC-RESOURCE-NAME</jta-data-source>
  6. 然后获取jdbc资源的名称并将其放在persistence.xml文件中: YOUR-JDBC-RESOURCE-NAME

and now you can connect to data base.

现在您可以连接到数据库。

If you are using Netbeans, it has some nice wizards to produce connection pool and jdbc resource in glassfish.

如果您使用Netbeans,它有一些很好的向导来生成glassfish中的连接池和jdbc资源。

Since you are using JEE6 this is another way using annotation:

由于您使用的是JEE6,这是另一种使用注释的方法:

    import javax.annotation.sql.DataSourceDefinition;

@DataSourceDefinition(name = "java:app/env/myDatasource",
        minPoolSize = 0,
        initialPoolSize = 0,
        className = "your.driver.class",
        serverName="localhost",
        user = "admin",
        password = "admin",
        databaseName = "test"
)

public class DbConfiguration {
}

For more info take a look at here.

有关详细信息,请查看此处。