Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 12th, 2006, 3:27 PM   #1
Sengbasy
Newbie
 
Join Date: May 2006
Posts: 3
Rep Power: 0 Sengbasy is on a distinguished road
JDBC-Problem

I faced a problem while I was working on this exercise about Java Database Connectivity. The problem is when I run the application it throws an SQL Exception. Because the lack of my experience, I didn't know what's the application problem

Note: The exception is ([Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'.)

import java.sql.*;
import java.io.*;

public class ProgramDB {
  public static void main(String[] args) {
    Connection conn = null;

    try {
      // 1) Load the driver
      String Driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      Class.forName(Driver);

      // 2) Create a connection to the database
      File file = new File("Programs.mdb");
      String DatabaseFile = file.getAbsolutePath();
      System.out.println("Database path " + DatabaseFile);

      String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="       + DatabaseFile;
      conn = DriverManager.getConnection(url, "", "");

      // 3) Create a JDBC statement for executing SQL expressions
      Statement statement = conn.createStatement();

      // 4) Execute an SQL expression and save the resutl in a ResultSet
      ResultSet rs = statement.executeQuery("SELECT * FROM Software");

      // 5) Maniplute the result
      System.out.println("Program\t\tVersion\t\tDescription\t\tCrack\t\tCD");
      System.out.println("-------\t\t-------\t\t-----------\t\t-----\t\t--");

      while (rs.next()) {
        String program = rs.getString("Program");
        String version = rs.getString("Version");
        String description = rs.getString("Description");
        boolean crack = rs.getBoolean("Crack");
        String cd = rs.getString("CD");

        System.out.println(program + "\t\t\t" + version + "\t\t\t" + description
        + "\t\t\t" + crack + "\t\t\t" + cd);
      }
    }

    catch (ClassNotFoundException ex) {
      System.out.println("Failed to load MS Access driver.");
    }

    catch (SQLException sqle) {
      System.out.println("SQL Exception: " + sqle.getMessage());
    }

    finally {
      if (conn != null)
        try {
          conn.close();
        }

        catch (SQLException ex) {
          ex.printStackTrace();
        }
    }
  }
}
Sengbasy is offline   Reply With Quote
Old May 26th, 2006, 10:00 AM   #2
kirkl_uk
Programmer
 
kirkl_uk's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 86
Rep Power: 4 kirkl_uk is on a distinguished road
Send a message via MSN to kirkl_uk
What does DatabaseFile contain when you output it after calling getAbsolutePath()? It seems the Access Driver cannot find this file, so it might be as simple as the file not being where it should be, or the DatabaseFile does not contain the FULL path.
__________________
kirkl_uk
kirkl_uk is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC