This question already has answers here:
No Database selected when retrieving from mysql website
                                
                                    (3个答案)
                                
                        
                                9个月前关闭。
            
                    
我的Java桌面应用程序有问题。当前,当我提交一个SQL查询时,它抛出一个没有选择数据库的错误。我想我已经准备好了一切,所以非常感谢您多加关注。

我试图在我的SQL语句中的gorchawgwah数据库中的healthdataadd表中搜索数据。

public class DBUtil {
    //Declare JDBC Driver
    private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String username = "gorchawgwah";
    private static final String password = "root";

    //Connection
    private static Connection conn = null;

    //Connection String
    //String connStr = "jdbc:mysql://localhost/test";
    //Username=HR, Password=HR, IP=localhost, IP=1521, SID=xe
    private static final String connStr = "jdbc:mysql://localhost/?gorchawgwah=true&user=gorchawgwah&password=";


    //Connect to DB
    public static void dbConnect() throws SQLException, ClassNotFoundException {
        //Setting Oracle JDBC Driver
        try {
            Class.forName(JDBC_DRIVER);
        } catch (ClassNotFoundException e) {
            System.out.println("Where is your mySQL JDBC Driver?");
            e.printStackTrace();
            throw e;
        }

        System.out.println("mySQL JDBC Driver Registered!");

        //Establish the Oracle Connection using Connection String
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost/?gorchawgwah=true&user=gorchawgwah&password=");
        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console" + e);
            e.printStackTrace();
            throw e;
        }
    }

 //DB Execute Query Operation
    public static ResultSet dbExecuteQuery(String queryStmt) throws SQLException, ClassNotFoundException {
        //Declare statement, resultSet and CachedResultSet as null
        Statement stmt = null;
        ResultSet resultSet = null;
        CachedRowSetImpl crs = null;
        try {
            //Connect to DB (Establish Oracle Connection)
            dbConnect();
            System.out.println("Select statement: " + queryStmt + "\n");

            //Create statement
            stmt = conn.createStatement();

            //Execute select (query) operation
            resultSet = stmt.executeQuery(queryStmt);

            //CachedRowSet Implementation
            //In order to prevent "java.sql.SQLRecoverableException: Closed Connection: next" error
            //We are using CachedRowSet
            crs = new CachedRowSetImpl();
            crs.populate(resultSet);
        } catch (SQLException e) {
            System.out.println("Problem occurred at executeQuery operation : " + e);
            throw e;
        } finally {
            if (resultSet != null) {
                //Close resultSet
                resultSet.close();
            }
            if (stmt != null) {
                //Close Statement
                stmt.close();
            }
            //Close connection
            dbDisconnect();
        }
        //Return CachedRowSet
        return crs;
    }


    public static dataAdd searchHealthEntry (String healthID) throws SQLException, ClassNotFoundException {
        //Declare a SELECT statement
        String selectStmt = "SELECT * FROM health_dataadd WHERE healthID="+healthID;

        //Execute SELECT statement
        try {
            //Get ResultSet from dbExecuteQuery method
            ResultSet rsHealth = DBUtil.dbExecuteQuery(selectStmt);

            //Send ResultSet to the getEmployeeFromResultSet method and get employee object
            dataAdd DataAdd = getDataAddFromResultSet(rsHealth);

            //Return employee object
            return DataAdd;
        } catch (SQLException e) {
            System.out.println("While searching an employee with " + healthID + " id, an error occurred: " + e);
            //Return exception
            throw e;
        }
    }

最佳答案

您错过了jdbc URL中的数据库名称:

jdbc:mysql://localhost/?


应该

jdbc:mysql://localhost/YourDBName?

10-05 20:33
查看更多