gpt4 book ai didi

java - 从 db2 获取结果集

转载 作者:行者123 更新时间:2023-12-01 09:09:13 26 4
gpt4 key购买 nike

这是我使用 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;
}
}

}

这是我运行时得到的结果。

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)

我想知道为什么当我运行它时它不返回任何内容。当我将查询字符串从 select VIRTUALMACHINE from MyDB 更改为 “SELECT * FROM SYSCAT.COLUMNS WHERE TABSCHEMA= 'GGA' AND TABNAME= 'VIRTUALMACHINE' 仅获取前 10 行” 我得到了结果。在这种情况下

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

这几乎是列的描述。我想知道为什么 select VIRTUALMACHINE from MyDB 没有产生任何结果。 DB2 中的查询不就是这样完成的吗?

最佳答案

当对象不存在时抛出 SQLSTATE 42704。

FROM 子句引用表名,而不是数据库。表名可以是限定的,也可以是不限定的;从技术上讲,限定表名称可以包括远程数据库名称。

你说这有效:

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

请注意,正在查询 SYSCAT 架构中的 COLUMNS 表。

所以以下应该有效:

select *
from GGA.VIRTUALMACHINE

关于java - 从 db2 获取结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41026347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com