简体   繁体   English

尝试使用 Java 连接到 mySQL 服务器(工作台)时出错

[英]error while trying to connect to mySQL server (workbench) with Java

I am trying to connect to mySQL using workbench and connectorJ ver 8.0.12 but I just can't connect and its always throws exceptions I searched for an answer for hours here and in google too but it seems that I do everything correctly.我正在尝试使用工作台和 connectorJ 版本 8.0.12 连接到 mySQL,但我无法连接并且它总是抛出异常我在这里和谷歌搜索了几个小时的答案,但似乎我做的一切都是正确的。

Can anyone help me?谁能帮我?

1. When I manually create the schema in the server and then put the command "create schema" this is all the errors that I get 1.当我在服务器中手动创建架构然后输入命令“创建架构”时,这是我得到的所有错误
2. But when I trying to create schema from starch this is the errors that I get 2.但是当我尝试从淀粉创建模式时,这是我得到的错误
3. My code is here 3.我的代码在这里

my code :我的代码:

public class MySQLClient {
private Connection connect = null;
private Statement stmt = null;
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/";
static final String USER = "root";
static final String PASS = "Pass123456";
static final String DataBaseName = "adults";


public void createSchema() {
    try {

        Class.forName(JDBC_DRIVER).newInstance();
        connect = DriverManager.getConnection(DB_URL, USER, PASS);
        System.out.println("Creating database...");
        stmt = connect.createStatement();
        String sql = "CREATE DATABASE " + DataBaseName;

        stmt.executeUpdate(sql);
        System.out.println("Database created successfully...");

        connect = DriverManager.getConnection(DB_URL + DataBaseName + "?useSSL=false", USER, PASS);
        stmt = connect.createStatement();
        createTables();
    } catch (SQLException se) {
        se.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException se2) {
        }// nothing we can do
        try {
            if (connect != null)
                connect.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }
}

errors when i created manually the schema :我手动创建架构时出现错误:

connected to database...
Creating database...
Database created successfully...
Connection to Database Failed
java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near '=false'.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:79)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:131)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:227)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at MySQLClient.createSchema(MySQLClient.java:37)
    at MySQLCommandLineClient.main(MySQLCommandLineClient.java:20)
Caused by: com.mysql.cj.exceptions.UnableToConnectException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near '=false'.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    ... 5 more
Caused by: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near '=false'.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.conf.ConnectionUrlParser.processKeyValuePattern(ConnectionUrlParser.java:547)
    at com.mysql.cj.conf.ConnectionUrlParser.parseQuerySection(ConnectionUrlParser.java:519)
    at com.mysql.cj.conf.ConnectionUrlParser.getProperties(ConnectionUrlParser.java:644)
    at com.mysql.cj.conf.ConnectionUrl.collectProperties(ConnectionUrl.java:304)
    at com.mysql.cj.conf.ConnectionUrl.<init>(ConnectionUrl.java:289)
    at com.mysql.cj.conf.url.SingleConnectionUrl.<init>(SingleConnectionUrl.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.cj.util.Util.handleNewInstance(Util.java:210)
    at com.mysql.cj.util.Util.getInstance(Util.java:185)
    at com.mysql.cj.util.Util.getInstance(Util.java:192)
    at com.mysql.cj.conf.ConnectionUrl.getConnectionUrlInstance(ConnectionUrl.java:201)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:204)
    ... 4 more

erros that i get when trying create schema from starch :尝试从淀粉创建模式时遇到的错误:

create schema
Sun Jul 29 11:01:18 IDT 2018 WARN: Establishing SSL connection without      server's identity verification is not recommended. According to MySQL 5.5.45+,  5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if  explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Connection to Database Failed
java.sql.SQLSyntaxErrorException: Unknown database 'adults'
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:832)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at MySQLClient.createSchema(MySQLClient.java:23)
    at MySQLCommandLineClient.main(MySQLCommandLineClient.java:20)
Please enter one of the possible commands

Sorry but what did you mean in对不起,你是什么意思

connect = DriverManager.getConnection(DB_URL + "nedy" + "?useSSL=false", USER, PASS);

while your created database is "adults"?而您创建的数据库是“成人”? And I think you should close the first connection before open the second.而且我认为您应该在打开第二个连接之前关闭第一个连接。

-- EDIT -- This worked fine for me -- 编辑 -- 这对我来说很好用

public class DatabaseConnector {

    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/";

    private static final String USERNAME = "root";
    private static final String PASSWORD = "password";

    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

    public static void main(String[] args) throws SQLException {
        createDatabase();
    }

    public static void createDatabase() throws SQLException {
        Connection conn = connect("");
        Statement st = conn.createStatement();
        st.executeUpdate("CREATE DATABASE test");
        //Close conn, st 
        conn = connect("test");
        st = conn.createStatement();
        st.executeUpdate("CREATE TABLE abc (id int)");
    }

    public static final Connection connect(String database) {
        Connection connection = null;
        try {
            Class.forName(JDBC_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            connection = DriverManager.getConnection(JDBC_URL + database, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

}

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

相关问题 连接到数据库mysql工作台时发生错误:java.sql.SQLNonTransientConnectionException:无法创建与数据库服务器的连接 - Error occur while connecting to database mysql workbench:java.sql.SQLNonTransientConnectionException: Could not create connection to database server 我试图使用休眠连接到MySql工作台 - Im trying to connect to MySql workbench using hibernate 尝试连接到MySQL数据库时Java中的NullPointerException - NullPointerException in java while trying to connect to MySQL database 试图将java程序连接到mySQL服务器 - Trying to connect java program to mySQL server 将我的应用程序连接到在线MySQL服务器(工作台) - Connect my app to online MySQL server (workbench) 尝试将 Java 与数据库连接时出错 - Error while trying to connect Java with database py4j.protocol.Py4JNetworkError:尝试连接到Java服务器时发生错误 - py4j.protocol.Py4JNetworkError : An error occurred while trying to connect to the Java server 无法从本地计算机连接到在服务器上运行的mysql docker(错误10061)-Workbench - Cannot connect from local machine to mysql docker running on server (error 10061) - Workbench 将 mysql 服务器连接到 android 应用程序 (java) 错误 - connect mysql server to an android app (java) error 无法从MySQL Workbench连接到dockerized MySQL服务器 - Cannot connect from MySQL Workbench to dockerized MySQL server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM