⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 orderdatabase.java

📁 短信开发用于文件交换处理转发的类模块
💻 JAVA
字号:
package com.pub.backserver.order;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.pub.berkeleydb.BklyDatabase;
import com.pub.berkeleydb.BklyEnv;

/**
 * @author cnyqaio@hotmail.com
 * @create 2007-7-16
 */

public class OrderDatabase extends BklyDatabase{
	
	private static Logger log = Logger.getLogger(OrderDatabase.class);
	
	public OrderDatabase() {
		super._rc = new OrderRecordCreator();
		super._recordClass = OrderEntity.class;
	}
	
	public int open(String name, BklyEnv env) {
		return open(name, env, false, false, false, true, true);
	}

	public int Insert(OrderEntity oe) {
		return this.put(oe);
	}

	public int Delete(OrderEntity oe) {
		return this.get(oe, true, true);
	}

	public int Query(OrderEntity oe) {
		return this.get(oe, true, false);
	}

	public int Update(OrderEntity oe) {
		String mobile = oe.getMobile();
		short provId = oe.getProvinceId();
		short operation = oe.getOperationId();
		int prodId = oe.getProdId();
		int gwId = oe.getGwId();
		int channelId = oe.getChannelId();
		int serviceCodeId = oe.getServiceCodeId();
		int que = Delete(oe);
		oe.setMobile(mobile);
		oe.setProvince(provId);
		oe.setOperation(operation);
		oe.setProdId(prodId);
		oe.setGwId(gwId);
		oe.setChannelId(channelId);
		oe.setServiceCodeId(serviceCodeId);
		return Insert(oe);
	}

	/**
	 * 文件格式:文本文件,字段分割符:\t 字段顺序:手机号(string)、运营商ID(short) 省ID(short)、
	 *                  产品ID(int)、网关ID(int) 渠道ID(int) 服务代码(String)
	 * 
	 * @param filename
	 *            String
	 * @return int
	 */
	public int loadFromFile(String filename) {
		FileReader fr = null;
		try {
			fr = new FileReader(filename);
		} catch (Throwable err) {
			if (log.isEnabledFor(Priority.ERROR)) {
				log.error(null, err);
			}
			if (fr != null) {
				try {
					fr.close();
				} catch (Throwable ex) {
				}
			}
			return -1;
		}

		BufferedReader be = new BufferedReader(fr);
		String line = null;
		int lineCount = 0, validCount = 0;
		OrderEntity oe = new OrderEntity();
		try {
			while ((line = be.readLine()) != null) {
				lineCount++;
				if (line.length() > 0) {
					String[] fds = line.split("\t");
					if (fds.length >= 4 && fds[0].length() <= 12
							&& fds[0].length() >= 11 && fds[1].length() > 0
							&& fds[2].length() > 0 && fds[3].length() > 0
							&& fds[4].length() > 0) {

						oe.setMobile(fds[0]);
						oe.setOperation((short) Integer.parseInt(fds[1]));
						oe.setProvince((short) Integer.parseInt(fds[2]));
						oe.setProdId(Integer.parseInt(fds[3]));
						oe.setGwId(Integer.parseInt(fds[4]));
						oe.setChannelId(Integer.parseInt(fds[5]));
						oe.setServiceCodeId(Integer.parseInt(fds[6]));

						this.Update(oe);

						validCount++;
					}

				}
				// else empty line
				if ((lineCount & 0x3fff) == 0 && log.isDebugEnabled())
					log.debug("have read file line " + lineCount);
			}

			if (log.isDebugEnabled()) {
				log.debug("loadFromFile lineCount=" + lineCount
						+ " validCount=" + validCount);
			}
		} catch (Throwable err) {
			if (log.isEnabledFor(Priority.ERROR)) {
				log.error(null, err);
			}
			return -2;
		}
		return validCount;
	}

	/**
	 * 从数据库里读入定购关系
	 * 
	 * @param driver
	 * @param url
	 * @param user
	 * @param pwd
	 * @param sql
	 * @return
	 */
	public int LoadFromDatabase(String driver, String url, String user,
			String pwd, String sql) {
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, pwd);
		} catch (Throwable err) {
			if (conn != null) {
				try {
					conn.close();
				} catch (Throwable ex) {
				}
			}
			conn = null;

			if (log.isEnabledFor(Priority.ERROR))
				log.error(null, err);

			return -1;
		}

		int count = 0;

		// load phase
		// String sql = "select vc2phase,numprovid,numcityid,numgwid from
		// smsv_phaseinfo";
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			count = Update(rs);
		} catch (Throwable err) {
			if (log.isEnabledFor(Priority.ERROR))
				log.error(null, err);
			return -2;
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Throwable e) {
				}
			}
			rs = null;

			if (stmt != null) {
				try {
					stmt.close();
				} catch (Throwable ex1) {
				}
			}
			stmt = null;

			try {
				conn.close();
			} catch (Throwable ex2) {
			}
			conn = null;
		}

		return count;
	}

	/**
	 * 
	 * @param rs ResultSet
	 *            字段次序必须:手机号(string)、运营商ID(short) 省ID(short)、
	 *                 产品ID(int)、网关ID(int) 渠道ID(int) 服务代码(String)
	 * @return int > 0 updated rows; < 0 error
	 */
	public int Update(ResultSet rs) {
		int count = 0;
		OrderEntity oe = new OrderEntity();

		try {
			while (rs.next()) {
				try {
					oe.setMobile(rs.getString(1));
					oe.setOperation(rs.getShort(2));
					oe.setProvince(rs.getShort(3));
					oe.setProdId(rs.getInt(4));
					oe.setGwId(rs.getInt(5));
					oe.setChannelId(rs.getInt(6));
					oe.setServiceCodeId(rs.getInt(7));

					this.Insert(oe);
					log.debug("count : " + count + " Insert : " + oe.getMobile());

					count++;

				} catch (Throwable ex) {
					if (log.isEnabledFor(Priority.ERROR)) {
						log.error(null, ex);
					}
				}

			}
		} catch (Throwable ex) {
			if (log.isEnabledFor(Priority.ERROR)) {
				log.error(null, ex);
			}
			return -1;
		}
		log.debug("Insert total : " + count);
		return count;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -