Results 1 to 5 of 5

Thread: MS Access Database problem - No data found [Resolved]

  1. #1

    Thread Starter
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180

    Resolved MS Access Database problem - No data found [Resolved]

    Hi guys,

    Bit of a puzzler here. I've written an app that connects to a MSAccess DB fine. The DB has 3 colums - Employeename (Text or String), PassId (Number/long) and Passdate (Date/Time). All 3 columns have data entered in them. I can retrieve info from the first column but whenever I try to get data from the other two, tthe SQL Exception thrown tells me No Data Found.

    Would really appreciate any help you could give me. Thanks in advance.

    Code:
    import java.sql.*;
    import java.util.Date;
    import java.util.Calendar;
    
    public class Interactor {
    	
    	
    private String URL = ""; private String username = ""; private String password = ""; private int employees = 0; private Statement stmt = null; private Connection con = null; private String dataSourceName = "NewTest"; public Interactor ( String u, String p ) { dataSourceName = "NewTest"; URL = "jdbc:odbc:" + dataSourceName; username = u; password = p; setupConnection (); } private void P ( String S ) { System.out.print ( S ); } private void PL ( String S ) { System.out.println ( S ); } public void setupConnection () { try { Class.forName ( "sun.jdbc.odbc.JdbcOdbcDriver" ); } catch ( Exception e ) { System.out.println ( "Failed to load JDBC/ODBC driver." ); return; } try { con = DriverManager.getConnection ( URL, "", "" ); stmt = con.createStatement (); } catch ( Exception e ) { System.err.println ( "problems connecting to " + URL ); System.err.println ( e.getMessage () ); e.printStackTrace (); } } public void getPassDates () { try { ResultSet result = stmt.executeQuery ( "SELECT * FROM Emp;" ); result.next(); PL ( result.getString ( "Employeename" ) ); PL ( "" + result.getLong ( "PassId" ) ); PL ( result.getDate ( "Passdate" ).toString () ); while ( result.next () ) { count ++; PL ( result.getDate ( "Passdate" ).toString () ); PL ( "" + result.getLong ( "PassId" ) ); } } catch ( Exception e ) { System.err.println ( e.getMessage () ); e.printStackTrace (); } } public int getNoEmployees () { try { String q = "SELECT Employeename FROM Emp;"; ResultSet result = stmt.executeQuery ( q ); result.next(); // move to first row PL ( result.getString ( "Employeename" ) ); //PL ( result.getDate ( 1 ).toString () ); employees = 1; while ( result.next () ) { employees ++; PL ( result.getString( "Employeename" ) ); //PL ( result.getDate ( "Passdate" ).toString () ); } PL ( "Number of employees : " + employees ); } catch ( Exception e ) { System.err.println ( e.getMessage () ); e.printStackTrace (); } return employees; }
    }

    <Moderator added green checkmark to the thread>
    Last edited by NoteMe; Feb 22nd, 2005 at 12:52 PM.
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: MS Access Database problem - No data found

    When you execute the query, what is "Emp"? Shouldn't that be the name of the database, like "New Test" ?

  3. #3

    Thread Starter
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180

    Re: MS Access Database problem - No data found

    Emp is the name of the only table in the database. The database is named NewTest.
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  4. #4
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: MS Access Database problem - No data found

    Should be something like this.
    VB Code:
    1. import java.sql.*;
    2.  
    3. public class test{
    4.     public static void main(String[] args){
    5.         Connection cn=null;
    6.         try{
    7.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    8.             cn=DriverManager.getConnection("jdbc:[b][/b]odbc:test","","");
    9.         }
    10.         catch(Exception ex){
    11.             System.out.println(ex.getMessage());
    12.         }
    13.  
    14.         employee e=new employee(cn);
    15.         System.out.println(e.count());
    16.         e.displayEmployees();
    17.     }
    18. }
    19.  
    20. class employee{
    21.     Connection cn;
    22.     public employee(Connection cn){
    23.         this.cn=cn;
    24.     }
    25.  
    26.     public void displayEmployees(){
    27.         try{
    28.             Statement s=cn.createStatement();
    29.             ResultSet rs=s.executeQuery("select * from emp");
    30.             while(rs.next()){
    31.                 System.out.println(rs.getString(1));
    32.                 System.out.println(rs.getString(2));
    33.                 System.out.println(rs.getDate(3)+"\n");
    34.             }
    35.         }
    36.         catch(Exception ex){
    37.             System.out.println(ex.getMessage());
    38.         }
    39.     }
    40.  
    41.     public int count(){
    42.         int n=0;
    43.         try{
    44.             Statement s=cn.createStatement();
    45.             ResultSet rs=s.executeQuery("select count(*) from emp");
    46.             rs.next();
    47.             n=Integer.parseInt(rs.getString(1).toString());
    48.         }
    49.         catch(Exception ex){
    50.             System.out.println(ex.getMessage());
    51.         }
    52.         return n;
    53.     }
    54. }

  5. #5

    Thread Starter
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180

    Re: MS Access Database problem - No data found

    Just solved the problem a couple of days ago but it was pretty similar to your suggestion - just changed the code to result.getString (1) etc as opposed to calling dat aby column name.

    Thanks for the help though!
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width