package com.dhe.business;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.dhe.sourceDB.DatabaseSourceParamFactory;
import com.dhe.sourceDB.GetSourceDB;
import com.dhe.targetDB.GetTargetDB;
public class Business extends javax.swing.JFrame implements Runnable {
/**
*
*/
private static final long serialVersionUID = -2814878419565328292L;
private long inter;
private Thread th;
public Thread getTh() {
return th;
}
public void setTh(Thread th) {
this.th = th;
}
public long getInter() {
return inter;
}
public void setInter(long inter) {
this.inter = inter;
}
public Business(Thread th) {
this.setInter(DatabaseSourceParamFactory.getInterval());
this.setTh(th);
}
public void run() {
// 开始同步数据
operateSynchro();
}
@SuppressWarnings("static-access")
public void operateSynchro() {
while (true) {
// 调用同步用户的方法
startSynchroUser();
// 调用同步车辆的方法
startSynchroCar();
try {
th.sleep(inter);
} catch (InterruptedException e) {
}
operateSynchro();
}
}
// 同步用户的方法 同步用户的时候要判断该用户所在组中是否有车辆 如果有车辆 就同步 没有车辆就不同步
public void startSynchroUser() {
Connection sourceConn = GetSourceDB.getConnection();
PreparedStatement sourcePs = null;
ResultSet sourceRs = null;
ResultSet tempSourceRs = null;
ResultSet tempSourceCompanyRs = null;
Connection targetConn = GetTargetDB.getConnection();
PreparedStatement targetPs = null;
ResultSet targetRs = null;
ResultSet targetCompanyRs = null;
ResultSet targetCompanyKeyRs = null;
ResultSet targetUserKeyRs = null;
// 查找所有用户
String sourceSql = "select * from userInfo";
try {
sourcePs = sourceConn.prepareStatement(sourceSql);
sourceRs = sourcePs.executeQuery();
while (sourceRs.next()) {
String tempLoginName = sourceRs.getString("UserName");
String tempPassword = sourceRs.getString("userPassword");
int tempLoginCount = 0;
int tempLoginUserId = sourceRs.getInt("userID");
String targetSql = "select count(*) from dhe_user where nickname='"
+ tempLoginName + "' ";
targetPs = targetConn.prepareStatement(targetSql);
targetRs = targetPs.executeQuery();
int count = 0;
if (targetRs.next()) {
count = targetRs.getInt(1);
}
// 目标表中不存在该用户的时候
if (count <= 0) {
sourceSql = "select count(*) from vehicleinfo v,( select d.deviceId from userInfo u,deviceGroupUnderUserGroup d where u.groupId = d.groupId and u.userId="
+ tempLoginUserId
+ " ) k where v.deviceId = k.deviceId";
sourcePs = sourceConn.prepareStatement(sourceSql);
tempSourceRs = sourcePs.executeQuery();
if (tempSourceRs.next()) {
tempLoginCount = tempSourceRs.getInt(1);
}
// 该用户下有车辆则进行同步该用户
if (tempLoginCount > 0) {
String tempCompanyName = "";
sourceSql = " select distinct(v.company) from vehicleinfo v,(select d.deviceId from userInfo u,deviceGroupUnderUserGroup d where u.groupId = d.groupId and u.userId="
+ tempLoginUserId
+ ") k where v.deviceId = k.deviceId";
sourcePs = sourceConn.prepareStatement(sourceSql);
tempSourceCompanyRs = sourcePs.executeQuery();
// 查询一个和该用户对应的单位名称
if (tempSourceCompanyRs.next()) {
tempCompanyName = tempSourceCompanyRs
.getString("company");
}
// 如果查到单位名称
if (tempCompanyName != null
&& !"".equals(tempCompanyName)) {
int tempCompanyId = -1;
targetPs = targetConn
.prepareStatement("select id from dhe_enterprise where name='"
+ tempCompanyName + "'");
targetCompanyRs = targetPs.executeQuery();
// 根据单位名称找到所对应的单位ID
if (targetCompanyRs.next()) {
tempCompanyId = targetCompanyRs.getInt("id");
}
// 如果没查到所对应的单位ID 则需要插入一个单位并返回一个ID
if (tempCompanyId == -1) {
targetPs = targetConn
.prepareStatement("insert into dhe_enterprise(name) values('"
+ tempCompanyName + "')");
targetPs.executeUpdate();
targetCompanyKeyRs = targetPs
.getGeneratedKeys();
if (targetCompanyKeyRs.next()) {
tempCompanyId = targetCompanyKeyRs
.getInt(1);
}
}
// 开始把用户插入到目标表中
targetSql = "insert into dhe_user(ischk,nickname,passwd,userType,enterpriseID,isValid) values(1,'"
+ tempLoginName
+ "','"
+ tempPassword
+ "',103," + tempCompanyId + ",1)";
targetPs = targetConn.prepareStatement(targetSql);
targetPs.executeUpdate();
int tempUserId = -1;
targetUserKeyRs = targetPs.getGeneratedKeys();
if (targetUserKeyRs.next()) {
tempUserId = targetUserKeyRs.getInt(1);
}
// 插入默认的权限
for (int i = 0; i < 3; i++) {
// 车辆状态实时监控
if (i == 0) {
targetSql = "insert into dhe_user_module_relation(userId,moduleId,isVaild) values("
+ tempUserId + ",20,0) ";
}
// 车辆状态历史轨迹
if (i == 1) {
targetSql = "insert into dhe_user_module_relation(userId,moduleId,isVaild) values("
+ tempUserId + ",51,0) ";
}
// 车辆一览
if (i == 2) {
targetSql = "insert into dhe_user_module_relation(userId,moduleId,isVaild) values("
+ tempUserId + ",41,0) ";
}
// 车辆轨迹回放
if (i == 2) {
targetSql = "insert into dhe_user_module_relation(userId,moduleId,isVaild) values("
+ tempUserId + ",70,0) ";
}
targetPs = targetConn
.prepareStatement(targetSql);
targetPs.executeUpdate();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
GetSourceDB.closeObject(sourceConn, sourcePs, sourceRs);
GetTargetDB.closeObject(targetConn, targetPs, targetRs);
try {
if (tempSourceRs != null) {
tempSourceRs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (tempSourceCompanyRs != null) {
tempSourceCompanyRs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (targetCompanyRs != null) {
targetCompanyRs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (targetUserKeyRs != null) {
targetUserKeyRs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (targetCompanyKeyRs != null) {
targetCompanyKeyRs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 同步车辆的方法
public void startSynchroCar() {
Connection sourceConn = GetSourceDB.getConnection();
PreparedStatement sourcePs = null;
ResultSet sourceRs = null;
Connection targetConn = GetTargetDB.getConnection();
PreparedStatement targetPs = null;
ResultSet targetRs = null;
ResultSet targetCompanyRs = null;
ResultSet targetCompanyKeyRs = null;
String sourceSql = "";
String targetSql = "";
try {
sourceSql = "select plateNum ,company ,model from vehicleinfo";
sourcePs = sourceConn.prepareStatement(sourceSql);
sourceRs = sourcePs.executeQuery();
while (sourceRs.next()) {
String tempCarNo = sourceRs.getString("plateNum");
String tempCompanyName = sourceRs.getString("company");
String tempModel = sourceRs.getString("model");
// 判断目标数据库中有没该车辆
targetSql = "select count(*) from dhe_ca
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java多数据库数据同步例子.rar (28个子文件)
java多数据库数据同步例子
源码
DataSynchro.rar 1.75MB
DataSynchro
bin
ApplicationResources.properties 681B
com
dhe
business
Business.class 7KB
bean
targetUser.class 6KB
SourceUser.class 1KB
sourceDB
GetSourceDB.class 2KB
DatabaseSourceParamFactory.class 1KB
targetDB
DatabaseTargetParamFactory.class 882B
GetTargetDB.class 2KB
main
Test.class 539B
DataMain.class 972B
lib
sqljdbc.jar 247KB
mysql-connector-java-5.1.6-bin.jar 687KB
src
ApplicationResources.properties 681B
com
dhe
business
Business.java 10KB
bean
SourceUser.java 837B
targetUser.java 5KB
sourceDB
GetSourceDB.java 2KB
DatabaseSourceParamFactory.java 773B
targetDB
DatabaseTargetParamFactory.java 512B
GetTargetDB.java 2KB
main
Test.java 203B
DataMain.java 445B
lib
sqljdbc.jar 247KB
mysql.jar 687KB
.project 387B
.classpath 232B
编译后的直接运行bat文件就可以执行
dataSyn.rar 899KB
共 28 条
- 1
liumingyang88
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页