比如cf create-service sqldb sqldb_small mySQLDB会报错,这里的service name及,plan都已经换成别的了.(坑)
正确的方式是:
cf create-service SQLDB SQLDB_OpenBeta SQLDB-cyper
绑定
cf bind-service cyperhellotest SQLDB-cyper
另外SQLDB和MYSQL的VCAP中的JSON格式竟然不一致.(坑)
最终的server.xml如下(双数据源)
<server description="new server"> <!-- Enable features --> <featureManager> <feature>servlet-3.0</feature> <feature>jdbc-4.0</feature> </featureManager> <dataSource id="blogDS" jndiName="jdbc/blogDS" connectionManagerRef="mysqlPool" jdbcDriverRef="mysqlDriver"> <properties databaseName="${cloud.services.mysql-cyper.connection.name}" serverName="${cloud.services.mysql-cyper.connection.host}" portNumber="${cloud.services.mysql-cyper.connection.port}" user="${cloud.services.mysql-cyper.connection.user}" password="${cloud.services.mysql-cyper.connection.password}" /> </dataSource> <connectionManager id="mysqlPool" maxPoolSize="10" /> <jdbcDriver id="mysqlDriver" libraryRef="mysqlLib" /> <library id="mysqlLib" filesetRef="mysqlFileset" /> <fileset id="mysqlFileset" dir="${shared.resource.dir}/mysql" includes="*.jar" /> <dataSource id="db2" jndiName="jdbc/db2"> <jdbcDriver libraryRef="DB2JCC4Lib"/> <properties.db2.jcc databaseName="${cloud.services.SQLDB-cyper.connection.db}" serverName="${cloud.services.SQLDB-cyper.connection.host}" portNumber="${cloud.services.SQLDB-cyper.connection.port}" user="${cloud.services.SQLDB-cyper.connection.username}" password="${cloud.services.SQLDB-cyper.connection.password}" /> </dataSource> <library id="DB2JCC4Lib"> <fileset dir="${shared.resource.dir}/db2" includes="*.jar"/> </library> <webApplication name="helloworld" location="helloworld.war" /> <httpEndpoint id="defaultHttpEndpoint" host="localhost" httpPort="9080" httpsPort="9443" /> </server>
启动时有个警告(但不影响使用)
bluemix docs中曾经写过.bluemix能自动注入SQLDB/MONGO,的连接信息,但前提是必须删除以上mysql的配置
如下:
<server description="new server"> <!-- Enable features --> <featureManager> <feature>servlet-3.0</feature> <feature>jdbc-4.0</feature> </featureManager> <dataSource id="db2" jndiName="jdbc/db2"> <jdbcDriver libraryRef="DB2JCC4Lib"/> <properties.db2.jcc databaseName="SAMPLE" serverName="localhost" portNumber="50000" user="db2admin" password="db2admin" /> </dataSource> <library id="DB2JCC4Lib"> <fileset dir="${shared.resource.dir}/db2" includes="*.jar"/> </library> <webApplication name="helloworld" location="helloworld.war" /> <httpEndpoint id="defaultHttpEndpoint" host="localhost" httpPort="9080" httpsPort="9443" /> </server>
以上的server.xml如果push,运行正常,没有任何警告
HelloServlet.java代码如下:
package test; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.annotation.Resource; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; @WebServlet("/HelloServlet") public class HelloServlet extends HttpServlet { @Resource(name = "jdbc/blogDS") private DataSource ds; @Resource(name = "jdbc/db2") private DataSource ds2; private Connection con; @Override protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<H1>Hello World Liberty Profile</H1>\n"); try { con = ds.getConnection(); Statement stmt = null; stmt = con.createStatement(); //drop table first~ in case table already exists stmt.executeUpdate("drop table if exists cities"); // create a table stmt.executeUpdate("create table cities (name varchar(50) not null primary key, population int, county varchar(30))"); // insert a test record stmt.executeUpdate("insert into cities values ('myHomeCity', 106769, 'myHomeCounty')"); // select a record ResultSet result = stmt .executeQuery("select county from cities where name='myHomeCity'"); result.next(); // display the county information for the city. out.println("The county for myHomeCity is " + result.getString(1)); // drop the table to clean up and to be able to rerun the test. stmt.executeUpdate("drop table cities"); } catch (SQLException e) { e.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } out.println("<H1>Hello World from DB2</H1>\n"); try { con = ds2.getConnection(); Statement stmt = null; stmt = con.createStatement(); // create a table stmt.executeUpdate("create table cities (name varchar(50) not null primary key, population int, county varchar(30))"); // insert a test record stmt.executeUpdate("insert into cities values ('myHomeCity', 106769, 'myHomeCounty')"); // select a record ResultSet result = stmt .executeQuery("select county from cities where name='myHomeCity'"); result.next(); // display the county information for the city. out.println("The DB2 county for myHomeCity is " + result.getString(1)); // drop the table to clean up and to be able to rerun the test. stmt.executeUpdate("drop table cities"); } catch (SQLException e) { e.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
哦对了.service.xml中对应的db2驱动在此.
C:\IBM\was855nalp\usr\shared\resources\db2\db2jcc.jar
这个jar包是从bluemix UI中java+db的boilerplate code是拷贝出来的
单机版server.xml的配置如下:
<server description="new server"> <!-- Enable features --> <featureManager> <feature>servlet-3.0</feature> <feature>jdbc-4.0</feature> </featureManager> <dataSource id="blogDS" jndiName="jdbc/blogDS" connectionManagerRef="mysqlPool" jdbcDriverRef="mysqlDriver"> <properties databaseName="test" serverName="localhost" portNumber="3306" /> </dataSource> <connectionManager id="mysqlPool" maxPoolSize="10" /> <jdbcDriver id="mysqlDriver" libraryRef="mysqlLib" /> <library id="mysqlLib" filesetRef="mysqlFileset" /> <fileset id="mysqlFileset" dir="${shared.resource.dir}/mysql" includes="*.jar" /> <dataSource id="db2" jndiName="jdbc/db2"> <jdbcDriver libraryRef="DB2JCC4Lib"/> <properties.db2.jcc databaseName="SAMPLE" serverName="localhost" portNumber="50000" user="db2admin" password="db2admin" /> </dataSource> <library id="DB2JCC4Lib"> <fileset dir="${shared.resource.dir}/db2" includes="*.jar"/> </library> <webApplication name="helloworld" location="helloworld.war" /> <httpEndpoint id="defaultHttpEndpoint" host="localhost" httpPort="9080" httpsPort="9443" /> </server>
参考:http://www-01.ibm.com/support/knowledgecenter/api/content/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ae/twlp_dep_configuring_ds.html
https://www.ng.bluemix.net/docs/#services/SQLDB/index.html#SQLDB