`
myhongkongzhen
  • 浏览: 97001 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

ACCESS数据库资源导入到ORACLE数据库表中的实现

    博客分类:
  • J2EE
阅读更多

 

 

2009年5月8号 天气晴  星期五

 

以下的代码实现了从ACCESS数据库表中将记录导入到ORACLE数据库表中的功能,主要的核心功能是ORACLE数据库ID自动生成的实现方法,应用到ROWID的方式。

 

具体代码如下:   (仅仅是一个可运行的DEMO参考,请修改部分代码后再运行于自己的机器上)

 

package g.cms.exchange;

import g.cms.bean.Archive;
import g.cms.bean.ArchiveType;
import g.cms.business.ArchiveService;
import g.cms.business.ArchiveTypeService;
import g.sql.ConnectionPool;
import g.sql.SQLService;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Jane(吴贞贞)
 * @email myhongkongzhen@gmail.com
 * @since JDK 1.6
 * @alter 2009年*月*号
 * @version 1.0 2009年3月**号
 */
public class AccessToOracle {
	private final static Log logger = LogFactory.getLog(AccessToOracle.class);

	// +DBFile(AccessDB)
	private static String url = "jdbc:odbc:driver="
			+ "{Microsoft Access Driver (*.mdb)};DBQ=";
	private static Connection accessConn, oracleConn;
	private static Statement accessStmt, accSt, accSts;
	private static PreparedStatement oracleStmt;
	// private static Statement accessStmt, oracleStmt;
	private static ResultSet accessRs, oracleRs, accRs, accRss;

	private ArchiveType archiveType;
	private ArchiveTypeService archiveTypeSrv;
	private Archive archive;
	private ArchiveService archiveSrv;
	private Map<Integer, Integer> idCache = new HashMap<Integer, Integer>();
	private List<String> nameCache = new ArrayList<String>();

	public ArchiveType getArchiveType() {
		return archiveType;
	}

	public void setArchiveType(ArchiveType archiveType) {
		this.archiveType = archiveType;
	}

	public ArchiveTypeService getArchiveTypeSrv() {
		return archiveTypeSrv;
	}

	public void setArchiveTypeSrv(ArchiveTypeService archiveTypeSrv) {
		this.archiveTypeSrv = archiveTypeSrv;
	}

	public Archive getArchive() {
		return archive;
	}

	public void setArchive(Archive archive) {
		this.archive = archive;
	}

	public ArchiveService getArchiveSrv() {
		return archiveSrv;
	}

	public void setArchiveSrv(ArchiveService archiveSrv) {
		this.archiveSrv = archiveSrv;
	}

	public Map<Integer, Integer> getIdCache() {
		return idCache;
	}

	public void setIdCache(Map<Integer, Integer> idCache) {
		this.idCache = idCache;
	}

	public AccessToOracle() {
		super();
		// TODO Auto-generated constructor stub
	}

	static {
		try {
			// Oracle Connection
			String urlo = "jdbc:oracle:thin:@192.168.1.3:1522:orcl";
			String driver = "oracle.jdbc.driver.OracleDriver";
			String password = "yppt";
			String user = "yppt";
			Class.forName(driver);
			oracleConn = DriverManager.getConnection(urlo, user, password);
			// oracleConn = ConnectionPool.getConnection();
			// oracleStmt = oracleConn.createStatement();
			logger.debug(oracleConn);

			// Access Connection
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			accessConn = DriverManager.getConnection(url
					+ "E:/workspace/yppt/docs/DATA/new3000/news3000.mdb");

			logger.debug(accessStmt);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			logger.debug("Access DB數據庫連接異常,類未找到...");
			e.printStackTrace();
		}
	}

	// 欄目數據AccessToOracle
	public void accessTypeToOracleChannel() {
		Object autoIncKeyFromApi = -1;
		ResultSet accRs = null;
		Statement accSt = null;
		Statement accSts = null;
		ResultSet accRss = null;

		try {
			accessStmt = accessConn.createStatement();

			accessRs = accessStmt
					.executeQuery("SELECT t.typeid,t.typename,t.typecontent FROM type t");

			logger.debug("準備查詢Access DB數據源數據...");

			int i = 1;
			int b = 1;
			int s = 1;
			while (accessRs.next()) {
				logger.debug("\n--父欄目導入欄目中..." + (i++));
				Integer typeid = accessRs.getInt("typeid");
				String typename = accessRs.getString("typename");
				String typecontent = accessRs.getString("typecontent");

				archiveType = new ArchiveType();
				archiveType.setName(typename);
				archiveType.setDescription(typecontent);
				archiveTypeSrv = new ArchiveTypeService();

				Integer id = archiveTypeSrv.insert(archiveType);
				// 導入數據后生成的ID同源數據中的ID相對應
				idCache.put(id, typeid);
				logger.debug("\n===\ntypeid : " + typeid + "====id : " + id
						+ "\n===");

				accSt = accessConn.createStatement();
				accRs = accSt
						.executeQuery("SELECT  b.bigclassid,b.bigclasszs,b.bigclassname"
								+ " FROM bigclass b WHERE typeid=" + typeid);
				while (accRs.next()) {
					logger.debug("\n--大類子欄目導入欄目中..." + (b++));
					Integer bigclassid = accRs.getInt("bigclassid");
					String bigclassname = accRs.getString("bigclassname");
					String bigclasszs = accRs.getString("bigclasszs");
					archiveType = new ArchiveType();
					archiveType.setName(bigclassname);
					archiveType.setDescription(bigclasszs);
					archiveType.setPid(id);
					archiveTypeSrv = new ArchiveTypeService();

					Integer bid = archiveTypeSrv.insert(archiveType);
					// 導入數據后生成的ID同源數據中的ID相對應
					idCache.put(bid, bigclassid);
					logger.debug("\n===\nbigclassid : " + bigclassid
							+ "====bid : " + bid + "===typeid : " + typeid
							+ "\n===");

					accSts = accessConn.createStatement();
					accRss = accSts
							.executeQuery("SELECT  s.smallclassid,s.smallclasszs,s.smallclassname "
									+ "FROM smallclass s WHERE s.BigClassID="
									+ bigclassid);
					while (accRss.next()) {
						logger.debug("\n--小類子欄目導入欄目中..." + (s++));
						Integer smallclassid = accRss.getInt("smallclassid");
						String smallclassname = accRss
								.getString("smallclassname");
						String smallclasszs = accRss.getString("smallclasszs");
						archiveType = new ArchiveType();
						archiveType.setName(smallclassname);
						archiveType.setDescription(smallclasszs);
						archiveType.setPid(bid);
						archiveTypeSrv = new ArchiveTypeService();

						Integer sid = archiveTypeSrv.insert(archiveType);
						// 源數據中的ID同導入數據后生成的ID相對應
						idCache.put(sid, smallclassid);
						logger.debug("\n===\nsmallclassid : " + smallclassid
								+ "====sid : " + sid + "===typeid" + typeid
								+ "\n===");

						logger
								.debug("\n-------重新讀取小欄目TYPEID==SMALLCLASSID---------");
					}

					logger.debug("\n-------重新讀取大欄目TYPEID==BIGCLASSID---------");

				}

				logger.debug("\n---------重新讀取欄目TYPEID---------");
			}

			archiveType = new ArchiveType();
			archiveType.setName("評論");
			archiveTypeSrv = new ArchiveTypeService();

			Integer id = archiveTypeSrv.insert(archiveType);
			// 導入數據后生成的ID同源數據中的ID相對應
			idCache.put(id, -1);
			nameCache.add("評論");
			logger.debug("\n====id : " + id + "\n===");

			archiveType = new ArchiveType();
			archiveType.setName("评论");
			archiveType.setPid(id);
			archiveTypeSrv = new ArchiveTypeService();

			Integer zid = archiveTypeSrv.insert(archiveType);
			// 導入數據后生成的ID同源數據中的ID相對應
			idCache.put(zid, id);
			nameCache.add("評論");
			logger.debug("\n====zid : " + zid + "\n===");

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionPool.close(accRs);
			ConnectionPool.close(accSt);
		}

	}

	// ACCESS文章導入ORACLE數據庫1
	public void accessNewsToOracleArchive() {
		Object autoIncKeyFromApi = -1;
		ResultSet accRs = null;
		Statement accSt = null;
		Statement accSts = null;
		ResultSet accRss = null;

		logger.debug("\n-----------\n" + idCache.size() + "\n--------");

		try {
			Integer bigclassid = 0;
			Set<Integer> keySet = idCache.keySet();
			for (Integer key : keySet) {
				bigclassid = idCache.get(key);
				logger.debug("\n==smallclassid\n" + bigclassid + "\n===");
				String sql = "SELECT n.newsid,n.title,n.checked,n.author,"
						+ "n.editor,n.updatetime,n.content,n.about,"
						+ "n.encode,n.click FROM news n "
						+ "WHERE n.bigclassid=" + bigclassid;

				accessStmt = accessConn.createStatement();
				accessRs = accessStmt.executeQuery(sql);

				logger.debug("準備查詢Access DB數據源數據...");

				int i = 1;
				while (accessRs.next()) {
					logger.debug("\n--文章導入中..." + (i++));

					oracleStmt = null;
					oracleRs = null;
					oracleStmt = oracleConn.prepareStatement("INSERT INTO "
							+ "archive(id,typeid,"
							+ "title,ischecked,author,sender,sendtime,"
							+ "keyword,click) " + "VALUES("
							+ "(select (nvl(max(to_number(id)), 0)) "
							+ "from archive)+1" + ",?,?,?,?,?,?,?,?)",
							Statement.RETURN_GENERATED_KEYS);

					logger.debug("\n---------欄目TYPEID---------" + key + "\n");

					oracleStmt.setInt(1, key);
					String title = accessRs.getString("title");
					if (null != title) {
						oracleStmt.setString(2, title);
					} else {
						continue;
					}
					String html = accessRs.getString("encode");
					if (null != html) {
						if ("html".equals(html.toLowerCase())) {
							oracleStmt.setInt(3, 1);
						} else {
							oracleStmt.setInt(3, 0);
						}
					} else {
						oracleStmt.setInt(3, 0);
					}
					oracleStmt.setString(4, accessRs.getString("author"));
					oracleStmt.setString(5, accessRs.getString("editor"));
					oracleStmt.setTimestamp(6, accessRs
							.getTimestamp("updatetime"));
					oracleStmt.setString(7, accessRs.getString("about"));
					oracleStmt.setInt(8, accessRs.getInt("click"));
					oracleStmt.executeUpdate();

					oracleRs = oracleStmt.getGeneratedKeys();
					if (oracleRs.next()) {
						autoIncKeyFromApi = oracleRs.getObject(1);
						if (autoIncKeyFromApi instanceof oracle.sql.ROWID) {
							autoIncKeyFromApi = ((oracle.sql.ROWID) autoIncKeyFromApi)
									.stringValue();
							String keySql = "SELECT ID FROM archive WHERE rowid=?";
							oracleStmt = null;
							oracleRs = null;
							oracleStmt = oracleConn.prepareStatement(keySql,
									Statement.RETURN_GENERATED_KEYS);
							oracleStmt.setObject(1, autoIncKeyFromApi);
							oracleRs = oracleStmt.executeQuery();
							if (oracleRs.next()) {
								autoIncKeyFromApi = oracleRs.getInt(1);
								// // 源數據中的ID同導入數據后生成的ID相對應
								// idCache.put((Integer) autoIncKeyFromApi, -1);
								String bolbSql = "UPDATE archive SET BODY=? WHERE ID=?";
								String content = accessRs.getString("content");
								if (null != content) {
									java.sql.Blob body = g.sql.SQLHelper
											.createBlob(content
													.getBytes("UTF-8"));
									Object[] args = new Object[] { body,
											autoIncKeyFromApi };
									SQLService ss = new SQLService();
									ss.update(bolbSql, args);
								} else {
									continue;
								}
							}
						} else {
							throw new RuntimeException(
									"操作失败,没有处理的Oracle RETURN_GENERATED_KEYS类型!");
						}
					}
				}
				logger.debug("\n---------重新讀取欄目TYPEID---------");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionPool.close(accRs);
			ConnectionPool.close(accSt);
		}
	}

	// ACCESS文章導入ORACLE數據庫2
	public void accessNewsToOracleArchive2() {
		Object autoIncKeyFromApi = -1;
		ResultSet accRs = null;
		Statement accSt = null;
		Statement accSts = null;
		ResultSet accRss = null;

		logger.debug("\n-----------\n" + idCache.size() + "\n--------");

		try {
			String sql = "SELECT b.id,b.title,b.content,b.dateandtime,"
					+ "b.upload FROM board b  WHERE 1=1";

			accessStmt = accessConn.createStatement();
			accessRs = accessStmt.executeQuery(sql);

			logger.debug("準備查詢Access DB數據源數據...");

			int i = 1;
			while (accessRs.next()) {
				logger.debug("\n--文章導入中..." + (i++));

				oracleStmt = null;
				oracleRs = null;
				oracleStmt = oracleConn.prepareStatement("INSERT INTO "
						+ "archive(id,typeid,title,sender,sendtime"
						+ ") VALUES(" + "(select (nvl(max(to_number(id)), 0)) "
						+ "from archive)+1" + ",?,?,?,?)",
						Statement.RETURN_GENERATED_KEYS);

				logger.debug("\n---------欄目TYPEID---------0" + "\n");

				oracleStmt.setInt(1, 52);
				String title = accessRs.getString("title");
				if (null != title) {
					oracleStmt.setString(2, title);
				} else {
					continue;
				}
				oracleStmt.setString(3, accessRs.getString("upload"));
				oracleStmt
						.setTimestamp(4, accessRs.getTimestamp("dateandtime"));
				oracleStmt.executeUpdate();

				oracleRs = oracleStmt.getGeneratedKeys();
				if (oracleRs.next()) {
					autoIncKeyFromApi = oracleRs.getObject(1);
					if (autoIncKeyFromApi instanceof oracle.sql.ROWID) {
						autoIncKeyFromApi = ((oracle.sql.ROWID) autoIncKeyFromApi)
								.stringValue();
						String keySql = "SELECT ID FROM archive WHERE rowid=?";
						oracleStmt = null;
						oracleRs = null;
						oracleStmt = oracleConn.prepareStatement(keySql,
								Statement.RETURN_GENERATED_KEYS);
						oracleStmt.setObject(1, autoIncKeyFromApi);
						oracleRs = oracleStmt.executeQuery();
						if (oracleRs.next()) {
							autoIncKeyFromApi = oracleRs.getInt(1);
							// // 源數據中的ID同導入數據后生成的ID相對應
							// idCache.put((Integer) autoIncKeyFromApi, -1);
							String bolbSql = "UPDATE archive SET BODY=? WHERE ID=?";
							String content = accessRs.getString("content");
							if (null != content) {
								java.sql.Blob body = g.sql.SQLHelper
										.createBlob(content.getBytes("UTF-8"));
								Object[] args = new Object[] { body,
										autoIncKeyFromApi };
								SQLService ss = new SQLService();
								ss.update(bolbSql, args);
							} else {
								continue;
							}
						}
					} else {
						throw new RuntimeException(
								"操作失败,没有处理的Oracle RETURN_GENERATED_KEYS类型!");
					}
				}
			}
			logger.debug("\n---------重新讀取文章ID---------");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionPool.close(accRs);
			ConnectionPool.close(accSt);
		}
	}

	// ACCESS文章導入ORACLE數據庫3
	public void accessNewsToOracleArchive3() {
		Object autoIncKeyFromApi = -1;
		ResultSet accRs = null;
		Statement accSt = null;
		Statement accSts = null;
		ResultSet accRss = null;

		logger.debug("\n-----------\n" + idCache.size() + "\n--------");

		try {
			String sql = "SELECT r.reviewid,r.Content,r.author"
					+ ",r.title,r.updatetime FROM Review r WHERE 1=1";

			accessStmt = accessConn.createStatement();
			accessRs = accessStmt.executeQuery(sql);

			logger.debug("準備查詢Access DB數據源數據...");

			int i = 1;
			while (accessRs.next()) {
				logger.debug("\n--文章導入中..." + (i++));

				oracleStmt = null;
				oracleRs = null;
				oracleStmt = oracleConn.prepareStatement("INSERT INTO "
						+ "archive(id,typeid,title,sender,sendtime"
						+ ") VALUES(" + "(select (nvl(max(to_number(id)), 0)) "
						+ "from archive)+1" + ",(SELECT id FROM archive_type "
						+ "WHERE name='评论'),?,?,?)",
						Statement.RETURN_GENERATED_KEYS);

				logger.debug("\n---------欄目TYPEID---------0" + "\n");

				String title = accessRs.getString("title");
				if (null != title) {
					oracleStmt.setString(1, title);
				} else {
					continue;
				}
				oracleStmt.setString(2, accessRs.getString("author"));
				oracleStmt.setTimestamp(3, accessRs.getTimestamp("updatetime"));
				oracleStmt.executeUpdate();

				oracleRs = oracleStmt.getGeneratedKeys();
				if (oracleRs.next()) {
					autoIncKeyFromApi = oracleRs.getObject(1);
					if (autoIncKeyFromApi instanceof oracle.sql.ROWID) {
						autoIncKeyFromApi = ((oracle.sql.ROWID) autoIncKeyFromApi)
								.stringValue();
						String keySql = "SELECT ID FROM archive WHERE rowid=?";
						oracleStmt = null;
						oracleRs = null;
						oracleStmt = oracleConn.prepareStatement(keySql,
								Statement.RETURN_GENERATED_KEYS);
						oracleStmt.setObject(1, autoIncKeyFromApi);
						oracleRs = oracleStmt.executeQuery();
						if (oracleRs.next()) {
							autoIncKeyFromApi = oracleRs.getInt(1);
							// // 源數據中的ID同導入數據后生成的ID相對應
							// idCache.put((Integer) autoIncKeyFromApi, -1);
							String bolbSql = "UPDATE archive SET BODY=? WHERE ID=?";
							String content = accessRs.getString("content");
							if (null != content) {
								java.sql.Blob body = g.sql.SQLHelper
										.createBlob(content.getBytes("UTF-8"));
								Object[] args = new Object[] { body,
										autoIncKeyFromApi };
								SQLService ss = new SQLService();
								ss.update(bolbSql, args);
							} else {
								continue;
							}
						}
					} else {
						throw new RuntimeException(
								"操作失败,没有处理的Oracle RETURN_GENERATED_KEYS类型!");
					}
				}
			}
			logger.debug("\n---------重新讀取文章ID---------");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionPool.close(accRs);
			ConnectionPool.close(accSt);
		}
	}

	// ACCESS用戶導入ORACLE數據庫
	public void accessUserToOracleManager() {
		Object autoIncKeyFromApi = -1;
		ResultSet accRs = null;
		Statement accSt = null;
		Statement accSts = null;
		ResultSet accRss = null;

		logger.debug("\n-----------\n" + idCache.size() + "\n--------");

		try {

			accessStmt = accessConn.createStatement();

			accessRs = accessStmt
					.executeQuery("SELECT a.id,a.username,a.passwd,a.fullname"
							+ " FROM ft_user a WHERE 1=1 ");

			logger.debug("準備查詢Access DB數據源數據...");

			int i = 1;
			int b = 1;
			int s = 1;
			while (accessRs.next()) {
				logger.debug("\n--用戶記錄導入中..." + (i++));
				Integer aid = accessRs.getInt("id");
				String username = accessRs.getString("username");
				String passwd = accessRs.getString("passwd");
				String fullname = accessRs.getString("fullname");

				oracleStmt = null;
				oracleRs = null;
				oracleStmt = oracleConn.prepareStatement("INSERT INTO "
						+ "manager(id,userid,password,name) " + "VALUES("
						+ "(select (nvl(max(to_number(id)), 0)) "
						+ "from manager)+1" + ",?,?,?)",
						Statement.RETURN_GENERATED_KEYS);

				oracleStmt.setString(1, username);
				oracleStmt.setString(2, passwd);
				oracleStmt.setString(3, fullname);
				oracleStmt.executeUpdate();

				nameCache.add(username);

				oracleRs = oracleStmt.getGeneratedKeys();
				if (oracleRs.next()) {
					autoIncKeyFromApi = oracleRs.getObject(1);
					if (autoIncKeyFromApi instanceof oracle.sql.ROWID) {
						autoIncKeyFromApi = ((oracle.sql.ROWID) autoIncKeyFromApi)
								.stringValue();
						String keySql = "SELECT ID FROM manager WHERE rowid=?";
						oracleStmt = null;
						oracleRs = null;
						oracleStmt = oracleConn.prepareStatement(keySql,
								Statement.RETURN_GENERATED_KEYS);
						oracleStmt.setObject(1, autoIncKeyFromApi);
						oracleRs = oracleStmt.executeQuery();
						if (oracleRs.next()) {
							autoIncKeyFromApi = oracleRs.getInt(1);
							// 源數據中的ID同導入數據后生成的ID相對應
							idCache.put((Integer) autoIncKeyFromApi, aid);
							logger.debug("\n===\nautoIncKeyFromApi : "
									+ autoIncKeyFromApi + "====aid : " + aid
									+ "\n===");
						}
					} else {
						throw new RuntimeException(
								"操作失败,没有处理的Oracle RETURN_GENERATED_KEYS类型!");
					}
				}

				logger.debug("\n---------重新讀取用戶ID---------");
			}

			oracleStmt = null;
			oracleRs = null;
			oracleStmt = oracleConn.prepareStatement("INSERT INTO "
					+ "manager(id,userid,password,name) " + "VALUES("
					+ "(select (nvl(max(to_number(id)), 0)) "
					+ "from manager)+1" + ",?,?,?)",
					Statement.RETURN_GENERATED_KEYS);

			oracleStmt.setString(1, "admin");
			oracleStmt.setString(2, "admin");
			oracleStmt.setString(3, "admin");
			oracleStmt.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionPool.close(accRs);
			ConnectionPool.close(accSt);
		}
	}

	// ACCESS統計數據導入ORACLE數據庫
	public void accessCountersToOracleCounters() {
		Object autoIncKeyFromApi = -1;
		ResultSet accRs = null;
		Statement accSt = null;
		Statement accSts = null;
		ResultSet accRss = null;

		logger.debug("\n-----------\n" + idCache.size() + "\n--------");

		try {

			accessStmt = accessConn.createStatement();

			accessRs = accessStmt
					.executeQuery("SELECT c.total,c.today,c.yesterday"
							+ ",c.month,c.bmonth FROM counters c WHERE 1=1 ");

			logger.debug("準備查詢Access DB數據源數據...");

			int i = 1;
			while (accessRs.next()) {
				logger.debug("\n--統計記錄導入中..." + (i++));

				oracleStmt = null;
				oracleRs = null;
				oracleStmt = oracleConn.prepareStatement("INSERT INTO "
						+ "counters(id,total,taday,yesterday,month,bmonth) "
						+ "VALUES(" + "(select (nvl(max(to_number(id)), 0)) "
						+ "from counters)+1" + ",?,?,?,?,?)",
						Statement.RETURN_GENERATED_KEYS);

				oracleStmt.setInt(1, accessRs.getInt("total"));
				oracleStmt.setInt(2, accessRs.getInt("today"));
				oracleStmt.setInt(3, accessRs.getInt("yesterday"));
				oracleStmt.setInt(4, accessRs.getInt("month"));
				oracleStmt.setInt(5, accessRs.getInt("bmonth"));
				oracleStmt.executeUpdate();

				logger.debug("\n---------重新讀取統計ID---------");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionPool.close(accRs);
			ConnectionPool.close(accSt);
		}
	}

	// ACCESS權限導入ORACLE數據庫
	public void accessToOracleSys_group() {
		Object autoIncKeyFromApi = -1;
		ResultSet accRs = null;
		Statement accSt = null;
		Statement accSts = null;
		ResultSet accRss = null;

		logger.debug("\n-----------\n" + nameCache.size() + "\n--------");

		try {
			oracleStmt = null;
			oracleRs = null;
			oracleStmt = oracleConn.prepareStatement("INSERT INTO "
					+ "sys_group(id,name,arctypeadd,"
					+ "arctypesee,arctypeedit,arctypedel,arcadd,"
					+ "arcsee,arcedit,arcdel) " + "VALUES("
					+ "(select (nvl(max(to_number(id)), 0)) "
					+ "from sys_group)+1" + ",?,?,?,?,?,?,?,?,?)",
					Statement.RETURN_GENERATED_KEYS);

			oracleStmt.setString(1, "admin");
			oracleStmt.setInt(2, 1);
			oracleStmt.setInt(3, 1);
			oracleStmt.setInt(4, 1);
			oracleStmt.setInt(5, 1);
			oracleStmt.setInt(6, 1);
			oracleStmt.setInt(7, 1);
			oracleStmt.setInt(8, 1);
			oracleStmt.setInt(9, 1);
			oracleStmt.executeUpdate();

			oracleRs = oracleStmt.getGeneratedKeys();
			if (oracleRs.next()) {
				autoIncKeyFromApi = oracleRs.getObject(1);
				if (autoIncKeyFromApi instanceof oracle.sql.ROWID) {
					autoIncKeyFromApi = ((oracle.sql.ROWID) autoIncKeyFromApi)
							.stringValue();
					String keySql = "SELECT ID FROM sys_group WHERE rowid=?";
					oracleStmt = null;
					oracleRs = null;
					oracleStmt = oracleConn.prepareStatement(keySql,
							Statement.RETURN_GENERATED_KEYS);
					oracleStmt.setObject(1, autoIncKeyFromApi);
					oracleRs = oracleStmt.executeQuery();
					if (oracleRs.next()) {
						autoIncKeyFromApi = oracleRs.getInt(1);
						// 源數據中的ID同導入數據后生成的ID相對應
						idCache.put((Integer) autoIncKeyFromApi, -1);
						logger.debug("\n===\nautoIncKeyFromApi : "
								+ autoIncKeyFromApi + "=======");
					}
				} else {
					throw new RuntimeException(
							"操作失败,没有处理的Oracle RETURN_GENERATED_KEYS类型!");
				}
			}

			logger.debug("\n---------重新讀取權限ID---------");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionPool.close(accRs);
			ConnectionPool.close(accSt);
		}
	}

	// // ACCESS權限導入ORACLE數據庫
	// public void accessToOracleSys_group() {
	// Object autoIncKeyFromApi = -1;
	// ResultSet accRs = null;
	// Statement accSt = null;
	// Statement accSts = null;
	// ResultSet accRss = null;
	//		
	// logger.debug("\n-----------\n" + nameCache.size() + "\n--------");
	//		
	// try {
	// for (String username : nameCache) {
	//				
	// accessStmt = accessConn.createStatement();
	// accessRs = accessStmt.executeQuery("SELECT a.username"
	// + " FROM admin a WHERE a.username=" + username);
	//				
	// logger.debug("準備查詢Access DB數據源數據...");
	//				
	// int i = 1;
	// int b = 1;
	// int s = 1;
	// while (accessRs.next()) {
	// logger.debug("\n--權限記錄導入中..." + (i++));
	// String uname = accessRs.getString("username");
	//					
	// oracleStmt = null;
	// oracleRs = null;
	// oracleStmt = oracleConn.prepareStatement("SELECT m.id "
	// + " FROM manager m WHERE m.username=" + username,
	// Statement.RETURN_GENERATED_KEYS);
	//					
	// oracleStmt = null;
	// oracleRs = null;
	// oracleStmt = oracleConn.prepareStatement("INSERT INTO "
	// + "sys_group(id,userid,password,name) " + "VALUES("
	// + "(select (nvl(max(to_number(id)), 0)) "
	// + "from manager)+1" + ",?,?,?)",
	// Statement.RETURN_GENERATED_KEYS);
	//					
	// oracleStmt.setString(1, username);
	// oracleStmt.setString(2, passwd);
	// oracleStmt.setString(3, fullname);
	// oracleStmt.executeUpdate();
	//					
	// oracleRs = oracleStmt.getGeneratedKeys();
	// if (oracleRs.next()) {
	// autoIncKeyFromApi = oracleRs.getObject(1);
	// if (autoIncKeyFromApi instanceof oracle.sql.ROWID) {
	// autoIncKeyFromApi = ((oracle.sql.ROWID) autoIncKeyFromApi)
	// .stringValue();
	// String keySql = "SELECT ID FROM manager WHERE rowid=?";
	// oracleStmt = null;
	// oracleRs = null;
	// oracleStmt = oracleConn.prepareStatement(keySql,
	// Statement.RETURN_GENERATED_KEYS);
	// oracleStmt.setObject(1, autoIncKeyFromApi);
	// oracleRs = oracleStmt.executeQuery();
	// if (oracleRs.next()) {
	// autoIncKeyFromApi = oracleRs.getInt(1);
	// // 源數據中的ID同導入數據后生成的ID相對應
	// idCache.put((Integer) autoIncKeyFromApi, aid);
	// }
	// } else {
	// throw new RuntimeException(
	// "操作失败,没有处理的Oracle RETURN_GENERATED_KEYS类型!");
	// }
	// }
	//					
	// logger.debug("\n---------重新讀取用戶ID---------");
	// }
	// }
	// } catch (SQLException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } finally {
	// ConnectionPool.close(accRs);
	// ConnectionPool.close(accSt);
	// }
	// }

	public void close() {

		// try {
		// accRss.close();
		// accSts.close();
		// } catch (SQLException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// }
		//
		// try {
		// accRs.close();
		// accSt.close();
		// } catch (SQLException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// }
		//
		// try {
		// accessRs.close();
		// accessStmt.close();
		// } catch (SQLException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// }
		//
		// try {
		// accessConn.close();
		// } catch (SQLException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		//
		// try {
		// oracleConn.close();
		// } catch (SQLException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		AccessToOracle atoTest = new AccessToOracle();

		// 欄目導入
		atoTest.accessTypeToOracleChannel();

		// 文章導入
		atoTest.accessNewsToOracleArchive();
		atoTest.accessNewsToOracleArchive2();
		atoTest.accessNewsToOracleArchive3();

		// 用戶導入
		atoTest.accessUserToOracleManager();

		// 統計導入
		atoTest.accessCountersToOracleCounters();

		// 權限導入
		atoTest.accessToOracleSys_group();

		atoTest.close();
	}

}

 

 

如果有不明白的地方可以联系我:qq511134962,msn:myhongkongzhen@gmail.com

分享到:
评论

相关推荐

    access数据库MDB数据文件导入oracle数据库操作步骤.pdf

    在 PL/SQL 工具中,选择 ODBC 导入器工具,该工具可以用来将数据从其他数据库导入到 Oracle 数据库中。选择 ODBC 导入器后,弹出对话框,要求输入 Access 数据库的用户名和密码。由于 Access 数据库不需要用户名和...

    将Access数据导入到Oracle数据库小程序

    本教程主要关注的是如何将Access数据库中的数据导入到Oracle数据库中。这是一个重要的过程,因为Access通常用于小型或中型企业,而Oracle则常用于大型企业,它提供了更高级别的性能和可扩展性。 **Access与Oracle...

    如何将Access表中的数据导入到ORACLE中

    要将 Access 表中的数据导入到 ORACLE 中,首先需要在 Access 数据库驻留的机器上安装 ORACLE 客户端。然后,使用 NetManager 配置 ORACLE 服务名。接下来,在 Access 数据库驻留的机器上创建 DSN,连接 ORACLE ...

    access导入到oracle数据库工具

    本文将详细讲解如何使用特定的工具将Access数据库中的数据导入到Oracle数据库中,以及涉及到的相关技术点。 首先,Access和Oracle是两种不同类型的数据库管理系统。Access是由微软开发的关系型数据库系统,主要应用...

    把Acces数据库的数据导入到oracle数据库中

    将Access数据库的数据导入Oracle数据库是常见的数据迁移操作,这在数据整合、系统升级或数据分析时非常有用。以下将详细介绍两种方法来实现这个过程。 **方法一:使用PL/SQL Developer** 1. **创建Oracle环境**:...

    Access数据库数据导入Oracle服数据库

    Access数据库和Oracle数据库都是在各自领域中广泛应用的关系型数据库管理系统,但它们有着不同的架构和功能特点。Access通常用于小型企业或个人用户,而Oracle则服务于大型企业和组织,提供更高级别的性能、安全性和...

    DBF格式的数据导入oracle的流程

    因此,将 DBF 文件导入到 Oracle 数据库中成为一个非常重要的工作。本文将详细介绍如何将 DBF 文件通过 PL/SQL 导入到 Oracle 数据库中。 环境准备 在开始将 DBF 文件导入到 Oracle 数据库之前,我们需要确保 ...

    用plsql将mdb文件导入到oracle数据库中

    本文旨在详细介绍如何使用PL/SQL将MDB文件(Microsoft Access数据库)导入到Oracle数据库中的过程及相关技术点。 ### 一、概述 标题与描述都明确指出了本篇文章的核心内容:利用PL/SQL进行MDB文件向Oracle数据库的...

    INFORMATICA从ACCESS数据库导数据到ORACLE

    在这个场景中,我们将讨论如何使用INFORMATICA将数据从ACCESS数据库导出并导入到ORACLE数据库。 首先,我们需要在Windows操作系统中配置一个ODBC(Open Database Connectivity)数据源,这是连接到ACCESS数据库的...

    省市县数据库含access 如何转oracle

    根据Access数据库中的表结构,确定字段名、数据类型、主键和外键等,并在Oracle中使用SQL语句创建相应的表。 4. 数据导入:有多种方式可以将数据导入Oracle,包括SQL*Loader、SQL Developer、Data Pump、PL/SQL程序...

    关于将ACCESS数据库中的表导入到oracle的问题.pdf

    将ACCESS数据库中的表导入到Oracle是一项常见的数据迁移任务,尤其在不同数据库系统间的数据整合时。以下是关于这个过程的详细步骤和注意事项: 1. **导出ACCESS数据**:首先,你需要在ACCESS数据库中选择要导出的...

    access数据库转换工具

    Access数据库转换工具是一种专门用于将Microsoft Access数据库格式的数据迁移到其他数据库系统或文件格式的软件。Access数据库(.mdb或.accdb)虽然在小型企业和个人用户中广泛应用,但有时需要与不支持Access格式的...

    access数据库导入Oracle数据库的方法[汇编].pdf

    标题和描述中提到的“access数据库导入Oracle数据库的方法[汇编].pdf”,指的是如何将Microsoft Access数据库中的数据迁移到Oracle数据库中的过程。Access和Oracle是两种非常不同的数据库系统,前者是由微软公司开发...

    access 数据库 迁移 到 oracle 数据库,delphi2007

    今天需要把access数据库的内容迁移到oracle数据库,本来想每个表都做一个导入按钮的,后来想想这个办法太死板,经过一个白天的努力,写成这个可以快速迁移数据的软件,因为只是满足自己需要,所以很多方面没有完善,...

    c#批量导入excel数据到oracle数据库.rar

    2. **创建表结构**:根据Excel中的数据,可能需要先在Oracle数据库中创建对应的表结构。描述中提到的“建表的sql”就是为此准备的,你可以直接执行SQL脚本来创建表。 3. **读取Excel数据**:使用EPPlus或其他库打开...

    access数据库导入Oracle数据库的方法.pdf

    access数据库导入Oracle数据库的方法.pdf

    将文件导入到数据库中的方法

    本文主要介绍了将文件导入到数据库中的方法,特别是针对 Oracle 数据库的数据导入方法。文章首先介绍了 Sql*Loader 方法,它是 Oracle 数据库下数据导入的最重要的方法之一。接着介绍了使用专业的数据抽取工具,例如...

    eclipse与access数据库的简单连接

    Access数据库虽然在大型企业级应用中不常见,但在小型项目或教学环境中,它提供了一个轻量级、易于使用的数据库解决方案。下面我们将深入探讨如何在Eclipse中配置和使用Access数据库。 首先,你需要确保系统中安装...

Global site tag (gtag.js) - Google Analytics