日期:2014-05-16  浏览次数:20549 次

JAVA连接Access数据库
package com.yaku.access;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class AccessTest {
	public static void main(String[] args) {
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// String url = "jdbc:odbc:wombat";
			// string url=“jdbc:odbc:Driver={MicroSoft Access Driver
			// (*.mdb)};DBQ=“+application.getRealPath(“/Data/ReportDemo.mdb“);
			String url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:\\Test.mdb";
			Connection conn = DriverManager.getConnection(url, "", "");
			Statement stmt = conn.createStatement();
			// ResultSet rs = stmt.executeQuery("delete from arts where id =
			// 2");
			// System.out.println("a row has beenn inserted;");
			PreparedStatement ps ;
			for(int i=0;i<1000;i++){
				String sex = "";
				if(i%2==0){
					sex="M";
				}else{
					sex="F";
				}
				ps = conn.prepareStatement
					("insert into test values('"+i+"','Yaku"+i+"','"+sex+"')");
				ps.executeUpdate();
				ps.close();
			}
			// rs.close();
			stmt.close();
			conn.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

?查询Access数据

package com.yaku.access;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Select {
	public static void main(String[] args) {
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// String url = "jdbc:odbc:wombat";
			// string url=“jdbc:odbc:Driver={MicroSoft Access Driver
			// (*.mdb)};DBQ=“+application.getRealPath(“/Data/ReportDemo.mdb“);
			String url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:\\Test.mdb";
			Connection conn = DriverManager.getConnection(url, "", "");
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * FROM test");
			System.out.println("Got ResultSet Now");
			//rs.beforeFirst();
			while (rs.next()) {
				System.out.println(rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
			}
			rs.close();

			stmt.close();
			conn.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

?删除Access数据

package com.yaku.access;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class Delete {
	public static void main(String[] args) {
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// String url = "jdbc:odbc:wombat";
			// string url=“jdbc:odbc:Driver={MicroSoft Access Driver
			// (*.mdb)};DBQ=“+application.getRealPath(“/Data/ReportDemo.mdb“);
			String url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:\\Test.mdb";
			Connection conn = DriverManager.getConnection(url, "", "");
			Statement stmt = conn.createStatement();
			// ResultSet rs = stmt.executeQuery("delete from arts where id = 2");
			// System.out.println("a row has beenn inserted;");
			PreparedStatement ps = conn.prepareStatement("delete from test where id = '1'");

			ps.executeUpdate();
			// rs.close();
			ps.close();

			stmt.close();
			conn.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

?