2012-01-08 70 views
4

我在Eclipse環境中使用java練習Oracle JDBC。我明白如何通過使用next()重複每一行表格來輸出SELECT * from product。我都有點吃力 要輸出的語句Java Oracle jdbc SELECT語句

SELECT pid, pname 
from product 
where price>20 

這裏是我的代碼:

import java.sql.*; 


public class intro { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) 
    { 
    // throws SQLException 

     //initiazlie the connection 

     Connection con=null; 

     try //try connection to database 
     { 
      //load driver 
      Class.forName("oracle.jdbc.OracleDriver"); 
      System.out.println("Oracle JDBC driver loaded ok."); 
      con=DriverManager.getConnection("jdbc:oracle:thin:test/[email protected]:1521:orcl"); 
      System.out.println("Connect with @oracle:1521:orcl"); 

      //declaring statement 
      Statement stmt = con.createStatement(); 

      String dropProductTable="drop table product cascade constraints"; 

      //create string 
      String createProductTable="CREATE TABLE product(" + 
      "pid number," + 
      "pname CHAR(20)," + 
      "price number," + 
      "PRIMARY KEY (pid)" + 
      ")"; //do not add the semicolon(;) after closing the parenthesis. 


      /*drop table */ 
      stmt.executeUpdate(dropProductTable); 


      //execute the create statement 
      stmt.executeUpdate(createProductTable);//execure the create statement 

      //create string that holds the insert statement 
      String insertIntoProduct="INSERT INTO product VALUES (1,'Pepsi',10)"; 
      String insertIntoProduct1="INSERT INTO product VALUES (2,'Fanta',20)"; 
      String insertIntoProduct2="INSERT INTO product VALUES (3,'Mirinda',30)"; 
      String insertIntoProduct3="INSERT INTO product VALUES (4,'Gum',5)"; 
      String updatePrice="UPDATE product set price=55 where price=20"; 



      //stmt.executeUpdate(insertIntoProduct); 
      stmt.executeUpdate(insertIntoProduct); 
      stmt.executeUpdate(insertIntoProduct1); 
      stmt.executeUpdate(insertIntoProduct2); 
      stmt.executeUpdate(insertIntoProduct3); 

      //update statement 
      stmt.executeUpdate(updatePrice); 



      //save the select statement in a string 
      String selectStat="SELECT * FROM product"; 
      String selectProduct="SELECT pid, pname from product where price>20"; 
      //stmt.executeUpdate(selectStat); 

      //create a result set 
      ResultSet rows = stmt.executeQuery(selectStat); 
      ResultSet rows1= stmt.executeQuery(selectProduct); 

      //stmt.executeQuery(selectStat); 


      int count=0; 
      while (rows.next()) { 
       count+=1; 
       String productNumber = rows.getString("pid"); 
       String productName = rows.getString("pname"); 
       String productPrice = rows.getString("price"); 
       System.out.println("Row #:"+count); 
       System.out.println("Product#: "+productNumber); 
       System.out.println("Product Name: "+productName); 
       System.out.println("Price: "+productPrice); 

       } 

      int count1=0; 
      while (rows1.next()) { 
       count1+=1; 
       String productNumber = rows1.getString("pid"); 
       String productName = rows1.getString("pname"); 
       String productPrice = rows1.getString("price"); 
       System.out.println("Row #:"+count); 
       System.out.println("Product#: "+productNumber); 
       System.out.println("Product Name: "+productName); 
       System.out.println("Price: "+productPrice); 

       } 

      con.close(); 

     } 
       catch (Exception e) 
       { 
        System.err.println("Exception:"+e.getMessage()); 
       } 


     } 

    } 

當我試圖輸出中selectProduct可變我得到這個錯誤

Exception:Invalid column name 

請需要幫助

這裏是我得到的輸出

Oracle JDBC driver loaded ok. 
Connect with @oracle:1521:orcl 
Row #:0 
Product#: 2 
Product Name: Fanta    
Price: 55 
Row #:0 
Product#: 3 
Product Name: Mirinda    
Price: 30 
+0

當你做** ** selectStat查詢,被返回的列是什麼? – 2012-01-08 14:49:32

+1

這個問題將出現在你的一個SQL調用中。我強烈建議將你的邏輯分解成不同的方法 - 每個只應該做一件事。同時輸出堆棧跟蹤,因爲它會告訴你程序中的哪個位置存在問題,使得診斷錯誤更快。 – 2012-01-08 14:51:22

回答

9

在你選擇只得到 「PID」 和 「PNAME」:

String selectProduct="SELECT pid, pname from product... 

但是當你正在嘗試使用一個字段是不是在你的SELECT:

String productPrice = rows1.getString("price"); 

試着把「價格」放在你的SELECT子句中。

+0

使用2個循環輸出查詢是否正確? – mydreamadsl 2012-01-08 14:54:03

+1

是的,你是正確的,每個結果集應該在不同的循環中處理。 – Aaron 2012-01-08 14:55:49

6

必須更換

SELECT pid, pname from product where price>20; 

SELECT pid, pname, price from product where price>20; 
+0

謝謝,uahakan! – mydreamadsl 2012-01-08 14:51:56