无法在Derby数据库中找到表

时间:2018-11-25 07:33:25

标签: java derby netbeans-8

我不知道为什么以下程序无法在我的derby数据库中找到表:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NdConnect
{
  public final static String SETUP_FILE_PATH = "/AppData/Local/NewdawnTest"; 
  private static final String CONNECTION_URL = "jdbc:derby:" + "C:/Users/" + System.getenv("USERNAME") + SETUP_FILE_PATH + "db" + ";create=true";
  private static Connection conn = null;
  private final static Properties dbProperties  = new Properties();
  private static PreparedStatement pstmtSelectTxns;   

  public static void connect() {
    try {  
      conn = DriverManager.getConnection(CONNECTION_URL  + ";create=true", dbProperties);
      pstmtSelectTxns = conn.prepareStatement("SELECT * from TXNS");
      System.out.println("Connected OK to " + CONNECTION_URL);   
    }
    catch (SQLException sqle) {
      System.out.println("SQL exception");
      System.out.println("Connected NOK:The connection URL is " + CONNECTION_URL);
      Logger.getLogger(NdConnect.class.getName()).log(Level.SEVERE, null, sqle);
    } 
  }

  public static void disconnect() 
  {
    try {
      if (conn != null) {
        conn.close();
        conn = null;
        System.out.println("0048 NDC OK:DB closed ");  
      }
    } catch (SQLException sqle) {
      Logger.getLogger(NdConnect.class.getName()).log(Level.SEVERE, null, sqle);
    }
  }

 public static void main(String[] args)
 {
//   NdConnect nd = new NdConnect();  
   NdConnect.connect();
   NdConnect.disconnect();
//    System.out.println("NdConnect finished");
  }
}

程序抛出SQLSyntaxErrorException:

run:
SQL exception
Connected NOK:The connection URL is jdbc:derby:C:/Users/Administrator/AppData/Local/NewdawnTestdb;create=true
Nov 25, 2018 6:58:42 AM NdConnect connect
0048 NDC OK:DB closed 
SEVERE: null
java.sql.SQLSyntaxErrorException: Table/View 'TXNS' does not exist.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement42.<init>(Unknown Source)
    at org.apache.derby.jdbc.Driver42.newEmbedPreparedStatement(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
    at NdConnect.connect(NdConnect.java:21)
    at NdConnect.main(NdConnect.java:47)
Caused by: ERROR 42X05: Table/View 'TXNS' does not exist.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.sql.compile.FromBaseTable.bindTableDescriptor(Unknown Source)
    at org.apache.derby.impl.sql.compile.FromBaseTable.bindNonVTITables(Unknown Source)
    at org.apache.derby.impl.sql.compile.FromList.bindTables(Unknown Source)
    at org.apache.derby.impl.sql.compile.SelectNode.bindNonVTITables(Unknown Source)
    at org.apache.derby.impl.sql.compile.DMLStatementNode.bindTables(Unknown Source)
    at org.apache.derby.impl.sql.compile.DMLStatementNode.bind(Unknown Source)
    at org.apache.derby.impl.sql.compile.CursorNode.bindStatement(Unknown Source)
    at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
    at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
    at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
    ... 7 more

但是数据库和表确实存在。我已经通过在Netbeans>服务中创建与数据库的连接来验证了这一点。当我在服务中运行此命令时:

SELECT * from TXNS

所需表正确显示,输出窗口返回:

Executed successfully in 0 s.
Fetching resultset took 0.016 s.
Line 1, column 1

Execution finished after 0.254 s, no errors occurred.

通过Netbeans服务查看此连接的属性将显示以下属性值:

Display name    NewDawn – TEST DB
Database URL    jdbc:derby:C:\Users\Administrator\AppData\Local\NewdawnTest\db
Driver          apache_derby_embedded
Driver class    org.apache.derby.jdbc.EmbeddedDriver

这似乎与我在Java代码示例中的设置相对应,所以我不明白为什么代码不起作用。

1 个答案:

答案 0 :(得分:2)

URL不同:

在Netbeans中:jdbc:derby:C:\Users\Administrator\AppData\Local\NewdawnTest\db

在Java中:jdbc:derby:C:/Users/Administrator/AppData/Local/NewdawnTestdb;create=true

CONNECTION_URL行更改为:

private static final String CONNECTION_URL = "jdbc:derby:" + "C:/Users/" + System.getenv("USERNAME") + SETUP_FILE_PATH + "/db"