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

ca1c2owp  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(483)

我试图连接到mysql使用工作台和connectorj版本8.0.12,但我就是无法连接,它总是抛出异常,我在这里和谷歌搜索了几个小时的答案,但似乎我做的一切都正确。
有人能帮我吗?
1.当我在服务器中手动创建模式,然后输入“createschema”命令时,我得到的所有错误都是这样的
2.但是当我试图从淀粉中创建模式时,这就是我得到的错误
我的代码在这里
我的代码:

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();
        }
    }
}

手动创建架构时出错:

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

尝试从淀粉创建模式时遇到的错误:

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
but5z9lq

but5z9lq1#

对不起,你刚才说什么

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

而你创建的数据库是“成人”?我认为你应该先关闭第一个连接,然后再打开第二个。
--编辑——这对我来说很好

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;
    }

}

相关问题