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
もうまく動作していないようです。
を試してみてください? –
何が例外ですか?何が問題なのですか?働かないという意味ですか? – developer
@TimBiegeleisen私がオンラインで見た例は、この形式に従っています。私は自分自身でかなり新しいです。私は、今日、DB2で作業を始めました。 – user3078335