0

I am learning Java getting trouble in JDBC connection.

Following is my code:

public class DemoPro {

public void test() {

    Connection con;
    Statement stmt;
    ResultSet rs;

        System.out.println("Looking for connection");
    try{
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        String dburl="jdbc:ucanaccess://E://WEB Project//SCMPayBoard//Database/SCMEmpPayment.accdb";
        con=DriverManager.getConnection(dburl,"","");
        System.out.println("Connection successfully");
        stmt=con.createStatement();
        String verify = "Select * from TestTable";
        stmt.executeQuery(verify);
        rs = stmt.getResultSet();
        System.out.println("Executing Query");

        while(rs.next())
        {
            System.out.println("Result/n/n/n");
            System.out.println(rs.getString(2) + " : " + rs.getString(3));
        }

        System.out.println("Closing Connection");
        con.close();
    }catch (SQLException | ClassNotFoundException ex) {
        Logger.getLogger(DemoPro.class.getName()).log(Level.SEVERE, null, ex);
    } 
}


public static void main(String[] args) {

  DemoPro dp=new DemoPro();
  dp.test();

}

}

Output : Looking for connection Connection successfully Executing Query Closing Connection BUILD SUCCESSFUL (total time: 1 second)

All sysout is executing but Table data is not fetched How do i know Table is getting access or not What should i do getting no result as expected

Nadim
  • 43
  • 7
  • All DB drivers that I know throw an exception if you do not have the required permissions. – Stefan Mar 15 '20 at 07:46
  • Check `System.out.println(rs.getString(1);`. Maybe you have just one column in the table. – Arvind Kumar Avinash Mar 15 '20 at 07:51
  • Use try with resources, so you do not have to force close connection. I guess you are not using any IDE? IF YES, then add debugger to `executing query` and check what your resultset(rs) has fetched. – bananas Mar 15 '20 at 07:57
  • For debug, change your query to '''Select count(*) from TestTable''' and print the result – Aditya Rewari Mar 15 '20 at 07:57
  • Either your database table `TestTable` might be empty or what Arvind Kumar Avinash has stated above can also be true. @Nadim, you can refer to [Zeb's answer](https://stackoverflow.com/questions/24229442/print-the-data-in-resultset-along-with-column-names) here or you can refer [this post](https://stackoverflow.com/questions/39436697/the-code-when-run-opens-the-jdbc-connection-but-it-is-not-printing-the-table-dat) as well. **Suggestion**: Always try to search for your similar question scenarios and refer to the approved & upvoted answers for the same. – Aniket Mar 15 '20 at 08:08
  • There is 3 Column @Arvind Kumar – Nadim Mar 15 '20 at 08:11
  • 2
    Guys I got the solution This code "stmt.executeQuery(verify);" was not assigned to ResultSet . Sol : rs= stmt.executeQuery(verify) – Nadim Mar 15 '20 at 08:22
  • `rs= stmt.executeQuery(verify)` is the typical way to execute the query, but it's strange that ucanaccess does not return that same resultset in the `rs = stmt.getResultSet();` . Ah well, well done on finding the solution. – racraman Mar 15 '20 at 08:35
  • @racraman In above code,ResultSet is not returning anything so after searching I change executeQuery to only execute() then in getResultSet gave the appropriate answer. Can you explain the reason ? – Nadim Mar 15 '20 at 08:39
  • `execute` is specified as returning a `boolean` and that you MUST use `getResultSet` to access its SELECT results. `executeQuery` returns a ResultSet, but it seems unstated/ambiguous whether that should be also returned from `getResultSet` or not (and in the case of ucanaccess it's not :) ). Anyhow, your solution is best practice (and always good to have less code :) ) – racraman Mar 15 '20 at 09:08

0 Answers0