由于底层异常,无法加载连接类:'java.lang.NumberFormatException:对于输入字符串:“OPENSHIFT_MYSQL_DB_PORT”'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30234193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:44:00  来源:igfitidea点击:

Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "OPENSHIFT_MYSQL_DB_PORT"'

javamysqljerseyopenshift

提问by MrPencil

I am trying to deploy my Jersey project on openshift. I have implemented this apple class to test the error in the another class since I guess the problem is with the establishing the database connection. in the Tails log I found this error:

我正在尝试在 openshift 上部署我的 Jersey 项目。我已经实现了这个苹果类来测试另一个类中的错误,因为我猜问题出在建立数据库连接上。在 Tails 日志中,我发现了这个错误:

Connecting to databasea| com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "OPENSHIFT_MYSQL_DB_PORT"'. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

连接到数据库a| com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法加载连接类,因为底层异常:'java.lang.NumberFormatException:对于输入字符串:“OPENSHIFT_MYSQL_DB_PORT”'。在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

package org.busTracker.serverSide;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("myresource")
public class Apple {

   //I modified my credients.
    String host = "jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:OPENSHIFT_MYSQL_DB_PORT/serverside";
    String user = "adminBjv5a4k";
    String password = "7tvPb1Bx3v8j";

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {

        Connection conn = null;  
        try {    
          Class.forName("com.mysql.jdbc.Driver");    
          System.out.println("Connecting to database…");    
          conn = DriverManager.getConnection(host,user,password);    
        } catch (Exception e) {    
          e.printStackTrace();    
        } finally {    
          if (conn != null) {    
            try {    
              conn.close();    
            } catch (SQLException e) {    
              // ignore    
            }    
          }    
        }   

        return "Hello, from apple class   14.05.15 11:35!";
    }

}

Edit:I added the following to the try block after DriverManager.getConnection():

编辑:我在 DriverManager.getConnection() 之后的 try 块中添加了以下内容:

  Map<String, String> env = System.getenv();
  for (String envName : env.keySet()) {
      System.out.format("%s=%s%n",
                        envName,
                        env.get(envName));
  }

I have tried the following but I am still getting the same error:

我尝试了以下操作,但仍然遇到相同的错误:

  • 此解决方案:https://forums.openshift.com/mysql-51-jboss-app-numberformatexception-mysql-url 并添加以下 jdbc:mysql://${env.OPENSHIFT_MYSQL_DB_HOST}:${env.OPENSHIFT_MYSQL_DB_PORT}/ serv??erside 但没有任何改变。
  • "jdbc:mysql://127.10.310.130:3306 /serverside"; 此值来自应用程序的 phpmyadmin。

回答by kucing_terbang

the problem is because of this line

问题是因为这条线

String host = "jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:OPENSHIFT_MYSQL_DB_PORT/serverside";

to get the environment variable, you need to use the method System.getEnv().get("[the variable name]"). So, in your case, the host variable should looks like this

要获取环境变量,您需要使用 方法System.getEnv().get("[the variable name]")。所以,在你的情况下,宿主变量应该是这样的

String host = "jdbc:mysql://" 
              + System.getenv().get("OPENSHIFT_MYSQL_DB_HOST") 
              + ":" 
              + System.getenv().get("OPENSHIFT_MYSQL_DB_PORT") 
              + "/serverside";

by the way, your edit does not work because the application already throws an exception before it execute the code. so, to make it work, you need to put it beforethe DriverManager.getConnection()function.

顺便说一下,您的编辑不起作用,因为应用程序在执行代码之前已经抛出异常。所以,为了让它工作,你需要把它before放在DriverManager.getConnection()函数里。

回答by user1419261

You could replace those variables as below .

您可以按如下方式替换这些变量。

       <property name="url"                                                                                                              
      value="jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/
       yourdatabasename" />

       can be replaced as below.

     <property name="url"  
    value="jdbc:mysql://127.12.97.2:3306/yourdatabasename"
    />

jdbc:mysql://127.12.97.2:3306/yourdatabasename"The IP address and the portnumber can be obtained from the openshift phpmyadmin page and they are usually displayed on top of the admin page.

jdbc:mysql://127.12.97.2:3306/yourdatabasename"IP地址和端口号可以从openshift phpmyadmin页面获取,它们通常显示在管理页面的顶部。

回答by Sahil Patel

I tried all the above solutions but it didn't solve my problem. So anyone still getting that exception, try this. The value stored in environment variable $OPENSHIFT_MYSQL_DB_HOSTis something like

我尝试了上述所有解决方案,但没有解决我的问题。所以任何人仍然得到那个例外,试试这个。存储在环境变量$OPENSHIFT_MYSQL_DB_HOST 中的值类似于

'jdbc:mysql://adminuname:[email protected]:3306/appname'

'jdbc:mysql://adminuname:[email protected]:3306/appname'

. Therefore, using that value to create the url for DriverManager.getConnection() gives us the exception. Instead try to know the value stored in $OPENSHIFT_MYSQL_DB_HOSTand then hard code the url into a String variable without that username and password. Something like this

. 因此,使用该值为 DriverManager.getConnection() 创建 url 会给我们带来异常。而是尝试知道存储在$OPENSHIFT_MYSQL_DB_HOST 中的值,然后将 url 硬编码到没有该用户名和密码的 String 变量中。像这样的东西

'jdbc:mysql://127.02.0.1:3306/appname'

'jdbc:mysql://127.02.0.1:3306/appname'

For me it started working after making this modification. All other environment variables can be used as it is.

对我来说,它在进行此修改后开始工作。所有其他环境变量都可以原样使用。

I found this solution here.

我在这里找到了这个解决方案。