线程“main”java.sql.sqlexception中的异常:未选择数据库

jum4pzuy  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(360)

这个问题在这里已经有答案了

java.sql.sqlexception:未选择数据库[重复](2个答案)
两年前关门了。
我需要在我的项目中运行sql脚本。我有一个sql脚本,它的内容在第一张图片上

我还必须创建一个数据源,如图2所示。

我有一个代码,在其中我尝试建立一个连接并运行我的sql脚本。

public class DBManager {
    private static DBManager instance;
    private static Connection connection;
    private static final Lock CONNECTION_LOCK = new ReentrantLock();

    private static final String url = "jdbc:mysql://localhost:3306/?useSSL=false";
    private static final String user = "root";
    private static final String pass = "root";

    private static final String SQL_INSERT_USER = "INSERT INTO users VALUES (DEFAULT ,?)";
    private static final String SQL_INSERT_GROUP = "INSERT INTO groups VALUES (DEFAULT ,?)";
    private static final String SQL_FIND_ALL_USERS = "SELECT * FROM users";
    private static final String SQL_FIND_ALL_GROUPS = "SELECT * FROM groups";
    private static final String SQL_FIND_USER_BY_LOGIN = "SELECT * FROM users WHERE login=?";
    private static final String SQL_FIND_GROUP_BY_NAME = "SELECT * FROM groups WHERE name=?";
    private static final String SQL_INSERT_USER_TO_GROUP = "INSERT INTO users_groups VALUES (?, ?)";
    private static final String SQL_FIND_GROUPS_BY_USER_ID = "SELECT g.id, g.name FROM users_groups ug\n" +
            "JOIN users u ON ug.user_id = u.id\n" +
            "JOIN groups g ON ug.group_id = g.id\n" +
            "WHERE u.id = ?";
    private static final String SQL_DELETE_GROUP = "DELETE FROM groups WHERE name=?";
    private static final String SQL_UPDATE_GROUP = "UPDATE groups SET name=? WHERE id=?";

    public static Connection getConnection() {
        return connection;
    }

    private DBManager(String url, String user, String pass) throws Exception {
        connection = DriverManager.getConnection(url, user, pass);

        executeScript(connection, new FileInputStream("sql/db-create.sql"));
    }

    public static synchronized DBManager getInstance()throws Exception {
        if (instance == null) {
            instance = new DBManager(url, user, pass);
        }
        return instance;
    }

以及运行项目的类。

public class Demo {
    //todo close all connections and scanners

    private static <T> void printList(List<T> list) {
        for (T element : list) {
            System.out.println(element);
        }
    }

    public static void main(String[] args) throws Exception {
        // users  ==> [ivanov]; groups ==> [teamA]

        DBManager dbManager = DBManager.getInstance();
     }
}

我有个例外。

Exception in thread "main" java.sql.SQLException: No database selected
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2482)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2440)
    at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:845)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:745)
    at database.DBManager.executeScript(DBManager.java:350)
    at database.DBManager.<init>(DBManager.java:48)
    at database.DBManager.getInstance(DBManager.java:53)
    at Demo.main(Demo.java:20)

我做的每件事都对吗?如何解决这个问题?

yizd12fk

yizd12fk1#

Exception in thread "main" java.sql.SQLException: No database selected

消息错误很明显,要解决问题,必须在url中指定数据库名称:

private static final String url = "jdbc:mysql://localhost:3306/db_name?useSSL=false";
                                                               ^^^^^^^

相关问题