工作中,有的存储过程里调用:
INSERT IGNORE INTO `box_phone_warning_imei` SELECT * FROM `box_log`.`box_local_phone_log` WHERE id <> v_id and imei = v_group and (mac <> v_group2 or createTime<>v_group3);
现在为了用table里的字段名 替换* ,达到下面的结果,以避免box_phone_warning_imei和box_local_phone_log字段不一致时导致数据拷贝失败。 INSERT IGNORE INTO `box_phone_warning_imei` SELECT id,imei,mac,phoneNum,phoneModel,phoneVendor,appSuccessNum,appSuccessIds,appSuccessNames,appFalseNum,appFalseIds,appFalseNames,boxId,channelId,fatherChannelId,province,city,createTime,updateTime,statType,success,errorMsg,albumId,address,realIp FROM `box_log`.`box_local_phone_log` WHERE id <> v_id and imei = v_group and (mac <> v_group2 or createTime<>v_group3);
?
面对以上这么简单的需求不可能手动去,数据表里一个字段一个字段的copy下来组合成一个string吧,这样体力活不应该是猿人干的。用jdbc来帮忙吧。
?
package jdbc.test; import java.sql.*; /** * @author jone * */ public class JDBCDemo { static String user = "root"; static String password = "88888888"; static String url = "jdbc:mysql://localhost:3306/box_log_copy"; static String tableName=""; static String driver = "com.mysql.jdbc.Driver"; static Connection con = null; static { try { Class.forName(driver); initInfo(2); con = DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); } } //想切换不同库里的表,配置以下url 和tableName就OK了。 private static void initInfo(int type) { switch (type) { case 1: url = "jdbc:mysql://localhost:3306/box_oms_copy"; tableName = "box_local_phone_sale_sum"; break; case 2: url = "jdbc:mysql://localhost:3306/box_log_copy"; tableName = "box_local_test_log"; break; default: url = "jdbc:mysql://localhost:3306/box_log_copy"; tableName="box_mobile_log"; break; } } /** * @param args */ public static void main(String[] args) { String sqlstr = "select * from "+tableName; querySql(tableName, sqlstr); } private static void querySql(String tableName, String sqlstr) { Statement stmt = null; ResultSet rs = null; try{ stmt = con.createStatement(); // sqlstr = "insert into "+tableName+" values ('20140113','jone',01)"; // stmt.executeUpdate(sqlstr); rs = stmt.executeQuery(sqlstr); StringBuilder sb=new StringBuilder(); ResultSetMetaData rsmd = rs.getMetaData(); int j = 0; j = rsmd.getColumnCount(); for(int k = 0; k<j; k++) { // System.out.print(rsmd.getCatalogName(k+1)); System.out.print(rsmd.getColumnName(k+1)); System.out.print("\t"); sb.append(rsmd.getColumnName(k+1)).append(","); } System.out.println(); System.err.println(sb.deleteCharAt(sb.length()-1).toString()); // 以下注释打开后可以输出每行记录的值。 // while(rs.next()) // { // for(int i=0;i<j;i++) // { // System.out.print(rs.getString(i+1)); // System.out.print("\t"); // } // System.out.println(); // } }catch(SQLException e2) { System.out.println("数据库存在异常!"); System.out.println(e2.toString()); } finally { try