It's not a secret anymore!*
The moose likes JDBC and the fly likes connecting to ms access database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "connecting to ms access database" Watch "connecting to ms access database" New topic
Author

connecting to ms access database

Oscar Kamau
Greenhorn

Joined: Oct 02, 2002
Posts: 20
I am using ms access 97 database and I want to read data from it (Oscar1.mdb) and display
it on the command prompt. Here is my code.
import java.sql.*;
import java.util.*;
public class myJdbc{
public static void main(String args[]){
String url="jdbc dbc scar1";
String query= "SELECT * FROM Person";
Statement stmt;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch (ClassNotFoundException e) {
System.out.println(e+" The class is not found");
}

try{
Connection con =DriverManager.getConnection(url);
stmt= con.createStatement();
ResultSet rs = stmt.executeQuery(query);

while(rs.next()){
int number = rs.getInt("PERSON#");
String firstName = rs.getString("FIRST");
String lastName = rs.getString("LAST");
System.out.println(number + " " + firstName + " " + lastName);

}
rs.close();
stmt.close();
con.close();
}catch(SQLException ex ){
ex.printStackTrace();
}
}
}
After compiling and running the code I'm geting these errors.
java.sql.SQLExeception: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
at sun.jdbc.JdbcOdbc.createSQLExeception(JdbcOdbc.java:6106)
at sun.jdbc.JdbcOdbc.standardError(JdbcOdbc.java:6263)
at sun.jdbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:2488)
at sun.jdbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:317)

at sun.jdbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:160)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at myJdbc.main(myJdbc.java:21)
I am a beginner java and anxious to move on. Please help...
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Did you create a DSN named Oscar1? If not then that is your next step.
You can also connect without using a DSN, the syntax is similar to this:
Oscar Kamau
Greenhorn

Joined: Oct 02, 2002
Posts: 20
Originally posted by Chris Mathews:
Did you create a DSN named Oscar1? If not then that is your next step.
You can also connect without using a DSN, the syntax is similar to this:

Thanks Chris.
I opted to connect without using the DSN, but unfotunately, I'm getting the same errors.
Oscar
Oscar Kamau
Greenhorn

Joined: Oct 02, 2002
Posts: 20
Thanks very much Chris Mathews. Your code worked finally. The reason it had earlier failed is due to the naming of the Access 97 drivers on the computer I was using. Thanks a lot, I appreciate it.
 
Consider Paul's rocket mass heater.
 
subject: connecting to ms access database