📄 phasedatabase.java
字号:
package com.pub.backserver.phase;
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;
/**
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Copyright: Copyright (c) 2005
* </p>
*
* <p>
* Company:
* </p>
*
* @author not attributable
* @version 1.0
*/
public class PhaseDatabase extends BklyDatabase {
private static Logger log = Logger.getLogger(PhaseDatabase.class);
public PhaseDatabase() {
super._rc = new PhaseRecordCreator();
super._recordClass = PhaseEntity.class;
}
public int open(String name, BklyEnv env) {
return open(name, env, false, false, false, true, true);
}
public int Insert(PhaseEntity pr) {
return this.put(pr);
}
public int Delete(PhaseEntity pr) {
return this.get(pr, true, true);
}
public int Query(PhaseEntity pr) {
if (pr.bytesPhase()[0] == new Integer('0').byteValue()) {
for (int i = 4; i < PhaseEntity.PackageLength; i++)
pr.bytesPhase()[i] = 0;
int ret = this.get(pr, true, false);
if (ret == -3)// not found
{
pr.bytesPhase()[3] = 0;
ret = this.get(pr, true, false);
}
return ret;
}
return this.get(pr, true, false);
}
public int Update(PhaseEntity pr) {
String mobile = pr.getStrPhase();
short provID = pr.getProvID();
short cityID = pr.getCityID();
short gwID = pr.getGwID();
int que = Delete(pr);
pr.setStrPhase(mobile);
pr.setProvID(provID);
pr.setCityID(cityID);
pr.setGwID(gwID);
return Insert(pr);
}
/**
* 文件格式:文本文件,字段分割符:\t 字段顺序:号段(string)、省ID(int)、城市ID(short)、网关ID(short)、运营商ID(int)
*
* @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;
PhaseEntity pe = new PhaseEntity();
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() >= 9 && fds[1].length() > 0
&& fds[1].charAt(0) != '0') {
pe.setStrPhase(fds[0]);
pe.setProvID((short) Integer.parseInt(fds[1]));
pe.setCityID((short) Integer.parseInt(fds[2]));
pe.setGwID((short) Integer.parseInt(fds[3]));
pe.setOpID(Integer.parseInt(fds[4]));
this.Update(pe);
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;
}
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)、provid(short)、cityID(short)、gwID(short)
* @return int > 0 updated rows; < 0 error
*/
public int Update(ResultSet rs) {
int count = 0;
PhaseEntity pr = new PhaseEntity();
try {
while (rs.next()) {
try {
pr.setStrPhase(rs.getString(1));
pr.setProvID(rs.getShort(2));
pr.setCityID(rs.getShort(3));
pr.setGwID((short)1);
pr.setOpID(rs.getInt(4));
this.Insert(pr);
log.debug("count : " + count + " Insert : " + pr.getStrPhase());
count++;
// if(count > super.count())
// {
// count--;
// log.info(pr.strPhase());
// }
} 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 + -