简体   繁体   English

从Selenium Java测试脚本到MySQL数据库的连接失败

[英]Connection from Selenium Java test script to MySQL database failing

I'm trying to connect to a MySQL database using the program below. 我正在尝试使用下面的程序连接到MySQL数据库。 Depending on how I present the connection string I get the following errors. 根据我显示连接字符串的方式,出现以下错误。

IP address separated from 'mysql' by ':' only: conn = DriverManager.getConnection("jdbc:mysql:[valid ip address]/localhost:3306/[valid database name]","[valid username]","[valid password]"); IP地址仅以':'与'mysql'分开:conn = DriverManager.getConnection(“ jdbc:mysql:[有效IP地址] / localhost:3306 / [有效数据库名称]”,“ [有效用户名]”,“ [有效的密码]“);

SQLState: 08001 VendorError: 0 java.sql.SQLException: No suitable driver found for jdbc:mysql:[valid ip address]/localhost:3306/[valid database name] SQLState:08001 VendorError:0 java.sql.SQLException:找不到适用于jdbc:mysql:[有效IP地址] / localhost:3306 / [有效数据库名称]的驱动程序

IP address separated from 'mysql' by ':http://': conn = DriverManager.getConnection("jdbc:mysql: http://[valid ip address]/localhost:3306/[valid database name]","[valid username]","[valid password]"); IP地址与“ mysql”之间用“:http://”隔开:conn = DriverManager.getConnection(“ jdbc:mysql: http:// [有效 IP地址] / localhost:3306 / [有效数据库名称]”,“ [有效的用户名]“,” [有效的密码]“));

SQLState: 08001 VendorError: 0 java.sql.SQLException: No suitable driver found for jdbc:mysql: http://[valid ip address]/localhost:3306/[valid database name] SQLState:08001 VendorError:0 java.sql.SQLException:找不到适用于jdbc:mysql的驱动程序: http:// [有效的 IP地址] / localhost:3306 / [有效的数据库名称]

IP address separated from 'mysql' by '://': conn = DriverManager.getConnection("jdbc:mysql://[valid ip address]/localhost:3306/[valid database name]","[valid username]","[valid password]"); IP地址与“ mysql”之间用“://”分隔:conn = DriverManager.getConnection(“ jdbc:mysql:// [有效IP地址] / localhost:3306 / [有效数据库名称]”,“ [有效用户名]” ,“ [有效密码]”);

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败最近一次成功发送到服务器的数据包是0毫秒前。 The driver has not received any packets from the server. 驱动程序尚未收到来自服务器的任何数据包。 SQLState: 08S01 VendorError: 0 SQLState:08S01 VendorError:0

In the latter instance a stacktrace provides 'Caused by: java.net.ConnectException: Connection refused: connect' 在后一种情况下,堆栈跟踪提供了“由以下原因引起:java.net.ConnectException:连接被拒绝:connect”

In all cases the stacktrace also indicates that the line of code setting the variable 'conn' is throwing the exception. 在所有情况下,stacktrace都表明设置变量“ conn”的代码行引发了异常。

Note that in the program listing text within square brackets represents obfuscated code and that the brackets are not actually in the program. 请注意,在程序列表中,方括号内的文本表示混淆的代码,并且括号实际上不在程序中。

My question is, do I actually have the required drivers installed or not, or is there a problem with my connection string? 我的问题是,我实际上是否安装了必需的驱动程序,或者我的连接字符串有问题吗? Could there be some other issue that I haven't thought of? 可能还有其他我没想到的问题吗?

Code follows 代码如下

package [package name];

import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQLConnectionTest
{

    @Test
    public void startWebDriver() 
    {
        try 
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } 
        catch (Exception ex) 
        {
            System.out.println(ex.getMessage());
        }

        Statement stmt = null;
        Connection conn = null;
        ResultSet rs = null;

        try 
        {
            conn = DriverManager.getConnection("jdbc:mysql://[valid ip address]/localhost:3306/[valid database name]","[valid username]","[valid password]");

            // Do something with the Connection

        } 
        catch (SQLException ex) 
        {
            // handle any errors
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
            ex.printStackTrace();
        }

        try 
        {
            conn.close();
        } 
        catch (SQLException e) 
        {
            System.out.println(e.getMessage());
        }
    }
}

Some information on the environment: 有关环境的一些信息:

IDE: Eclipse Luna Service Release 2 (4.4.2), Build id: 20150219-0600 IDE:Eclipse Luna Service Release 2(4.4.2),内部版本号:20150219-0600

jre System Library: jre1.8.0_45 jre系统库:jre1.8.0_45

MySQLConnector library: mysql-connector-java-5.1.36.jar (appears in Referenced Libraries section of Package Explorer) MySQLConnector库:mysql-connector-java-5.1.36.jar(显示在“程序包资源管理器”的“引用的库”部分)

I've also installed mysql-connector-java-gpl-5.1.36, jdk-8u45-windows-x64 and selenium-java-2.45.0 我还安装了mysql-connector-java-gpl-5.1.36,jdk-8u45-windows-x64和selenium-java-2.45.0

I'll be happy to provide any further information as required. 我很乐意根据需要提供任何进一步的信息。

The connection process seems OK to me, however there's something strange in the connection URL. 连接过程在我看来似乎还可以,但是连接URL中有些奇怪的地方。 You have 你有

conn = DriverManager.getConnection("jdbc:mysql://[valid ip address]/localhost:3306/[valid database name]","[valid username]","[valid password]");

which has IP address and localhost:port. 具有IP地址和localhost:port。 This is not a valid URL, yo have to remove either the localhost or the IP address: 这不是有效的网址,您必须删除本地主机或IP地址:

conn = DriverManager.getConnection("jdbc:mysql://[valid ip address]:3306/[valid database name]","[valid username]","[valid password]");

or 要么

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/[valid database name]","[valid username]","[valid password]");

That should do the trick. 这应该够了吧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM