JavaOracleJDBCSELECT语句

JavaOracleJDBCSELECT语句,java,sql,oracle,jdbc,Java,Sql,Oracle,Jdbc,我正在Eclipse环境中练习使用java的Oracle JDBC。通过使用next()迭代表格的每一行,我了解了如何输出SELECT*from product。我步履蹒跚 输出语句的步骤 SELECT pid, pname from product where price>20 这是我的密码: import java.sql.*; public class intro { /** * @param args */ public static

我正在Eclipse环境中练习使用java的Oracle JDBC。通过使用
next()
迭代表格的每一行,我了解了如何输出
SELECT*from product
。我步履蹒跚 输出语句的步骤

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/123321@localhost: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

在您的选择中,您只获得“pid”和“pname”:

但您尝试使用的字段不在您的选择中:

String productPrice = rows1.getString("price");
尝试在SELECT子句中添加“价格”。

您必须替换

SELECT pid, pname from product where price>20;


执行selectStat查询时,返回的列是什么?问题将出现在一个SQL调用中。我强烈建议将您的逻辑拆分为离散的方法-每个方法只能做一件事。还输出堆栈跟踪,因为它会告诉您在程序中的何处出现问题,从而更快地诊断错误。是的,每个结果集都应该在单独的循环中处理,这是正确的。
SELECT pid, pname from product where price>20;
SELECT pid, pname, price from product where price>20;