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