No Database selected when retrieving from mysql website
我有一个mysql数据库,正试图从我们的网站主机(godaddy)中检索。 我遵循的格式似乎正确,但是它告诉我:
1 |
码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { // STEP 2: Register JDBC driver Class.forName(JDBC_DRIVER); // STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); // STEP 4: Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql; sql ="SELECT * FROM item_master"; ResultSet rs = stmt.executeQuery(sql); //<-- This is where the error is. // STEP 5: Extract data from result set while (rs.next()) { // Retrieve by column name int id = rs.getInt("id"); // Display values System.out.print("ID:" + id); } ... } |
我做了conn的print语句,以为连接可能为空,并显示为:
1 | com.mysql.jdbc.JDBC4Connection@2a6***** |
任何人都有什么想法会导致这样的事情?
您的数据库URL应包含您的数据库名称。 这通常是您的URL,后跟" / DBNAME"。
其中" mydb"是您的数据库名称。
DB_URL的值是什么? 据我所知,URL必须采用以下形式:
" WEBURL / DATABASENAME:PORT"
您是否只是在尝试不指定数据库就连接到WEBURL?
我有同样的问题。
只需再运行一个查询(在" SELECT"语句之前)即可连接到数据库。
第一:
1 2 3 |
之后:
1 2 3 | sql ="SELECT * FROM item_master"; rs = stmt.executeQuery(sql); |