Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7025595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:59:43+00:00 2026-05-27T23:59:43+00:00

I am practising Oracle JDBC using Java in Eclipse. I understood how to output

  • 0

I am practising Oracle JDBC using Java in Eclipse. I understood how to output SELECT * from product by iterating each line of the table using .next(). I am stuggling to output this statement:

SELECT pid, pname 
FROM product 
WHERE price>20

Here is my code:

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());
                }
                
        
        }
        
    }

When I am trying to ouput the selectProduct variable I get this error:

Exception:Invalid column name

Here is the output I am getting:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:59:43+00:00Added an answer on May 27, 2026 at 11:59 pm

    In your SELECT you are only getting “pid” and “pname”:

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

    But then you’re trying to use a field that isn’t in your SELECT:

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

    Try putting “price” in your SELECT clause.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following code for practising, http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BorderLayoutDemoProject/src/layout/BorderLayoutDemo.java I also add frame.setSize(frame.getMaximumSize()); in createAndShowGUI()
I am trying to create a new File in Eclipse using the following Java
I'm practising Django now, and I'm stuck. When I run: >>> from django import
i was practising to create,store and get data's from database, i am successful in
I am practising c# with a phonebook example using Hashtable. I have a class
I'm practising grid view from the book, Beginning android 2, I typed this xml
Good day! I'm practising materials from Ruby on Rails Tutorial by Michael Hartle. Below
While practicing the jxl API, i cant extract details from certain Excel sheet its
I'm practicing Jquery and I've written this simple Jquery statement: var someText = $(table
I just started practicing TDD in my projects. I'm developing a project now using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.