线程" main"中的例外情况java.sql.SQLException:未选择任何数据库

时间:2018-06-09 10:55:39

标签: java mysql jdbc

我需要在项目中运行sql脚本。我有一个sql脚本,其内容在第一张图片enter image description here 我还必须创建一个数据源,如图2所示。enter image description here我有一个代码,我尝试建立连接并运行我的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)

我做对了吗?以及如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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

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

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