Java Oracle jdbc SELECT 语句

标签 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 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

最佳答案

在您的 SELECT 中,您只会得到“pid”和“pname”:

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

但是您正在尝试使用不在您的 SELECT 中的字段:

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

尝试将“价格”放入您的 SELECT 子句中。

关于Java Oracle jdbc SELECT 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8778422/

相关文章:

java - 如何计算字体的宽度?

java - 在没有同步的情况下使用 volatile

sql - PostgreSQL 更新触发器

sql - 尝试 block 内的@@ RowCount无法返回正确的值--SQL Server

java - 处理 '\&' 从 java 到 SP(除了设置定义/扫描关闭)

java - JDBC ResultSet.getWarnings

java - JSONObject 解析字典对象

java - 与外部类同名的内部类?

mysql - 如何排除不等于运算符的 NULL?

java - Glassfish v3.0.1 与 Oracle GlassFish Server 3.0.1 产品的对比——使用商业版本的任何_技术_理由?