从DB2中的表中获取值

时间:2016-12-06 00:46:43

标签: java database jdbc db2 db2-400

我想使用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 似乎没有效果。

0 个答案:

没有答案