2014-01-13 72 views
0

我已经安装了WAMP的mysql服务器并创建了名为getataxi的数据库。当我去phpMyAdmin我可以看到数据库及其表格。我也通过JDBC连接到这个数据库。当我创建一个连接到数据库使用:JDBC表错误:未知数据库

conn = DriverManager.getConnection(DB_URL,USER,PASS);

我得到这个错误

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知数据库 'getataxi' 在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util。 H andleNewInstance(Util.java:411)

可能是什么问题?

注:

  1. 我WAMP的服务器联机。
  2. 我试图连接到软件附带的测试数据库,它似乎可以找到测试数据库,因为我得到了不同的错误消息,说数据库中的表未找到,它的真实性。
  3. 我试着建立大写字母和小写getataxi,但没有帮助。
  4. 我有一个* .sql文件,其中包含我导入的表格。
  5. DB_URL是jdbc:mysql://localhost/getataxi我也尝试jdbc:mysql://localhost:3306/getataxi
  6. MySQL的用户是根
  7. 我使用Windows
+0

post DB_URL和USR – venergiac

+0

您可以通过网络访问getataxi吗?如果没有,端口是否打开并且mysql正在监听它? – hd1

回答

2

以下简化的情况下工作正常,我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 
+0

即时通讯很抱歉打扰所有与这个问题,事实证明,我忘记了一个月前羡慕安装MySQL服务器之前,作为一种服务和作为服务运行,所以当我连接到数据库它连接到MySQL,而不是现在我stoped mysql服务和它的工作,再次如此抱歉 –

0

的MySQL数据库和表对应的文件。所以,如果你在Linux下,他们是区分大小写(但不在Windows下)。

-1

正如你说你有WAMP服务器了:

保存下面的代码为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>"; 
?> 

你应该能够看到在浏览器中连接到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(); 
     } 
    } 
} 

你看到成功连接没有任何错误。那么你的代码中有一些问题。请妥善检查您的代码。

+0

@downvoter我可以知道downvote ..的共振? – Lucky

相关问题