简体   繁体   English

DB2 SQL错误:SQLCODE = -204,SQLSTATE = 42704

[英]DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704

I created local database in DB2 called " TestDB " then I created table called " TestTable ". 我在DB2中创建了名为“ TestDB ”的本地数据库,然后创建了名为“ TestTable ”的表。
I found that the table is put under schema name is " yasmin ". 我发现该表放在模式名称下面是“ yasmin ”。
I am trying to connect to the DB2 database using JDBC but I got this exception 我试图使用JDBC连接到DB2数据库但我得到了这个例外

    R SQLException information
[1/4/14 11:32:59:289 EST] 0000004d SystemErr     R Error msg: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=DB2ADMIN.TESTTABLE, DRIVER=3.61.86
[1/4/14 11:32:59:290 EST] 0000004d SystemErr     R SQLSTATE: 42704
[1/4/14 11:32:59:290 EST] 0000004d SystemErr     R Error code: -204
[1/4/14 11:32:59:290 EST] 0000004d SystemErr     R com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=DB2ADMIN.TESTTABLE, DRIVER=3.61.86

I tried many solutions on the internet Like set schema but unfortunately doesn't work. 我在互联网上尝试了很多解决方案,就像设置架构一样,但遗憾的是不行。

This is the JDBC code I used 这是我使用的JDBC代码

 String urlPrefix = "jdbc:db2:";
        String url;
        String user;
        String password;
        String empNo;                                                              
        Connection con;
        Statement stmt;
        ResultSet rs;

        url = urlPrefix + "//127.0.0.1:50000/TestDB";
        user = "db2admin";
        password = "db2admin";
        try 
        {                                                                        
          // Load the driver
          Class.forName("com.ibm.db2.jcc.DB2Driver");                              
          System.out.println("**** Loaded the JDBC driver");

          // Create the connection using the IBM Data Server Driver for JDBC and SQLJ
          con = DriverManager.getConnection (url, user, password);                 
          // Commit changes manually

          con.setAutoCommit(false);
          System.out.println("**** Created a JDBC connection to the data source");
          stmt = con.createStatement();   con.createStatement();                                         
          System.out.println("**** Created JDBC Statement object");
          // Execute a query and generate a ResultSet instance

          rs = stmt.executeQuery("select *from TestTable");                   
          System.out.println("**** Created JDBC ResultSet object");
        }

        catch (ClassNotFoundException e)
        {
          System.err.println("Could not load JDBC driver");
          System.out.println("Exception: " + e);
          e.printStackTrace();
        }

        catch(SQLException ex)                                                      
        {
          System.err.println("SQLException information");
          while(ex!=null) {
            System.err.println ("Error msg: " + ex.getMessage());
            System.err.println ("SQLSTATE: " + ex.getSQLState());
            System.err.println ("Error code: " + ex.getErrorCode());
            ex.printStackTrace();
            ex = ex.getNextException(); // For drivers that support chained exceptions
          }
        }

As @Mark Rotteveel said, the -204 error is from a missing object, but it's missing for a reason other than what he said. 正如@Mark Rotteveel所说, -204错误是来自一个丢失的物体,但是由于其他原因而失踪了。

It's not found because you did not prefix it with the schema name. 找不到它,因为您没有为模式名称添加前缀。 You said above that it's in schema yasmin , but you're connecting with db2admin , so it's trying to look for db2admin.TestTable . 你在上面说过它是在架构yasmin ,但是你正在与db2admin连接,所以它试图寻找db2admin.TestTable

SELECT * FROM yasmin.TestTable

should be what you're looking for. 应该是你正在寻找的。

By default, the search path for schemas is the name of the currently connecting user. 默认情况下,模式的搜索路径是当前连接用户的名称。 You can see what it is using 你可以看到它的用途

SELECT CURRENT SCHEMA FROM SYSIBM.SYSDUMMY1

If you want to change it, you can use the SET SCHEMA command to change the search path, but usually it's easier just to include the schema name in your query. 如果要更改它,可以使用SET SCHEMA命令更改搜索路径,但通常只需在查询中包含模式名称即可。

The error SQLERROR -204, SQLSTATE 42704 is a missing/unknown objectname and it is most likely caused by a missing space in: 错误SQLERROR -204,SQLSTATE 42704是一个缺失/未知的对象名,很可能是由于以下缺少的空间引起的:

