OpenShift webapps mysql java连接 [英] OpenShift webapps mysql java connection

查看:161
本文介绍了OpenShift webapps mysql java连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在openshift上有一个JBoss EWS Tomcat 7 Java应用程序,安装了MySQL 5.5和PhpMyAdmin 4.0盒式磁带。我在本地复制了存储库,并删除了源文件夹。相反,我只是有一个已编译的Java应用程序,一个WAR文件,已被复制到webapps目录,git被推送到OpenShift服务器。

I have a JBoss EWS Tomcat 7 Java application on openshift, installed with the MySQL 5.5 and PhpMyAdmin 4.0 cartridges. I have copied the repository locally, and deleted the source folder. Instead, I simply have a compiled Java application, a WAR file, that has been copied into the webapps directory and git pushed to the OpenShift servers.

如果我有一些数据库,我怎么能在webapp编译的war文件中访问它。标准MySQL JDBC会起作用吗?根据我读过的一些帖子,OpenShift阻止访问数据库的外部请求,在这种情况下,webapp目录的war文件可能正在发出外部请求。其来源如下: https://www.openshift.com/ forums / express / external-access-to-mysql (由信誉良好的OpenShift开发人员回答)。

If I have some database, how would I be able to access it in the webapp compiled war file. Would standard MySQL JDBC work? According to some posts I have read, OpenShift blocks the accessing of external requests to the database, and in this case, the webapp directory's war file may be making an "external request". The source for that is here: https://www.openshift.com/forums/express/external-access-to-mysql (as answered by reputable OpenShift developers).

我过去实际上尝试过JDBC而它没有工作,但这可能是由于错误的代码。如果有人想编写一些显示如何完成此操作的代码,我将不胜感激。如果你可以测试它会更好:)

I actually tried JDBC in the past and it did not work, but that may have been due to incorrect code. If someone wants to write some code that shows how this would be done, I would appreciate it. And if you could test it that would be even nicer :)

顺便说一下,在答案中请不要包括端口转发。我知道有效,我以前尝试过,端口转发工作。但它通常有点不安全,每次都必须从计算机启动。

By the way, in the answer please do not include port forwarding. I know that works, I have tried it before, and port forwarding works. But it is often a little insecure, and has to be started each time from a computer.

推荐答案

我已经解决了这个问题我非常兴奋。虽然我自己没有得到帮助,但我希望有其他人来这个帖子并且能够让它正常工作!

I have resolved the issue by myself and was extremely excited. Although I did not receive help myself, I hope someone else comes along this thread and is able to get it working!

所以我有一个Vaadin应用程序,已经编译进入WAR文件。我通过以下步骤将其部署到OpenShift服务器:

So I have a Vaadin application, which has been compiled into a WAR file. I deployed it to OpenShift servers by the following steps:


  1. 在Web浏览器中打开OpenShift。登录Openshift。导航到相关应用程序。

  2. 获取位于磁带右侧的应用程序的ssh代码(它应该在屏幕右侧)。使用Command-C或Ctrl-C复制该代码。

  3. 打开终端并输入 git clone ssh:\\xxxxxxxxxxxxxxxx ...

  4. 如果你在Mac上,就像我一样,它应该在 Users / Username / Appname 创建一个项目目录。在该目录中,删除源文件夹和 pom.xml 。获取已编译的WAR文件并将其复制到 webapps 目录中。

  5. 转到终端。键入 cd Appname ,然后 git add。 git commit -m部署,最后 git push

  6. 您的应用程序现在应该在 www完全运行.openshiftappname-domainname.rhcloud.com / warfilename

  1. Open OpenShift in your web browser. Log in to Openshift. Navigate to the application in question.
  2. Get the ssh code of that application (it should be right of screen), located to the right of the cartridges. Copy that code using Command-C or Ctrl-C.
  3. Open Terminal and type git clone ssh:\\xxxxxxxxxxxxxxxx...
  4. If you are on Mac, like I am, it should create a project directory at Users/Username/Appname. Inside that directory, delete the source folder and pom.xml. Take your compiled WAR file and copy it into the webapps directory .
  5. Go to Terminal. Type cd Appname, and then git add ., git commit -m "Deployment", and finally git push.
  6. Your application should now fully function at www.openshiftappname-domainname.rhcloud.com/warfilename



MySQL访问



MySQL access


  1. 为MySQL和phpMyAdmin安装墨盒。这应该通过添加Cartidge openshift.com 应用程序中心提供。

  2. 注意您的用户名密码到OpenShift自动为您生成的MySQL数据库。转到 www.openshiftappname-domainname.rhcloud.com/phpmyadmin ,输入身份验证凭据。

  3. 在phpMyAdmin内应该有服务器IP地址;它看起来像 127.x.y.z:3306 x,y和z 可以是一位数到三位数。

  4. 快速创建一个新的数据库以您想要的任何名称命名。我将其命名为 test ,然后在其中生成一个名为 testtable 的新表。

  5. 那么请记住您部署的WAR应用程序?好吧,如果你使用MySQL,我打赌你已经将它包含在你的应用程序中。建立与MySQL连接的基本步骤就是这样。

  1. Install the cartridges for MySQL and phpMyAdmin. This should be available through Add Cartidge at your openshift.com app hub.
  2. Note your username and password to the MySQL database that OpenShift automatically generates for you. Go to www.openshiftappname-domainname.rhcloud.com/phpmyadmin, enter the authentication credentials.
  3. Inside phpMyAdmin there should be the server IP address; it looks something like 127.x.y.z:3306. x, y, and z can be single digit to three digit numbers.
  4. Quickly create a new database named whatever you want. I am going to name it test and then consequently produce a new table in there named testtable.
  5. So remember your deployed WAR application? Well, if you are using MySQL, I bet you have already included it in your application. The basic steps to establish a connection to MySQL is as such.



Java代码



转到编译到WAR文件中的打开IDE项目。如果它是Maven项目,请转到项目中的pom.xml,并添加依赖项:

Java Code

Go the open IDE project that you compiled into your WAR file. Go to the pom.xml inside your project if it is a Maven project, and add the dependency:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.25</version>
 </dependency>




  1. 然后使用以下代码。

String s =jdbc:mysql://+ host +:+ port +/+ name,其中host是服务器IP地址,端口是 3306 ,名称是数据库名称,在我的情况下, test

String s = "jdbc:mysql://" + host + ":" + port + "/" + name", where host is the server IP address, the port is 3306 and name is the database name, in my case, test.

try {
    Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
     e.printStackTrace();
}

try {
    Connection con = DriverManager.getConnection(s, username, password);
}
catch (SQLException e) {
    e.printStackTrace();
}

现在 if(con == null)它不起作用。如果它不是 null ,它确实如此。

Now if (con == null) it did not work. And if it is not null, it did.

要进行测试,你应该重新编译你的WAR文件(在对它进行可视化测试之后)。如果你需要进一步的帮助,请发表评论。它应该在编译WAR文件时重新编译在第一部分中执行步骤 4-6 将已编译的webapp部署到OpenShift 。谢谢!

To test, you should recompile your WAR file (after putting some way to visually test it). If you need further help, please leave a comment. It should work when you compile WAR file and redo steps 4-6 in the first section: Deployment of compiled webapp to OpenShift. Thanks!

这篇关于OpenShift webapps mysql java连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