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();
}
}
}
}