The code when run opens the jdbc connection but it is not printing the table data.What is wrong with this code?

ⅰ亾dé卋堺 提交于 2020-07-19 18:53:04

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/39436697/the-code-when-run-opens-the-jdbc-connection-but-it-is-not-printing-the-table-dat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!