Oops! Access Ole Object Quick Start

Oops! Access Ole Object Quick Start
 

Oops! Access Ole Object Quick Start

Purpose:

操作accressole对象。并掌握测试驱动

Quick Start

新建一个名叫Oops_Access的项目java project。新建一个lib文件夹,导入以下类库

添加项目依赖性

在项目根目录加入一个demo.mdb文件(直接ctrl+C就可以进来了)

demo.mdb数据库里面建立一张表,名字demo,表结构如下:

新建一个类,名叫DBController.java

import  java.io.ByteArrayInputStream;

import  java.sql.Connection;

import  java.sql.DriverManager;

import  java.sql.PreparedStatement;

import  java.sql.ResultSet;

import  java.sql.Statement;

public   class  DBController

{

       
public   void  Insert(String value)

       {

              
try

              {

                     Class.forName(
" sun.jdbc.odbc.JdbcOdbcDriver " );

                     String database 
=   " jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=demo.mdb " ;

                     Connection con 
=  DriverManager.getConnection(database,  "" "" );

                     String sql 
=   " INSERT INTO DEMO (NAME,MEMO) VALUES(?,?) " ;

                     PreparedStatement pstm 
=   null ;

                     pstm 
=  con.prepareStatement(sql);

                     pstm.setString(
1 " Dr.Oops " );

                     ByteArrayInputStream stream 
=   new  ByteArrayInputStream(value

                                   .getBytes());

                     pstm.setBinaryStream(
2 , stream, stream.available());

                     pstm.execute();

                     pstm.close();

                     con.close();

              }

              
catch  (Exception e)

              {

                     System.out.println(
" Error:  "   +  e);

              }

       }

       
public  String Select(String id)

       {

              String memo 
=   null ;

              
try

              {

                     Class.forName(
" sun.jdbc.odbc.JdbcOdbcDriver " );

                     String database 
=   " jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=demo.mdb " ;

                     Connection con 
=  DriverManager.getConnection(database,  "" "" );

                     Statement s 
=  con.createStatement();

                     s.execute(
" select ID,NAME,MEMO from DEMO where ID= "   +  id);

                     ResultSet rs 
=  s.getResultSet();

                     
if  (rs  !=   null )

                     {

                            
while  (rs.next())

                            {

                                   
byte [] image  =  rs.getBytes( 3 );

                                   memo 
=   new  String(image,  " gb2312 " );

                            }

                     }

                     s.close();

                     con.close();

              }

              
catch  (Exception e)

              {

                     System.out.println(
" Error:  "   +  e);

              }

              
return  memo;

       }

}



新建一个TestMe.java文件

import  junit.framework.TestCase;

public   class  TestMe  extends  TestCase

{

       
public   void  test()

       {

              DBController controller 
=   new  DBController();

              controller.Insert(
" nice to meet you! " );

              System.out.println(controller.Select(
" 1 " ));

       }

}



然后Run as JUnit

你可能感兴趣的:(Oops! Access Ole Object Quick Start)