I often get an ODBC source drive error when I link to access
And then I realized that it was.
To connect to an Access data source, you first establish a JDBC-ODBC bridge by:
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
Class is a class in the Java.lang package that can be built by calling its static method forname. However, an exception may occur when the bridge is established, which we do not want to see, so we want to catch this exception:
try{
class.forname ("Sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e) {exception capture}
Then there is the connection to the database, where you use the connection class in the JAVA.SQL package to declare an object, and then use the static method getconnection the class DriverManager call it to create the connection.
Connection con=drivermanager.getconnection ("JDBC:ODBC: Data Source Name", "Data source username", "Data source password");/note This is the name and password of the data source you need to configure.
try{
class.forname ("Sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e) {
System.out.println ("" +e);
}
The following sentence is the key
String path = This.getclass (). getClassLoader (). GetResource ("Db1.mdb"). GetPath (). substring (1);
Db1.mdb for Create Access database files
//This database file path to Java file on the same path as
String url= "Jdbc:odbc:driver={microsoft Access Driver" (* . mdb)};D bq= "+path;
Connection conn=drivermanager.getconnection (URL);
Statement st = Conn.createstatement ();
Instance
Package Com.neusoft.common;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
/** * C3P0 JDBC Connection Pool * * @author * */public class Dbconnectionpool {protected Dbconnectionpool () {}
private static Combopooleddatasource ds = NULL;
static {try {ds = new Combopooleddatasource ();
Set JDBC Driver class Ds.setdriverclass ("Sun.jdbc.odbc.JdbcOdbcDriver"); Set JDBC URL ds. Setjdbcurl ("Jdbc:odbc:driver={microsoft Access driver (*.mdb)};D bq=h:/gy
T_web/database/siteweaver.mdb ");
Set the login username//ds.setuser ("admin") of the database;
Set the login username//Ds.setpassword ("admin") of the database;
Set the maximum connection number of the connection pool ds.setmaxpoolsize (200);
Set the minimum connection number of the connection pool ds.setminpoolsize (20); } CATCH (Exception e) {e.printstacktrace ();
} protected static synchronized Connection getconnection () {Connection con = null;
try {con = ds.getconnection ();
catch (SQLException e) {e.printstacktrace ();
return con; /** * Query Common method * * @param SQL * @param params * @return */public ResultSet query (S)
Tring SQL, object[] params) {Connection conn = null;
PreparedStatement PS Tutorial tmt = NULL;
ResultSet rs = null;
conn = This.getconnection ();
try {pstmt = conn.preparestatement (sql); if (params!= null) {for (int i = 0; i < params.length; i++) {Pstmt.setobject (i +
1, params[i]);
rs = Pstmt.executequery (); catch (SQLException e) {//TODO auto-generated catch block E.printstacktracE ();
} return RS; /** * Modify Add Delete Common method * * @param SQL * @param params * @return * * * public int Execute
SQL (String sql, object[] params) {Connection conn = null;
PreparedStatement pstmt = null;
int result = 0;
conn = This.getconnection ();
try {pstmt = conn.preparestatement (sql); if (params!= null) {for (int i = 0; i < params.length; i++) {Pstmt.setobject (i +
1, params[i]);
result = Pstmt.executeupdate ();
catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
return result; }
}
Java Connection Access instance:
Import java.sql.*;
public class Connectaccess {
/**
* Beginner Please note:
* 1: First create an Access file A1.mdb, and put it under D:;
* 2: Create a table in the database file A1.mdb Table1
3: Add a column for Table1 and insert at least one record;
* 4: This article is a complete class that can be run directly.
*/public
static void Main (String args[]) throws Exception {
connectaccess ca=new connectaccess ();
Ca. Connectaccessfile ();
Ca. Connectaccessdatasource ();
}
public void Connectaccessfile () throws Exception
{
class.forname ("" "Sun.jdbc.odbc.JdbcOdbcDriver");
/**
* Connect directly to an Access file.
*/
String dbur1 = "" Jdbc:odbc:driver={microsoft Access Driver (*.mdb)};D bq=d:a1.mdb "";
Connection conn = drivermanager.getconnection (Dbur1, "username" "," "password");
Statement stmt = Conn.createstatement ();
ResultSet rs = stmt.executequery ("" "Select * from Table1");
while (Rs.next ()) {
System.out.println (rs.getstring (1));
}
Rs.close ();
Stmt.close ();
Conn.close ();
}
Attention:
JDBC can complete a connection to a database and then send SQL statements to the database to handle the results returned by the database.
JDBC is designed to resemble ODBC. One way JDBC and database connections are established is to first establish a JDBC-ODBC bridge.
First you install office2000, you have Access2000, you start Access, you create a new database Mess.mdb, you create a table that contains two text fields name and sex, the table name is member, and you literally enter some strings into the members table. Save in any directory, such as under C disk. Then open the Control Panel-> management tools-> Data Source (ODBC), select System DSN, or select the User DSN tab, where DSN for each user applies to avoid permissions problems that occur when debugging, click Add,
Click "Finish", the pop-up dialog box to set the "data source name" to your favorite name, such as Redsun. Click "Select" To find "C:mess.mdb" file, so an ODBC data source is configured