• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connecting to MS Access

 
Felix Li
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am trying to do a tiny practice on connecting to a simple Access Database stored locally. Some web sites explained using DriverManager, but I am confused on the construction on the url part. Anyone can explain to me how DriverManager is used and how can I know what the url is supposed to be? Any methods to connection to Database other than DriverManager are welcomed.

Thanks in advance
 
Xavier Anguita
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm not sure if i can resolve your problem but i can tell how i connect to access in my java application.

I have a java class called connector which connects to access like following:

public class connectionAccess {

Connection con = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement pstmt = null;

static{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
System.out.println("Error cnfe: "+ ex.getMessage());
}
}

public void createDataBaseConnection() throws SQLException{

con = DriverManager.getConnection("jdbcdbc:DBNameSpecifiedInControlPanel","","");
stmt = con.createStatement();
}

public void closeDataBaseConnection() {

try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (con != null) con.close();
} catch (SQLException ex) {
}
}
}


then you can call the method createDataBaseConnection before you do your selects/updates/deletes and then the closeDataBaseConnection method.

In the control panel / odbc administrator you have to make System DSN to your access DB called DBNameSpecifiedInControlPanel
 
Felix Li
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got it, thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic