JDBC远程从一个MySql数据库中的一张表里面读出数据(这个数据库需要用SSH隧道连接,大约8W条数据),然后分别插入到另一个数据库中的两张表里
2014-08-20 19:23
931 查看
package com.eeepay.lzj.db; import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public class ChangeDB { public static int lport = 33102;//本地端口(随便取) public static String rhost = "172.***.***.***";//远程MySQL服务器 public static int rport = 3306;//远程MySQL服务端口 public static void go() { String user = "***";//SSH连接用户名 String password = "******";//SSH连接密码 String host = "120.132.***.***";//SSH服务器 int port = *****;//SSH访问端口 try { JSch jsch = new JSch(); Session session = jsch.getSession(user, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); System.out.println(session.getServerVersion());//这里打印SSH服务器版本信息 int assinged_port = session.setPortForwardingL(lport, rhost, rport); System.out.println("localhost:" + assinged_port + " -> " + rhost + ":" + rport); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String mobileNo;//mobile_username String accountName;//account_name String accountNo;//account_no String cnaps;//cnaps_no String bankName;//bank_name String realName;//lawyer int status;//open_status String password;//mobile_password String idCard;//id_card_no int realNameAuth;//real_flag Date createTime; go(); try { //1、加载驱动 Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //2、创建连接 Connection conn = null; Connection conn2 = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:33102/posp", "****", "***"); conn2 = DriverManager.getConnection("jdbc:mysql://115.29.***.***:3306/bag", "****", "*****"); } catch (SQLException e) { System.out.println("未连接上数据库"); e.printStackTrace(); } PreparedStatement pstmt = null; PreparedStatement pstmt2 = null; PreparedStatement pstmt22 = null; PreparedStatement pstmt3 = null; try { String sql = "select mobile_username,account_name,account_no,cnaps_no,bank_name,lawyer,open_status,mobile_password,id_card_no,real_flag,create_time from pos_merchant"; //String sql = "select * from settle_account where id=1"; String sql2 = "insert into bag_login(mobile_no,status,real_name,create_time,password,pay_password,idcard,real_name_auth) values(?,?,?,?,?,?,?,?)"; String sql22 = "select * from bag_login where mobile_no=?"; String sql3 = "insert into settle_account(mobile_no,account_name,account_no,cnaps,bank_name,create_time) values(?,?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt2 = conn2.prepareStatement(sql2); pstmt22 = conn2.prepareStatement(sql22); pstmt3 = conn2.prepareStatement(sql3); ResultSet rs = pstmt.executeQuery(); int i=1; while(rs.next()){ System.out.println(i++); createTime = rs.getDate("create_time"); mobileNo = rs.getString("mobile_username"); accountName = rs.getString("account_name"); accountNo = rs.getString("account_no"); cnaps = rs.getString("cnaps_no"); bankName = rs.getString("bank_name"); realName = rs.getString("lawyer"); status = rs.getInt("open_status"); password = rs.getString("mobile_password"); idCard = rs.getString("id_card_no"); realNameAuth = rs.getInt("real_flag"); pstmt3.setString(1, mobileNo); pstmt3.setString(2, accountName); pstmt3.setString(3, accountNo); pstmt3.setString(4, cnaps); pstmt3.setString(5, bankName); pstmt3.setDate(6, createTime); pstmt3.execute(); pstmt22.setString(1, mobileNo); if(!pstmt22.execute()){ pstmt2.setString(1, mobileNo); pstmt2.setInt(2, status); pstmt2.setString(3, realName); pstmt2.setDate(4, createTime); pstmt2.setString(5, password); pstmt2.setString(6, password); pstmt2.setString(7, idCard); pstmt2.setInt(8, realNameAuth); pstmt2.execute(); } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { pstmt.close(); conn.close(); pstmt2.close(); conn2.close(); pstmt3.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
相关文章推荐
- JDBC远程从一个MySql数据库中的一张表里面读出数据(这个数据库需要用SSH隧道连接
- 将数据库常用的操作(连接数据库,获得所有数据,获得一条记录,获得一列记录,获得一条索引数组,获得一条关联数组)都给封装到db.class.php里面,谁需要谁继承这个类 注意:提交封装的类文件
- SQL 把一个数据库里面的某张表里的数据插入到另外一个数据库里面的一张表
- 定时从远程的数据库中取数据,然后把取出来的数据插入或更新本地的oracle数据库的表
- JDBC 数据库连接 创建表格、插入、查询、删除、修改数据 基本操作
- JDBC应用程序连接数据库–插入数据并且获得相应自增长值的列(即主键id)
- JAVA操作数据库-->从一张表中取值,经过判断,然后插入另一张表中。
- java利用jdbc连接数据库之插入数据
- 用JDBC连接Vertica数据库后插入一行数据的例子源代码(接上篇)
- 编写操作数据库的JAVA程序时需要的连接MySQL数据库的JDBC连接包mysql-connector-java-5.1.10.zip怎么安装
- Java:jdbc连接数据库插入中文数据乱码问题
- java 使用jdbc连接mysql数据库插入数据乱码
- SQl:跨数据库从一个表的数据插入另一个数据库的表数据 生成语句进行插入
- SQL数据库中把一张表从一个数据库中插入到另外一个数据库的一张表里
- SQL SERVER 数据库 怎么从一个服务器一个表中把数据插入到另一个服务器中的一个表内(纯复制)
- SQL高级语句-SELECT INTO 从一个表中选取数据,然后把数据插入另一个表中
- JDBC应用程序连接数据库--插入数据并且获得相应自增长值的列
- mysql 从一个数据库数据表插入到另一个数据库数据表
- 使用JDBC连接MySQL数据库--典型案例分析(四)----更新和插入员工数据
- 03-JDBC连接MySQL数据库【插入数据】