iam尝试使用工作台和连接器J ver 8.0.12连接到mySQL
但我只是无法连接,它总是抛出异常,我在这里和Google中都在搜索了数小时的答案,但看来我做得很好
谁能帮我 ?
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
我的代码:
ublic 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
答案 0 :(得分:0)
对不起,你的意思是
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;
}
}