rs = stmt.executeQuery("select *from TestTable");

Which should be: 应该是:

rs = stmt.executeQuery("select * from TestTable");

The problem was The table I created in db2 has schema name "yasmin" 问题是我在db2中创建的表具有模式名称"yasmin"
I used username and password "db2admin/db2admin" In JDBC connection 我在JDBC连接中使用了用户名和密码"db2admin/db2admin"
so it search for the table in this schema "db2admin" 所以它在这个模式“db2admin”中搜索表
so I need to set the schema to be "yasmin" 所以我需要将架构设置为"yasmin"
so I added the following lines of code after creating the connection 所以我在创建连接后添加了以下代码行

            String schemaName="yasmin";
            if (schemaName != null && schemaName.length() != 0) {

            try {
                statement = connection.createStatement();
                statement.executeUpdate("set current sqlid = " + schemaName);
                System.out.println("The schema is set successfully.");
            } catch (SQLException exception) {
                exception.printStackTrace();

            }

and grant db2admin all privilages on this table in db2 并在db2中为此表授予db2admin所有特权

1-Right click on the table choose privilages 1 - 右键单击​​表格选择特权
2-Click on "Add user" button 2 - 单击“添加用户”按钮
3-then write the username "db2admin" and click "apply" button 3 - 然后写入用户名“db2admin”并单击“应用”按钮
4-Then select the user you just add and click "gtant all" button 4 - 然后选择您刚添加的用户并单击“gtant all”按钮

Now he can see the table. 现在他可以看到桌子了。

Many Thanks @Mark Rotteveel and @bhamby for your help. 非常感谢@Mark Rotteveel和@bhamby的帮助。

如果您使用Hibernate在Spring中进行连接,请在属性行中使用以下内容

<property name="url" value="jdbc:db2://<ip>:<port>/<database>:currentSchema=<currentSchema>;" />

其他方式只是创建一个ALIAS,喜欢这样:

CREATE ALIAS USER1.YOURTABLE FOR TABLE USER2.YOURTABLE;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我使用DB2 LUW和WebSphere App Server获得SQLCODE = -204,SQLSTATE = 42704? - Why do I get SQLCODE=-204, SQLSTATE=42704 with DB2 LUW and WebSphere App Server? DB2 jdbc SQL 错误:SQLCODE=-302,SQLSTATE=22001 on Select - DB2 jdbc SQL Error: SQLCODE=-302, SQLSTATE=22001 on Select DB2错误SQLCODE = -103,SQLSTATE = 42604 - DB2 Error SQLCODE=-103, SQLSTATE=42604 SQL查询的准备好的语句,错误DB2 SQL错误:SQLCODE = -206,SQLSTATE = 42703 - Prepared statement for SQL query, error DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703 com.ibm.db2.jcc.am.io:DB2 SQL错误:SQLCODE = -440,SQLSTATE = 42884 - com.ibm.db2.jcc.am.io: DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884 对SQL查询(DB2)使用PreparedStatement SQLCODE = -206 SQLSTATE = 42703 - Using a PreparedStatement for a SQL query (DB2) SQLCODE=-206 SQLSTATE=42703 插入并选择Select Give sql-error(SQLCODE = -803,SQLSTATE = 23505)(db2 z / os) - Insert with Select give sql-error (SQLCODE=-803, SQLSTATE=23505)(db2 z/os) 准备好的语句失败,并出现DB2 SQL错误:SQLCODE:-401,SQLSTATE:42818 - Prepared Statement failing with DB2 SQL error: SQLCODE: -401, SQLSTATE: 42818 DB2 SQL错误:SQLCODE = -117,SQLSTATE = 42802,SQLERRMC = null,DRIVER = 3.68.61 - DB2 SQL Error: SQLCODE=-117, SQLSTATE=42802, SQLERRMC=null, DRIVER=3.68.61 在具有DB2并行进程的Java中:我出现了死锁异常:DB2 SQL错误:SQLCODE = -911,SQLSTATE = 40001,SQLERRMC = 2,DRIVER = 3.59.81 - In java with DB2 parallel process: I got Deadlock Exception :DB2 SQL Error: SQLCODE=-911, SQLSTATE=40001, SQLERRMC=2, DRIVER=3.59.81
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM