derby嵌入式数据库netbeans语法错误

时间:2016-12-28 05:41:51

标签: java netbeans derby embedded-database

我正在尝试使用Derby嵌入式数据库。我添加了jar文件和库,并能够创建表。但是,出于某种原因,当我尝试插入数据时,会触发异常。它写着:

"java.sql.SQLSyntaxErrorException: Syntax error: Encountered
                                   "testName" at line 1, column 18." 

我绝对相信我提供了正确的表名和列名。到目前为止,我还没有找到任何其他帖子的答案。 下面是我为测试编写的代码示例,并返回相同的异常。

    private static final String URL = "jdbc:derby:EDBTest;create=true;";
    private static final String USERNAME = "xxx";
    private static final String PASSWORD = "xxx";

    private Connection connection = null;

    public TestEDBM() {

        try {

//            System.setProperty("derby.system.home", "User");

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

            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TestEDBM.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(TestEDBM.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void createDB() {
        try {
            PreparedStatement x = connection.prepareStatement(
                    "CREATE TABLE test(testID INT not null primary key"
                    + " GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
                    + "testName VARCHAR(50))"
            );
            x.execute();
        } catch (SQLException ex) {
            Logger.getLogger(TestEDBM.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addValue(String data) {
        int id = -1;
        try {
            PreparedStatement x = connection.prepareStatement(
                    "INSERT INTO test testName values (?)", Statement.RETURN_GENERATED_KEYS
            );
            x.setString(1, data);

            id = x.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(TestEDBM.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(id);
    }

    public String retrieveData() {
        ResultSet resultSet = null;
        String data = "";
        try {
            resultSet = connection.createStatement().executeQuery("SELECT * FROM"
                    + " test");
            resultSet.next();

            data = resultSet.getString("testName");

        } catch (SQLException ex) {
            Logger.getLogger(TestEDBM.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } // end try
            catch (SQLException sqlException) {
                sqlException.printStackTrace();
            } // end catch
        } // end finally
        return data;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        TestEDBM t = new TestEDBM();
//        t.createDB();
        t.addValue("Tester");
        System.out.println(t.retrieveData());
    }

}

0 个答案:

没有答案