首先数据库迁移的IP要配置正确,旧的–>新的
其次,数据库迁移要保证新、旧字段的匹配,要每个对应。
public static void main(String[] args) {
Connection con_origin = null;
Connection con_destination = null;
ResultSet rs = null;
Statement st_ori = null;
Statement st_des = null;
//这里会用到try-catch-finally
Class.forName("org.mariadb.jdbc.Driver");
con_origin = DriverManager.getConnection("jdbc:mysql://旧IP地址:3306/tdb?useUnicode=true&characterEncoding=UTF-8", "root", "123456");
con_destination = DriverManager.getConnection("jdbc:mysql://新IP地址:3306/config_cbtc2_atp?useUnicode=true&characterEncoding=UTF-8", "root", "123456");
st_ori = con_origin.createStatement();
st_des = con_destination.createStatement();
// 迁移TestTable工程表数据
transferTestTable(rs,st_ori,st_des);
数据迁移的方法及格式:
private static void transferTestTable(ResultSet rs,Statement st_ori,Statement st_des){
int i = 0;
try {
String query_sql = " select * from linetable ";
rs = st_ori.executeQuery(query_sql);
while (rs.next()) {
// 将查询出的内容添加到list中,其中userName为数据库中的字段名称
//System.out.println(rs.getString("name"));
String del_sql = " delete from linetable where id = "+"'"+rs.getString("id")+"'";
st_des.execute(del_sql);
String insert_sql = " insert into artable(id,creator,gmt_create,is_deleted,"
+ "project_id,"
+ "sortud,"
+ "atp_device_no,"
+ "n_id_track1,"
+ "n_id_track2,"
+ "n_id_track3,"
+ "n_id_track4) values("+"'"+rs.getString("id")+"',"
+"'"+rs.getString("creator")+"',"
+"'"+rs.getString("create_time")+"',"
+"0,"
+"'"+rs.getString("projectid")+"',"
+"'"+rs.getString("SortUD")+"',"
+"'"+rs.getString("NID_AR_AREA")+"',"
+"'"+rs.getString("NID_LINE")+"',"
+"'"+rs.getString("N_TRACK")+"',"
+"'"+rs.getString("NID_TRACK1")+"',"
+"'"+rs.getString("NID_TRACK2")+"',"
+"'"+rs.getString("NID_TRACK3")+"',"
+"'"+rs.getString("NID_TRACK4")+"'"
+") ";
//System.out.println(insert_sql);
st_des.execute(insert_sql);
i++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("插入line数据条数为"+i+"条。");
}
注意:在上述迁移的过程中
遇到的问题:
1、报错数据不匹配(查看insert和values后的内容是一一对应的);
报错数据为空(加保护程序if-else。判断为空时和不为空时应该执行的语句);
报错空指针(该问题复杂,建议打印输出看结果在做相应的处理);
报错格式问题(1、insert最后没有分号;2、insert最后之前的引号中要加入逗号【,】;3、values 最后一个getString引号中没有逗号;4、values最后之前的getString引号中有逗号【,】;)
2、出现组合的情况时,可以使用借用德摩根定律。
德摩根定律:出现多种情况可以用if-else的嵌套;
3、有些变量值会有其他的问题可以单独处理;
例如:
String ATTR_uSTOP="";
String ATTR_dSTOP="";
while (rs.next()) {
// 将查询出的内容添加到list中,其中userName为数据库中的字段名称
//System.out.println(rs.getString("name"));
String ATTR_uSTOP="";
String ATTR_dSTOP="";
String del_sql = " delete from balisetable where id = "+"'"+rs.getString("id")+"'";
st_des.execute(del_sql);
String insert_sql = "";
if (null == rs.getString("SortUD")){
insert_sql = " insert into balisetable(id,creator,gmt_create,is_deleted,"
+ "project_id,"
+ "sortud,"
+ "atp_device_no,"
+ "attrustop,"
+ "attrdstop,"
+ "m_version_bal) values("+"'"+rs.getString("id")+"',"
+"'"+rs.getString("creator")+"',"
+"'"+rs.getString("create_time")+"',"
+"0,"
+"'"+rs.getString("ProjectID")+"',"
+"0,"
+"'"+rs.getString("NID_BALISE")+"',"
+"'"+rs.getString("ATTR_uSTOP")+"',"
+"'"+rs.getString("ATTR_dSTOP")+"',"
+"'"+rs.getString("M_VERSIONBAL")+"'"
+") ";
}
else {
if(null == rs.getString("ATTR_uSTOP")||rs.getString("ATTR_uSTOP")==""){
ATTR_uSTOP="'"+rs.getString("ATTR_uSTOP")+"',";
}
else{
if(rs.getString("ATTR_uSTOP").equals("1")){
ATTR_uSTOP="'"+"0x0001"+"',";
}
else{
ATTR_uSTOP="'"+rs.getString("ATTR_uSTOP")+"',";
}
}
if(null == rs.getString("ATTR_dSTOP")||rs.getString("ATTR_dSTOP")==""){
ATTR_dSTOP="'"+rs.getString("ATTR_dSTOP")+"',";
}
else{
if(rs.getString("ATTR_dSTOP").equals("2")){
ATTR_dSTOP="'"+"0x0002"+"',";
}
else{
ATTR_dSTOP="'"+rs.getString("ATTR_dSTOP")+"',";
}
}
insert_sql = " insert into balisetable(id,creator,gmt_create,is_deleted,"
+ "project_id,"
+ "sortud,"
+ "atp_device_no,"
+ "attrustop,"
+ "attrdstop,"
+ "m_version_bal) values("+"'"+rs.getString("id")+"',"
+"'"+rs.getString("creator")+"',"
+"'"+rs.getString("create_time")+"',"
+"0,"
+"'"+rs.getString("ProjectID")+"',"
+"'"+rs.getString("SortUD")+"',"
+"'"+rs.getString("NID_BALISE")+"',"
+ATTR_uSTOP
+ATTR_dSTOP
+"'"+rs.getString("M_VERSIONBAL")+"'"
+") ";
}
//System.out.println(ATTR_uF);
st_des.execute(insert_sql);
i++;
}
}
4、新旧导出的Excel表有其中两行的某些字段不一致:分三步检查,检查前端、数据库(数据的查询操作)、后台。前端显示一致,而后台的新旧代码逻辑一致,问题出现在数据库中,数据虽然一致,但是,排序的依据一致,但是根据排序的依据检查,发现数据库中的两组数据不一致,一种是int型排序,另一种是字符串排序。int排序按照常规的大小;字符串排序根据第一位排序例如:1,10,11;之后再看第一位是2的。
解决方法:字符串+0 强制转化为数字型,这样就可以按照int的方法比较了。
5、数据库中为灰色的(Null),赋值时直接可以赋值为大写NULL;
6、进制转换问题
//十六进制转换为十进制
cell.setCellValue(Integer.parseInt(po.getAttrDStop().replaceAll("0x", ""),16));
//进制转换注意长度可能超过,要是用Long,Integer.valueOf和他的区别,trim()是删除前后的空格,Long.parseLong
项目心得:
1、首先解决问题要一个一个来,不建议合并解决,当然类似问题可以这样处理;
2、查找不出原因的时候可以采用跟代码、debug的调试方式来查找问题的根源;
3、注意代码中的细节问题,例如:标点符号、名称的一致性、逻辑关系,同结构的代码复制的时候注意修改不一样的地方。总之归结为细节问题;
4、注意报错的类型,根据报错提示去修改代码。