有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试使用workbench和connectorJ版本8.0.12连接到mySQL 但我就是无法连接,它总是抛出例外,我在这里和谷歌搜索了几个小时的答案,但似乎我做的每件事都是正确的

有人能帮我吗

一,When I manually create the schema in the server and then put the command "create schema" this is all the errors that I get
2.But when I trying to create schema from starch this is the errors that I get
3.My code is here

我的代码:

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

共 (1) 个答案

  1. # 1 楼答案

    对不起,但你的意思是什么

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