Why am I getting these errors from Apache Derby?

五迷三道 提交于 2019-12-11 04:46:38

问题


I run my program, the database is connected to and then it gives me two errors. One saying

'Schema "ROOT" does not exist' 

and another saying

'Lexical error at line 1, column 8. Encountered: "@"(64), after:"".

Here is the code from the two SQL statements:

private void UpdateJTable() {
    String sql ="select idhonscores AS RowNo , Name, Characters, Kills, Deaths, Assists, XPM, CK from honscores";
    try {
        st = conn.prepareStatement(sql);
        rs = st.executeQuery();
        table.setModel(DbUtils.resultSetToTableModel(rs));

    } catch(Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

That is from the first error and

String sql3 ="SELECT "+"@rn:=@rn+1"+" AS Rank, Name, Kills 
              FROM (Select Name, sum(Kills) as Kills 
                    from honscores group by Name order by Kills DESC) t1,
                   (SELECT "+"@rn:=0"+") t2;";

Is for the second error


回答1:


  1. In derby the default schema is always the schema of the user you use to create the jdbc connection. I can't tell from you question, how you initialize and setup the derby database. But appending ;create=true to the jdbc-url might help (this will create the db and schema if it does not exist).

You can change to a different schema by executing:

SET SCHEMA MYSCHEMA;
  1. The @-syntax might not be available in derby. Not everything that works in another db (especially if it is db specific) will work in derby.



回答2:


What has been said about username being the default schema is true. If you do not specify a username the default schema will be APP. This schema always exists. If you connect as user root the schema "root" will not be created until you create a table in it. E.g:

ij> connect 'jdbc:derby:memory:foo;user=root;create=true';
ij> show schemas;
TABLE_SCHEM                   
------------------------------
APP                           
NULLID                        
SQLJ                          
SYS                           
SYSCAT                        
SYSCS_DIAG                    
SYSCS_UTIL                    
SYSFUN                        
SYSIBM                        
SYSPROC                       
SYSSTAT                       

11 rows selected
ij> create table foo(i int) ;
0 rows inserted/updated/deleted
ij> show schemas;
TABLE_SCHEM                   
------------------------------
APP                           
NULLID                        
ROOT                          
SQLJ                          
SYS                           
SYSCAT                        
SYSCS_DIAG                    
SYSCS_UTIL                    
SYSFUN                        
SYSIBM                        
SYSPROC                       
SYSSTAT                       

12 rows selected

Wrt. @ in table and column names: That is only allowed if you quote the name containing @. E.g.:

ij> create table @T@(@c@ int);
ERROR 42X02: Lexical error at line 1, column 14.  Encountered: "@" (64), after : "".
ij> create table "@T@"("@c@" int);
0 rows inserted/updated/deleted
ij> insert into "@T@" values (0),(1),(2);
3 rows inserted/updated/deleted
ij> select * from "@T@";
@c@        
-----------
0          
1          
2          

3 rows selected

Note however, that when quoting a name it becomes case-sensitive. So in the previous example the following fails:

ij> select * from "@t@";
ERROR 42X05: Table/View '@t@' does not exist.

because in quotes, @T@ and @t@ are considered different names.



来源:https://stackoverflow.com/questions/8224840/why-am-i-getting-these-errors-from-apache-derby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!