java - "java.sql.SQLException: No database selected"与 MySQL JDBC

标签 java mysql jdbc

代码:

Connection dbConnection;
public Connection getDbConnection() throws ClassNotFoundException, SQLException{
String connectionString = "jdbc:mysql://127.2.0.1:3306/?user=root?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false";
;
String url="?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
    Class.forName("com.mysql.jdbc.Driver");

    dbConnection = DriverManager.getConnection(connectionString, dbUser, dbPass);
    return dbConnection;

}
public void SingUpUser(String id,String login, String password) {
    String insert = "INSERT INTO"+Constant.USER_TABLE+"("+Constant.USERS_ID+","+Constant.USERS_LOGIN+","+Constant.USER_PASSWORD+")"+
"VALUES(?,?,?)";


    try {
        PreparedStatement prSt = getDbConnection().prepareStatement(insert);
        prSt.setString(1, id);
        prSt.setString(2, login);
        prSt.setString(3, password);
        prSt.executeUpdate();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

错误:

java.sql.SQLException: No database selected
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
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.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:955)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1094)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1042)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1345)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1027)
at minelaunch.Databases.SingUpUser(Databases.java:36)
at minelaunch.minelauncherv1$5.actionPerformed(minelauncherv1.java:182)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我希望写入数据库,但收到此错误。

最佳答案

来自堆栈跟踪:

java.sql.SQLException: No database selected

您已从连接字符串中删除了数据库名称,因此现在 MySQL 不知道您要使用哪个数据库。再次编辑连接字符串并指定您要使用的数据库的实际名称。

关于java - "java.sql.SQLException: No database selected"与 MySQL JDBC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56405798/

相关文章:

java - LongClick 事件也会触发 Click 事件

Java 8 - 并行调用异步方法并合并它们的结果

java - 我想在背景图像上绘制可拖动的 JComponent

mysql - 为什么我不能将 varchar 的长度设置为 1000

php - PHP 和 MySQL 中的数学

java - jsp编程

java - Tomcat 8 中 DB2 的 Log4j jdbc appender

java - 检测单元类测试 - 无法在未调用 Looper.prepare() 的线程内创建处理程序

php - SUM 加倍结果的问题

java - 使用 JDBC 调用自定义类型输入参数的 PL/SQL 存储过程,所有字段均为 null