线程“main”java.sql.SQLException中的异常:未选择数据库[重复]

Posted

技术标签:

【中文标题】线程“main”java.sql.SQLException中的异常:未选择数据库[重复]【英文标题】:Exception in thread "main" java.sql.SQLException: No database selected [duplicate] 【发布时间】:2018-06-09 10:55:39 【问题描述】:

我需要在我的项目中运行 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)

我做的每一件事都正确吗?以及如何解决这个问题?

【问题讨论】:

【参考方案1】:
Exception in thread "main" java.sql.SQLException: No database selected

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

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

【讨论】:

我已经尝试过了。我得到以下异常。线程“主”com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException 中的异常:未知数据库“人” 这是另一个问题@VIRICH 在这种情况下您似乎没有人员数据库,您必须手动创建它,或者您可以按照本教程通过java kode-blog.com/jdbc-create-database-dynamically-create-databases创建它跨度>

以上是关于线程“main”java.sql.SQLException中的异常:未选择数据库[重复]的主要内容,如果未能解决你的问题,请参考以下文章

多线程

为啥 Main 函数上的 Sleep() 会停止所有线程?

android中怎样让main线程和子线程同步一个变量

pthread_exit在main线程中的用处

RunLoop在main线程和自己创建的线程如何启动

DispatchQueue.main.async 阻塞主线程