How does Java manipulate an Access database? See the following example: [Don't know why, insert code times wrong, so just paste it out]
Package Com.ria.utils.common;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import Java.sql.ResultSetMetaData;
Import java.sql.Statement;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
public class Msaccessdbutils {
private static Msaccessdbutils instance = NULL;
public static Msaccessdbutils getinstance () {
if (instance = = null) {
Instance = new Msaccessdbutils ();
}
return instance;
}
Private Msaccessdbutils () {}
First called Mdbfile=d:/xxx.mdb
public void Loadconfig (String mdbfile,string user,string psw) throws exception{
Mdb_file = Mdbfile;
user = user;
PWD = PSW;
Loaddriver ();
}
public void Loadconfig (String mdbfile) throws exception{
Mdb_file = Mdbfile;
Loaddriver ();
}
private static String Dirverclass = "Sun.jdbc.odbc.JdbcOdbcDriver";
Jdbc:odbc:driver={microsoft Access Driver *.mdb)};D BQ = Northwind.mdb
Jdbc:odbc:driver={microsoft Access Driver (*.mdb)}; Dbq=c://test.mdb
private static String URL =
"Jdbc:odbc:driver={microsoft Access Driver (*.mdb)}; Dbq= ";
private static String Mdb_file = Null;//access file [here to Absolute path]
private static String user = "";
private static String pwd = "";
private static Connection Conn;
private static Statement stmt;
private static ResultSet Rs;
private void Loaddriver () throws exception{
try {
Class.forName (Dirverclass);
catch (Exception e) {
Throw e;
}
}
Create a connection that cannot be scrolled
public static void Connect () throws exception{
try {
System.out.println (Url+mdb_file);
SYSTEM.OUT.PRINTLN (user);
System.out.println (PWD);
conn = Drivermanager.getconnection (url+mdb_file, user, PWD);
stmt = Conn.createstatement ();
catch (Exception e) {
Throw e;
}
}
public static void Connect (Boolean autocommit) throws exception{
try {
conn = Drivermanager.getconnection (url+mdb_file, user, PWD);
Conn.setautocommit (autocommit);
stmt = Conn.createstatement ();
catch (Exception e) {
Throw e;
}
}
Create a connection that can be scrolled
public static void Connect2 () throws exception{
try {
conn = Drivermanager.getconnection (url+mdb_file, user, PWD);
stmt = Conn.createstatement (resultset.type_scroll_insensitive,
RESULTSET.CONCUR_READ_ONLY);
catch (Exception e) {
Throw e;
}
}
Close connection
public static void Close () throws exception{
try {
if (Rs!= null) {
Rs.close ();
Rs=null;
}
if (stmt!= null) {
Stmt.close ();
Stmt=null;
}
IF (conn!= null) {
Conn.close ();
Conn=null;
}
catch (Exception e) {
Throw e;
}
}
Query statement
public static List executequery (String sql) throws exception{
List L = new ArrayList ();
try {
if (stmt = = null) {
Connect ();
}
rs = stmt.executequery (SQL);
L = orgresultset4list (RS);
Close ();
catch (Exception e) {
Throw e;
}
return l;
}
public static int executeupdate (String sql) throws exception{
try {
if (stmt = = null) {
Connect ();
}
int res = stmt.executeupdate (SQL);
Close ();
return res;
catch (Exception e) {
Throw e;
}
}
public static int[] executeupdate (string[] sql) throws exception{
try {
if (stmt = = null) {
Connect (false);
}
for (int i = 0; i < sql.length; i++) {
Stmt.addbatch (Sql[i]);
}
int[] res = Stmt.executebatch ();
Conn.commit ();
Close ();
return res;
catch (Exception e) {
Throw e;
}
}
private static List Orgresultset4list (ResultSet rs) throws exception{
try {
ResultSetMetaData RSMD = Rs.getmetadata ();
int cols = Rsmd.getcolumncount ();
List L = new ArrayList ();
Map recordmap = null;
while (Rs.next ()) {
Recordmap = new HashMap ();
for (int i = 0; i < cols; i++) {
Recordmap.put ((String) (Rsmd.getcolumnname (i+1)). toLowerCase (), Rs.getobject (i+1));
}
L.add (RECORDMAP);
}
return l;
}
catch (Exception ex) {
Ex.printstacktrace ();
Throw ex;
}
}
public static void Main (string[] args) {
if (args!=null && args.length>0) {
System.out.println ("args[0]=" +args[0]);
try {
Msaccessdbutils.getinstance (). Loadconfig (Args[0], "", "");
catch (Exception ex) {
Ex.printstacktrace ();
System.exit (-1);
}
Msaccessdbutils.getinstance (). Go ();
}else{
System.exit (0);
}
}
void Go () {
try{
for (int k = 0; k < 3; k++) {
String sql =
"INSERT into Leave_words (cmp_name,tell,web_url,addr,mail,conts,notes) values";
SQL + =
"(' Company Name 2 ', ' 0100003333 ', ' http://sdfa.com ', ' Beijing da SDF chopper Rice card third ', ' aa@aa.com ', ' Well done ', ' [No] ')";
int res = Msaccessdbutils.getinstance (). executeupdate (SQL);
System.out.println ("insert=" + res);
sql = "SELECT * from Leave_words";
List L = msaccessdbutils.getinstance (). executequery (SQL);
Msaccessdbutils.getinstance (). Close ();
if (l!= null) {
System.out.println ("= =" + (l.get (0)). toString ());
Java.text.SimpleDateFormat DF =
New Java.text.SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss W E");
Map Rowmap;
for (int i = 0; i < l.size (); i++) {
Rowmap = (MAP) l.get (i);
Java.util.Date Crt_time =
New Java.util.Date ((Java.sql.Timestamp) Rowmap.get (
"Crt_time")). GetTime ());
System.out.println (Df.format (crt_time));
}
Java.sql.Date d = new Java.sql.Date ();
}
}
}catch (Exception ex) {
Ex.printstacktrace ();
System.exit (-1);
}
}
}