Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connecting to ms access database

 
Oscar Kamau
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic