Fri Apr 08, 2011 4:08 am
Fri Apr 08, 2011 1:51 pm
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MSAccessDAO {
private String username;
private String password;
private String driver;
private String URL =
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\MSDB\\rundb.mdb;}";
public MSAccessDAO(String username, String password, String driver) {
this.username = username;
this.password = password;
if (driver == null || driver.trim() == "") {
this.driver = "sun.jdbc.odbc.JdbcOdbcDriver";
}
}
public Connection getConnection() {
try {
Class.forName(driver);
Connection connection = DriverManager.getConnection(URL, username, password);
return connection;
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return null;
}
public static void main(String[] args) throws Exception {
Connection connection = null;
try {
MSAccessDAO accessConnect = new MSAccessDAO("admin","admin", null);
connection =accessConnect.getConnection();
if(connection!=null){
// Do your work .
}else {
System.out.println("Failed to create connection to DB");
}
}finally {
connection.close();
}
}
public String getURL() {
return URL;
}
public void setURL(String URL) {
this.URL = URL;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.