我有一个基于EJB的应用程序,它连接到MySQL数据库并提供用于更新操作的Web UI。部署到本地WAS Liberty时,它可以正常工作。
以下是数据源的server.xml配置。
<library id="MySQLDriverLib">
<file name="${User-defined_JDBC_DRIVER_PATH}/mysql-connector-java-5.1.38-bin.jar"/>
</library>
<dataSource id="DefaultDataSource" type="javax.sql.ConnectionPoolDataSource" transactional="true">
<jdbcDriver libraryRef="MySQLDriverLib"/>
<properties URL="jdbc:mysql://localhost:3306/ic16_lab2434" connectionUrl="jdbc:mysql://localhost:3306/ic16_lab2434" driver="com.mysql.jdbc.Driver" driverClass="com.mysql.jdbc.Driver" metadata="mySQL" password="object00" user="root" userName="root" />
</dataSource>
<variable name="User-defined_JDBC_DRIVER_PATH" value="C:\Software\mysql-connector-java-5.1.38"/>
正如您所看到的,它使用JDBC驱动程序jar的库,它使用path to file指定。显然,如果我尝试将EAR部署到Bluemix Liberty,这将无效。这就是我部署整个服务器目录以进行最少数量更改的原因。即使在这种情况下,我也不知道如何为数据源正确配置JDBC驱动程序库,以便服务器选择它。请帮忙。
答案 0 :(得分:1)
如果要连接到mysql数据库并希望手动在server.xml中提供凭据,则可以执行以下操作:
server.xml中:
public class Bike {
int speed;
int gear;
void changeGear(int newVal) {
gear = newVal;
}
void speedUp(int newVal) {
speed = newVal;
}
void breaks(int slow) {
speed = speed + slow;
}
void printState() {
System.out.println("Gear is: " + gear);
System.out.println("Speed is: " + speed + ("MPH"));
}
}
//________________________
public static boolean cycle = true;
public static Bike b1 = new Bike();
public static int x;
public static int y;
public static Scanner input = new Scanner(System.in);
public static int choice;
//______________________________
public static void cycling() {
while (cycle == true) {
System.out.println("What would you like to do now? Enter a number.");
System.out.println("1: Speed Change");
System.out.println("2: Change Gear");
choice = input.nextInt();
if (choice == 1) {
System.out.println("Choose a speed change");
y = input.nextInt();
if (y < 0) {
b1.breaks(y);
if (b1.speed < 0) {
b1.speed = Math.abs(y) + y;
System.out.println("You've stopped entirely");
}
}
if (y >= 0) {
b1.speed = y;
b1.printState();
if (b1.speed >= 0 && b1.speed <= 10) {
while (b1.gear != 1) {
System.out.println("You need to be in Gear 1 for " + "that! Please change gears.");
x = input.nextInt();
b1.changeGear(x);
}
if (b1.speed >= 11 && b1.speed <= 20) {
while (b1.gear != 2) {
System.out.println("You need to be in Gear 2 for" + "that! Please change gears.");
x = input.nextInt();
b1.changeGear(x);
}
if (b1.speed >= 21) {
while (b1.gear != 3) {
System.out.println("You need to be in Gear 3 for" + "that! Please change gears.");
x = input.nextInt();
b1.changeGear(x);
}
}
/*if(b1.speed >= 0 && b1.speed <=10){
b1.gear = 1;
}else if(b1.speed >= 11 && b1.speed <=20){
b1.gear = 2;
}else if(b1.speed >= 21){
b1.gear = 3;
}*/
b1.printState();
}
}
}
}
}
}
在这个例子中,我将把mysql jar文件放在服务器配置目录<dataSource jndiName="jdbc/TradeDataSource">
<jdbcDriver id="mysqlDriver" libraryRef="mysql-connector" />
<properties
URL="jdbc:mysql://1.2.3.4:3306/db"
password="mypassword" user="admin" />
</dataSource>
<library description="MySQL JDBC Driver" id="mysql-connector"
name="MySQL Connector">
<fileset dir="${server.config.dir}" id="mysql-connector-jar"
includes="mysql-connector-java-*.jar" />
</library>
您现在可以直接从defaultServer dir
wlp/usr/servers/defaultServer/mysql-connector-java-5.1.34-bin.jar
但是,自由buildpack可以自动为从Bluemix目录绑定的数据库生成server.xml数据源配置。例如,如果我创建SQLDB或ClearDB服务并将其绑定到Liberty应用程序并将服务命名为&#34; TradeDataSource&#34;,buildpack将生成配置并自动将正确的驱动程序jar添加到类路径中。
cf files yourappname app / wlp / usr / servers / defaultServer / server.xml
cf push
我现在可以使用其jndi名称查找数据源:<dataSource id='mysql-TradeDataSource' jdbcDriverRef='mysql-driver' jndiName='jdbc/TradeDataSource' transactional='true' type='javax.sql.ConnectionPoolDataSource'>
<properties id='mysql-TradeDataSource-props' databaseName='${cloud.services.TradeDataSource.connection.name}' user='${cloud.services.TradeDataSource.connection.user}' password='${cloud.services.TradeDataSource.connection.password}' portNumber='${cloud.services.TradeDataSource.connection.port}' serverName='${cloud.services.TradeDataSource.connection.host}'/>
<connectionManager id='mysql-TradeDataSource-conMgr' maxPoolSize='10'/>
</dataSource>
<jdbcDriver id='mysql-driver' javax.sql.XADataSource='org.mariadb.jdbc.MySQLDataSource' javax.sql.ConnectionPoolDataSource='org.mariadb.jdbc.MySQLDataSource' libraryRef='mysql-library'/>