0
我想從使用db2的表中獲取值並打印出結果。從DB2中的表中獲取值
這是我試圖用做代碼:
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class connection {
public static void main(String[] argv) {
try {
Class.forName("COM.ibm.db2.jdbc.app.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 rset=null;
boolean found=false;
try {
conn = DriverManager.getConnection("jdbc:db2:sabarish","db2admin","Murugasaranam");
if (conn != null)
{
System.out.println("DB2 Database Connected");
}
else
{
System.out.println("Db2 connection Failed ");
}
pstmt=conn.prepareStatement("SELECT * FROM SYSCAT.COLUMNS WHERE TABSCHEMA= 'STD' AND TABNAME= 'inventory'");
rset=pstmt.executeQuery();
if(rset!=null)
{
while(rset.next())
{
found=true;
System.out.println("Class Code: "+rset.getString("clcode"));
System.out.println("Name: "+rset.getString("name"));
}
}
if (found ==false)
{
System.out.println("No Information Found");
}
} catch (SQLException e) {
System.out.println("DB2 Database connection Failed");
e.printStackTrace();
return;
}
}
}
,只打印出列名。而不是列名,我可以使用什麼查詢語句來獲得結果? db2 select * from store.inventory
似乎不工作。
我不是一個DB2專家以任何手段,但爲什麼你從你的JDBC查詢忽略數據庫名稱? –
什麼是例外?究竟是什麼問題?不工作的意思? – developer
@TimBiegeleisen我在網上看到的例子遵循這種格式。我自己很新。我今天剛剛開始使用DB2。 – user3078335