[英]JDBC table error : Unknown database
我已經安裝了WAMP的mysql服務器,並創建了名為getataxi
的數據庫。 當我轉到phpMyAdmin
我可以看到數據庫及其表。 我還通過JDBC連接到該數據庫。 當我使用創建與數據庫的連接時:
conn = DriverManager.getConnection(DB_URL,USER,PASS);
我收到此錯誤 :
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)處的未知數據庫'getataxi',sun.reflect.NativeConstructorAccessorImpl.newInstance(sun.reflect.DelegatingConstructorAccessorImpl。 java.lang.reflect.Constructor.newInstance上的未知源(com.mysql.jdbc.Util.handleNewInstance(Util.java:411)處的未知源)
可能是什么問題呢 ?
注意 :
jdbc:mysql://localhost/getataxi
我也嘗試過jdbc:mysql://localhost:3306/getataxi
在WAMP( USBWebserver )下,以下簡化的情況對我來說很好用,成功列出了名為getataxi
的數據庫中的所有表:
import java.sql.*;
public class mysqlTestMain {
public static void main(String[] args) {
Connection conn = null;
try {
String myConnectionString =
"jdbc:mysql://localhost:3306/getataxi?" +
"useUnicode=yes&characterEncoding=UTF-8";
conn = DriverManager.getConnection(myConnectionString, "root", "beer");
Statement stmt = conn.createStatement();
stmt.execute("SHOW TABLES");
ResultSet rs = stmt.getResultSet();
while (rs.next()) {
System.out.println(rs.getString(1));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
如果相同的代碼對您不起作用,請嘗試以下代碼列出服務器上的所有(可見)數據庫:
import java.sql.*;
public class mysqlTestMain {
public static void main(String[] args) {
Connection conn = null;
try {
String myConnectionString =
"jdbc:mysql://localhost:3306?" +
"useUnicode=yes&characterEncoding=UTF-8";
conn = DriverManager.getConnection(myConnectionString, "root", "beer");
Statement stmt = conn.createStatement();
stmt.execute("SHOW DATABASES");
ResultSet rs = stmt.getResultSet();
while (rs.next()) {
System.out.println(rs.getString(1));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
當我運行它時,我得到這樣的列表:
information_schema
getataxi
mydb
mysql
performance_schema
test
MySQL表和數據庫對應於文件。 因此,如果您使用的是Linux,則它們區分大小寫 (但不適用於Windows)。
如您所說,您已經啟動了服務器:
將以下代碼另存為php文件,並將其放在您的www文件夾中,然后嘗試訪問該頁面
<?php
$username = "root";
$password = "your_password";
$hostname = "localhost:3306"; <==change port if different1
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
您應該能夠在瀏覽器中看到Connected to MySQL消息,以確認主機名,用戶名和密碼組合。
在Java代碼中,
import java.sql.*;
public class Java2MySql
{
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/";
String dbName = "demo”
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "mypasswd";
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
conn.close();
System.out.println("Successfully connected");
} catch (Exception e) {
e.printStackTrace();
}
}
}
您會看到成功連接,沒有任何錯誤。 然后,您的代碼中會有一些問題。 請正確檢查您的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.