0
votes

This is my code to query a database for a resultset using db2

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class Db2{
    public static void main(String[] argv) {
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
        }
        catch (ClassNotFoundException e) {
            System.out.println("Please include Classpath  Where your DB2 Driver is located");
            e.printStackTrace();
            return;
        }
        System.out.println("DB2 driver is loaded successfully");
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs=null;
        Statement stmt = null;
        boolean found=false;
        String name = "";
        try {
            conn = DriverManager.getConnection("jdbc:db2://server:900/MyDB"
                    +":user=user1;password=swim;"+
                     "traceLevel=" +
                     (com.ibm.db2.jcc.DB2BaseDataSource.TRACE_ALL) + ";");
            if (conn != null)
            {
                System.out.println("DB2 Database Connected");
            }
            else
            {
                System.out.println("Db2 connection Failed ");
            }

            // Create the Statement
            stmt = conn.createStatement();                                            
            System.out.println("**** Created JDBC Statement object");

            // Execute a query and generate a ResultSet instance
            rs = stmt.executeQuery("SELECT * FROM VIRTUALMACHINE");                    
            System.out.println("**** Created JDBC ResultSet object");

            device
            while (rs.next()) {
              name = rs.getString(2);
              System.out.println("Name = " + name);
            }
            System.out.println("**** Fetched all rows from JDBC ResultSet");
            // Close the ResultSet
            rs.close();
            System.out.println("**** Closed JDBC ResultSet");



        } catch (SQLException e) {
            System.out.println("DB2 Database connection Failed");
            e.printStackTrace();
            return;
        }
    }

}

This is the result I get when I run it.

DB2 driver is loaded successfully DB2 Database Connected **** Created JDBC Statement object DB2 Database connection Failed com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=user1.MyDB, DRIVER=3.71.22 at com.ibm.db2.jcc.am.gd.a(gd.java:813) at com.ibm.db2.jcc.am.gd.a(gd.java:66) at com.ibm.db2.jcc.am.gd.a(gd.java:140) at com.ibm.db2.jcc.am.gp.c(gp.java:2788) at com.ibm.db2.jcc.am.gp.d(gp.java:2776) at com.ibm.db2.jcc.am.gp.a(gp.java:2220) at com.ibm.db2.jcc.t4.bb.i(bb.java:148) at com.ibm.db2.jcc.t4.bb.b(bb.java:41) at com.ibm.db2.jcc.t4.p.a(p.java:32) at com.ibm.db2.jcc.t4.vb.i(vb.java:145) at com.ibm.db2.jcc.am.gp.ib(gp.java:2189) at com.ibm.db2.jcc.am.gp.a(gp.java:3286) at com.ibm.db2.jcc.am.gp.a(gp.java:711) at com.ibm.db2.jcc.am.gp.executeQuery(gp.java:690) at Db2.main(Db2.java:46)

I want to know why it does not return anything when I run it. When I change the query string from select VIRTUALMACHINE from MyDB to "SELECT * FROM SYSCAT.COLUMNS WHERE TABSCHEMA= 'GGA' AND TABNAME= 'VIRTUALMACHINE' fetch first 10 rows only" I get a result. In this case

DB2 driver is loaded successfully DB2 Database Connected **** Created JDBC Statement object **** Created JDBC ResultSet object Name = GGA Name = GGA Name = GGA Name = GGA
**** Fetched all rows from JDBC ResultSet **** Closed JDBC ResultSet

Which is pretty much the description of the columns. I want to why select VIRTUALMACHINE from MyDB produces nothing. Is it not how querying is done in DB2?

1
Are you sure you don't mean SELECT * FROM VIRTUALMACHINE?Kayaman
My bad.. that is what I meant. i have been playing around with it. Changing it up to see what works. I just copied and pasted with out changing it back. Thanks.user3078335
SELECT * FROM VIRTUALMACHINE was what i used. It didn't work at first, so I was just playing around with other stuff. I fixed it.user3078335
What about GGA.VIRTUALMACHINE?Kayaman
@Kayaman thank you so much!!user3078335

1 Answers

1
votes

SQLSTATE 42704 is thrown when an object doesn't exist.

The FROM clause references a table name, not a DB. The table name may be qualified or unqualified; and technically the qualified table name could include a remote DB name.

You say this works:

SELECT * 
FROM SYSCAT.COLUMNS 
WHERE TABSCHEMA= 'GGA' AND TABNAME= 'VIRTUALMACHINE' 
fetch first 10 rows only

Note that the COLUMNS table in the SYSCAT schema is being queried.

So the following should work:

select *
from GGA.VIRTUALMACHINE