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

标签 java mysql

我正在尝试使用工作台和connectorJ版本8.0.12连接到mySQL 但我就是无法连接,而且总是抛出异常。我在这里和谷歌中搜索了几个小时的答案,但似乎我做的一切都是正确的。

有人可以帮助我吗?

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

最佳答案

抱歉,您的意思是什么

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

}

关于java - 尝试使用 Java 连接到 mySQL 服务器(工作台)时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51578398/

相关文章:

java - 如果在 SAX 中设置 setNamespaceAware(true),如何获取 "xmlns:XXX"属性?

java - 警告 "A web service endpoint could not be found"是什么?

php - 在 mysql/phpmyadmin 中上传和显示图像

mysql - 解释计划在mysql中的含义

php - 如何在 Mysql 表中有一列单选按钮

java - 使用 javax.comm 的 Java 应用程序的可移植性

java - JFreeChart - 监听系列的更改

java - 远程处理案例中的惰性/急切加载策略 (JPA)

php - 确认用户帐号被删除不行

MYSQL查询连胜包括比分