Looking for a JavaEE Architect or Potential Tech Co-Founder?
Please don't hesitate to contact me.

How to connect MSAccess Database from Java using JDBC Driver

The method is almost similar to my previous code, and like that it's better to explain it by code:

private static Connection getConnection() {
 String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String url = "jdbc:odbc:anime"; //anime is the database
    String username = "ipieluser"; //leave blank if none
    String password = "ipielpassword"; //leave blank if none
    try {
  Class.forName(driver);
 } catch (ClassNotFoundException e) {
  e.printStackTrace();
 }
    try {
  return DriverManager.getConnection(url, username, password);
 } catch (SQLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 return null;
}

How to call:
public static void main(String args[]) {
 try {
  Connection conn = getConnection();
     Statement st = conn.createStatement();
     st = conn.createStatement();
     ResultSet rs = st.executeQuery("SELECT * FROM localTable");     
     
  //get and displays the number of columns
     ResultSetMetaData rsMetaData = rs.getMetaData();
  int numberOfColumns = rsMetaData.getColumnCount();
     System.out.println("resultSet MetaData column Count=" + numberOfColumns);
 
     st.close();
     conn.close();
 } catch(Exception e) {
  System.out.println(e.getMessage());
 }
}
How to connect MSAccess Database from Java using JDBC Driver How to connect MSAccess Database from Java using JDBC Driver Reviewed by czetsuya on Wednesday, January 20, 2010 Rating: 5

No comments:

Powered by Blogger.