【问题标题】:Program runs without exception but shows no table names程序运行无异常但不显示表名
【发布时间】:2012-07-28 06:58:44
【问题描述】:

我在 mysql 中有一些数据库,它们都包含一些带有几列的表。我从堆栈溢出答案中得到了下面的代码。 答案在: How can I detect a SQL table's existence in Java?

代码给出了输出-

Driver Loaded.
Got Connection.

代码-

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {
  public static void main(String[] args) throws Exception {
DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
  System.out.println(rs.getString(3));
}  }

  static Connection conn;

  static Statement st;

  static {
try {
  // Step 1: Load the JDBC driver.
  System.out.println("Driver Loaded.");
  // Step 2: Establish the connection to the database.
  String url = "jdbc:mysql://localhost:3306/";

  conn = DriverManager.getConnection(url, "cowboy", "123456");
  System.out.println("Got Connection.");

  st = conn.createStatement();
} catch (Exception e) {
  System.err.println("Got an exception! ");
  e.printStackTrace();
  System.exit(0);
}
  }
}

【问题讨论】:

    标签: java mysql jdbc


    【解决方案1】:

    在你的代码中只有你

    System.out.println("Driver Loaded.");
    

    这还不够。 您必须先加载驱动程序!

     Class.forName("com.mysql.jdbc.Driver");
     System.out.println("Driver Loaded.");
    

    我很惊讶这有效

    conn = DriverManager.getConnection(url, "cowboy", "123456");
    

    你来到这行代码。

    System.out.println("Got Connection.");
    

    使用下面的代码,它会去,但你不会得到一个表格列表

    static {
        try {
          // Step 1: Load the JDBC driver.
          Class.forName("com.mysql.jdbc.Driver");
          System.out.println("Driver Loaded.");
          // Step 2: Establish the connection to the database.
          String url = "jdbc:mysql://localhost";
    
          conn = DriverManager.getConnection(url,"user","passw");
          System.out.println("Got Connection.");
          ....
          }
    }
    

    正确设置数据库名称

    static {
           try {
           // Step 1: Load the JDBC driver.
           Class.forName("com.mysql.jdbc.Driver");
           System.out.println("Driver Loaded.");
           // Step 2: Establish the connection to the database.
           String url = "jdbc:mysql://localhost/myDataBase";
           conn = DriverManager.getConnection(url,"user","passw");
           System.out.println("Got Connection.");
           ....
           }
    }
    

    您可以看到您的 myDataBase 表的列表。

    【讨论】:

    • 我认为不需要 Class.forName("com.mysql.jdbc.Driver");没有它,我的其他代码也能正常工作。我试着把这条线。仍然没有成功。现在呢?
    • 字符串 url = "jdbc:mysql://localhost/myDataBase";将 myDataBase 替换为您的数据库名称
    • 你的问题是什么意思?程序运行无异常但没有显示表名?
    【解决方案2】:

    此代码用于显示特定数据库的表,而不是所有数据库的所有表。您没有在 url 字符串中指定任何数据库,因此没有可显示的内容。

    如果您仔细查看用于回答链接问题的link,可以看到String url = "jdbc:hsqldb:data/tutorial";,因此您必须先连接到数据库。

    PS:如果您使用 jdbc4 之前的驱动程序,您可能需要加载驱动程序,请使用:Class.forName("com.mysql.jdbc.Driver"); 并确保驱动程序在类路径中可用。

    【讨论】:

    • 我从不在我的代码中使用 Class.forName("xxxx")。我总是将驱动程序 jar 文件添加到构建路径并且我的代码有效。这是一个好习惯吗?
    • @sweetdreams 如果您使用 jdbc4 之前的驱动程序,您必须:Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.) (docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html)
    • @sweetdreams 顺便说一下,您的主要问题是您必须在 url 中指定要连接的数据库。
    • 我不确定这意味着什么。我认为不需要指定数据库名称。
    • @sweetdreams 当然你有,你怎么能肯定你没有呢? 1)您提供的链接是关于How can I detect a SQL table's existence in Java?,因此这意味着您已经选择了一个数据库(数据库由表组成)。 2)您显然复制/粘贴此示例:java2s.com/Code/JavaAPI/java.sql/…,在示例中,连接位于名为“tutorial”的表上。甚至评论说// Step 2: Establish the connection to the database. 但你没有在你的代码中指定任何...
    猜你喜欢
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多