【问题标题】:Schema 'TEST' does not exist架构“测试”不存在
【发布时间】:2015-12-12 09:38:52
【问题描述】:

我不熟悉使用 DB 连接创建 Java 程序。我试图让程序创建一个表,读取一个表,以便我可以运行查询并显示某些数据。据我所知,我的程序已成功连接到数据库,但收到错误消息:

Syntax error: Encountered ")" at line 8, column 1.
Schema 'TEST' does not exist
Schema 'TEST' does not exist
Schema 'TEST' does not exist
Schema 'TEST' does not exist

其他错误,我习惯于接收 # 行,这样我至少知道从哪里开始查找。使用一行和一列 #,我不确定,我查看了其他帖子并尝试进行更新,例如将 APP 设为默认架构。关于从哪里开始寻找的正确方向的有益推动。一旦我弄清楚如何克服这个问题并打印查询,我知道我会很高兴的。感谢您提供的任何帮助。

import static java.lang.System.out;
import java.sql.*;
import java.sql.SQLException;

public class AnimalDB1 {

private static final String url = "jdbc:derby://localhost:1527/AnimalDB;create=true;user=test;password=test";
private static final String tableName = "Animal";
private static Connection conn = null;
private static int nextId = 1;
private boolean tablesCreated = false;

private static void createConnection(){
try{
    System.out.println("Connecting to Database...");
    conn = DriverManager.getConnection(url);
    System.out.println("Database Connection Successful\n");
}
catch (SQLException e){}

}

// Increments the ID number for each animal
private void incId(){
    AnimalDB1.nextId++;
}

private void animalTable() throws SQLException{
    Statement statement = null;

    try{
    StringBuilder sb = new StringBuilder("");
        sb.append("CREATE table Animal (\n");
            sb.append("ID INTEGER NOT NULL,\n");
            sb.append("AnimalName varchar(15),\n");
            sb.append("Char1 varchar(15),\n");
            sb.append("Char2 varchar(15),\n");
            sb.append("Char3 varchar(15),\n");
            sb.append("Char4 varchar(15),\n");

        sb.append(")\n");

        // Get a statement from the connection so we can execute the query.
        statement = conn.createStatement();
        statement.executeUpdate(sb.toString());
        tablesCreated = true;
    } catch (Exception e){
        System.out.println(e.getMessage());
    } finally {
        if(statement != null){
            try {
                statement.close();
            } 
            catch(Exception e){
                System.err.println(e.getMessage());
                System.exit(0); // Something is terribly wrong so just quit the program.
            }
        }
    }
}

private void createAnimal (String animalName, String char1, String char2, String char3, String char4){
    PreparedStatement pState = null;
    try{
        String sql = "Insert into Animal values (?,?,?,?,?,?)";
        pState = conn.prepareStatement(sql);
        pState.setInt(1, nextId);
        pState.setString(2, animalName);
        pState.setString(3, char1);
        pState.setString(4, char2);
        pState.setString(5, char3);
        pState.setString(6, char3);

        pState.executeUpdate();
        pState.close();
        incId();
    }
    catch (SQLException e){
        System.err.println(e.getMessage());
    }
}

private static void closeConnection() {
    try {
        // Close the connection
        if(conn != null){
            conn.close();
        }

    } catch(Exception e){
        System.out.println(e.getMessage());
    }
}

public static void queryShowAnimals() throws SQLException{
    String query = "SELECT * FROM Animal";
    try{
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()){
            out.print(rs.getInt(nextId) + " ");
            out.print(rs.getString("animalName") + "  ");
            out.print(rs.getString("char1") + ", ");
            out.print(rs.getString("char2") + ", ");
            out.print(rs.getString("char3") + ", ");
            out.print(rs.getString("char4") + ", ");                               
        }
    }catch (SQLException se){}

}
public static void main(String[] args) throws ClassNotFoundException, SQLException {

    AnimalDB1 db = new AnimalDB1();

    AnimalDB1.createConnection();

    System.out.println("Welcome to the Animal Database");
    System.out.println("The list below shows all of the animals currently "
                       + "stored in the database\n");

    db.animalTable();

    db.createAnimal("Huskie", "White", "Long hair", "Four legs", "Paws");
    db.createAnimal("Salmon", "Silver", "Scales", "Fins", "Swims");
    db.createAnimal("Crow", "Black", "Feathers", "Claws", "Flies");
    db.createAnimal("Black Snake", "Black", "Scales", "No Appendages", "Slithers");              

    AnimalDB1.queryShowAnimals();

    closeConnection();
}

}

【问题讨论】:

    标签: java jdbc derby


    【解决方案1】:

    有两个打字错误:

    ...
    StringBuilder sb = new StringBuilder("");
    sb.append("CREATE table Animal (\n");
        sb.append("ID INTEGER NOT NULL,\n");
        sb.append("AnimalName varchar(15),\n");
        sb.append("Char1 varchar(15),\n");
        sb.append("Char2 varchar(15),\n");
        sb.append("Char3 varchar(15),\n");
        sb.append("Char4 varchar(15)\n"); // <-- unnecessary comma
    
    sb.append(")\n");
    ...
    

    和:

    ...
    while (rs.next()){
        out.print(rs.getInt("ID") + " "); // <-- invalid column identifier
        out.print(rs.getString("animalName") + "  ");
        out.print(rs.getString("char1") + ", ");
        out.print(rs.getString("char2") + ", ");
        out.print(rs.getString("char3") + ", ");
        out.print(rs.getString("char4") + ", ");                               
    }
    ...
    

    另外,我相信您希望使用嵌入式数据库。因此,您需要加载相应的驱动程序(Java 6 以来的可选步骤):

    Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
    

    还有网址:

    jdbc:derby:AnimalDB;create=true;user=test;password=test
    

    【讨论】:

    • 非常感谢您指出这些内容,除此之外我做了一些更改,但您对列标识符的帮助起到了作用。
    【解决方案2】:

    尝试改用字符串缓冲区,但我看不出在每一行之后使用 \n 的意义。此外,你在每个 " 之后都有适当的空格。

    StringBuffer sb = new StringBuffer("");
        sb.append("CREATE table Animal ( ");
            sb.append("ID INTEGER NOT NULL ");
            sb.append("AnimalName varchar(15) ");
            sb.append("Char1 varchar(15) ");
            sb.append("Char2 varchar(15) ");
            sb.append("Char3 varchar(15) ");
            sb.append("Char4 varchar(15) ");
            sb.append(" ) ");
    

    【讨论】:

    • 我尝试了您的建议,但没有奏效,尽管错误确实略有变化。此外,当我将其更改为 StringBuffer 时,NetBeans 确实建议使用 StringBuilder。这是我现在得到的错误: 语法错误:在第 1 行第 43 列遇到“AnimalName”。架构“TEST”不存在 架构“TEST”不存在 架构“TEST”不存在 架构“TEST”不存在
    • n 你还要检查 url (url = "jdbc:derby://localhost:1527/AnimalDB;create=true;user=test;password=test";) 是否正确。我感觉它无法连接到数据库
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 2011-08-30
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多