最近做的项目中用到将oracle数据导出到access中和将access中数据导入到oracle中,大致操作如下:
使用jdbc进行数据库连接操作
- 连接oracle:
- Class.forName("oracle.jdbc.driver.OracleDriver");
- oracleUrl="jdbc:oracle:thin:@<host>:<port>:<SID> ";
- Connection oracleConn= DriverManager.getConnection(oracleUrl,"userName","password");
- String oracleSql="select * from .....";
- PreparedStatement oraclePs = conn.prepareStatement(oracleSql);
- //执行查询,查询数据存储在oracleRs结果集中
- ResultSet oracleRs=oraclePs.execute();
- //ResultSetMetaData可以获取记录的字段名、字段类型、字段数等信息
- ResultSetMetaData oracleRsoracleRsmd = oracleRs.getMetaData();
- while(oracleRs.next()){ //遍历每一条记录
- for(int i = 1; i <=oracleRsmd .getColumnCount(); i++){ //遍历记录的每一个字段
- //依次获取每一个字段的值,注意:此处不能重复获取统一个i的值
- System.out.println(oracleRs.getString(i));
- //依次获取每个字段的字段名
- System.out.println(oracleRsmd .getColumnName(i));
- //依次获取每个字段的类型,注意:此处不要写成oracleRsmd.getColumnType(i)
- System.out.println(oracleRsmd .getColumnTypeName(i));
- }
- }
- 连接access:
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- //此处对应office2003的access文件2007以上后缀为accdb请注意 另外文件名位置自己改
- String accessUrl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\test.mdb";
- //连接access的数据库一般来说都不需要密码(自己设了除外)
- Connection accessConn= DriverManager.getConnection(accessUrl,"","")
- String accessSql ="insert ........";
- PreparedStatement accessPs = accessConn.prepareStatement(accessSql);
- ResultSet acce***s = accessPs.executeQuery();
- ResultSetMetaData acce***sacce***smd = acce***s.getMetaData();
- ..............//下面操作与上面一样了
- 这两段代码嵌套搭配就可以实现数据的导出了,需要注意的是:oracle的字段类型和access字段类型有差别所以在创建表的时候要先获取字段类型进行判断转换,例如:
- if("VARCHAR2".equals(oracleRsmd .getColumnTypeName(i))){
- //下面zzz代码access中与varchar2对应的数据类型 sql语句需要循环来拼字符串
- String accessSql="create table xxx ( yyy zzzz,.............";
- }
- 下面附上access与oracle字段对应表
The table below shows the ADO Data Type mapping between Access, SQL Server, and Oracle:
DataType Enum | Value | Access | SQLServer | Oracle |
---|---|---|---|---|
adBigInt | 20 | BigInt (SQL Server 2000 +) | ||
adBinary | 128 | Binary TimeStamp | Raw * | |
adBoolean | 11 | YesNo | Bit | |
adChar | 129 | Char | Char | |
adCurrency | 6 | Currency | Money SmallMoney | |
adDate | 7 | Date | DateTime | |
adDBTimeStamp | 135 | DateTime (Access 97 (ODBC)) | DateTime SmallDateTime | Date |
adDecimal | 14 | Decimal * | ||
adDouble | 5 | Double | Float | Float |
adGUID | 72 | ReplicationID (Access 97 (OLEDB)), (Access 2000 (OLEDB)) | UniqueIdentifier (SQL Server 7.0 +) | |
adIDispatch | 9 | |||
adInteger | 3 | AutoNumber Integer Long | Identity (SQL Server 6.5) Int | Int * |
adLongVarBinary | 205 | OLEObject | Image | Long Raw * Blob (Oracle 8.1.x) |
adLongVarChar | 201 | Memo (Access 97) Hyperlink (Access 97) | Text | Long * Clob (Oracle 8.1.x) |
adLongVarWChar | 203 | Memo (Access 2000 (OLEDB)) Hyperlink (Access 2000 (OLEDB)) | NText (SQL Server 7.0 +) | NClob (Oracle 8.1.x) |
adNumeric | 131 | Decimal (Access 2000 (OLEDB)) | Decimal Numeric | Decimal Integer Number SmallInt |
adSingle | 4 | Single | Real | |
adSmallInt | 2 | Integer | SmallInt | |
adUnsignedTinyInt | 17 | Byte | TinyInt | |
adVarBinary | 204 | ReplicationID (Access 97) | VarBinary | |
adVarChar | 200 | Text (Access 97) | VarChar | VarChar |
adVariant | 12 | Sql_Variant (SQL Server 2000 +) | VarChar2 | |
adVarWChar | 202 | Text (Access 2000 (OLEDB)) | NVarChar (SQL Server 7.0 +) | NVarChar2 |
adWChar | 130 | NChar (SQL Server 7.0 +) |
* In Oracle 8.0.x - decimal and int are equal to number and number(10).
转载于:https://blog.51cto.com/zhangzhiyogn/1186755