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?
SELECT * FROM VIRTUALMACHINE
? – KayamanSELECT * FROM VIRTUALMACHINE
was what i used. It didn't work at first, so I was just playing around with other stuff. I fixed it. – user3078335GGA.VIRTUALMACHINE
? – Kayaman