1
package com.cg.tr.jdbc;
import java.sql.Connection; 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.cg.trg.utilself.DBUtilSelf;

 public class MenuBase {
public static void main(String[] args) {
 Connection connection = DBUtilSelf.openConnection();
 System.out.println("Connection opened");
 String sql="SELECT BNUM FROM BOOK";

try
 {
    Statement st=connection.createStatement();
    ResultSet rs=st.executeQuery(sql);
    System.out.println("Book details");
    while(rs.next())
    {
        System.out.print(rs.getInt("BNUM")+"\t");
        System.out.print(rs.getString("BNAME")+"\t");
        System.out.print(rs.getFloat("BPRICE")+"\t");
        System.out.print(rs.getString("BAUTHOR")+"\t");
        System.out.println();
     }

}
  catch(SQLException e)
  {
    e.printStackTrace();
  }
  finally
  {
    DBUtilSelf.closeConnection();
  } 

  }
 }

This code when run on eclipse does not print the data from table in oracle. The output is:

Connection opened Book details

It does not enter the while loop.Neither any exception is generated.I have made sure that column names from the table are written correctly.Still it does not give output

package com.cg.trg.utilself;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtilSelf 
 {

 static Connection connection;
 static String url;
 static String username;
 static String password;

 static
 {
    //load properties file...
    Properties prop =new Properties();
    FileInputStream fis;

    try
    {
    fis=new FileInputStream("jdbc.properties");
    prop.load(fis);
    }
   catch(IOException e)
    {
System.out.println("Problem while loading properties file:"+e.getMessage());

     }

 url=prop.getProperty("url");
 username=prop.getProperty("username");
password=prop.getProperty("password");

 }
 public static Connection openConnection()
 {
try
  {
   connection=DriverManager.getConnection(url,username,password);
  }
 catch(SQLException e)
 {
  System.out.println("Error while opening connection"+e.getMessage());
 }
return connection;
 }

public static void closeConnection()
{
    if(connection!=null)
    {
    try
    {
    connection.close();
    }
     catch(SQLException e)
     { 
    System.out.println("Error while closing connection:"+e.getMessage());
    }
    }
  }
  }

This is the DBUtilSelf.java file

Ribs
  • 21
  • 2
  • 1
    Because the table is empty? – Tunaki Sep 11 '16 at 13:42
  • 1
    Clearly, rs gives no data. – pepan Sep 11 '16 at 13:42
  • select * from book; BNUM BNAME BPRICE BAUTHOR ---------- -------------------- ---------- -------------------- 1001 Let Us C 1200 Yashvant Kanetkar 1002 Core JAVA 2100.65 H.Schild 1003 J2EE 2450 H.Schild 1004 Computer Networks 2200 Tanenbaum 1005 Operating System 3290 Millan 1006 Algorithms 1900 Coreman 1007 Complete C++ 1750.55 Yashvant Kanetkar 7 rows selected.The table is not empty – Ribs Sep 11 '16 at 13:48
  • What does 'SELECT BNUM FROM BOOK' give? – pepan Sep 11 '16 at 13:50
  • It gives all the booknums BNUM ---------- 1001 1002 1003 1004 1005 1006 1007 – Ribs Sep 11 '16 at 13:51
  • Why are you selecting one column but then printing other coulums that don't exist in the rs? – OldProgrammer Sep 11 '16 at 13:51
  • I tried with select * from book also in the main program,but still no results.How come rs is not getting any data? – Ribs Sep 11 '16 at 13:53
  • Can you post the content of this method as well DBUtilSelf.openConnection(); – Ramachandran.A.G Sep 11 '16 at 13:54
  • Which DB is underline? Isn't ';' missing at the end of the statement? – pepan Sep 11 '16 at 13:55
  • I have added the DBUtilSelf.java file – Ribs Sep 11 '16 at 14:01
  • where is ';' missing? – Ribs Sep 11 '16 at 14:02
  • Try to add "dbName." before BOOK; where dbName si the name of your database. – pepan Sep 11 '16 at 14:07
  • I added the dbName : SELECT * FROM ORCL.BOOK Connection opened java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:461) at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:402) at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1108) at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:541) com.cg.trg.jdbcdemos.MenuBase.main(MenuBase.java:18) – Ribs Sep 11 '16 at 14:12

1 Answers1

0

The code was right but the only problem was that after creating table in sql,it was not commited. Using the commit query in sql saves the changes in the database so that they can be visible through jdbc.

Ribs
  • 21
  • 2
  • I think this cannot be the case. If a table is not committed to a database, then it's not physically available i.e. it's absent. Since it's not available/committed and in such case, if one tries to access it an exception will be thrown like: `java.sql.SQLException: Table not found in statement [SELECT BNUM FROM BOOK]` for that [check here](https://stackoverflow.com/questions/39190278/table-not-found-in-statement). By the way, this isn't a solution for this scenario. – Aniket Mar 15 '20 at 08:32