注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

断尘居

温柔的男人像海洋。

 
 
 
 
 

日志

 
 

数据导入(从oracle到mysql)  

2011-03-24 11:29:29|  分类: JDK |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {

public Connection getOracleConn() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");

String strCon = "jdbc:oracle:thin:@10.6.128.28:1521:ora10";
String username = "cofcodb";
String password = "cofcodb";
conn = DriverManager.getConnection(strCon, username,
password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public  Connection getMySqlConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://10.6.1.3/ultrax";
String username = "root";
String password = "root";
conn = DriverManager.getConnection(url,username,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.Test.db.DBConnection;
import com.Test.vo.Member;

public class TestSQL {

// 读取用户信息
@SuppressWarnings("unchecked")
public static List<Member> getUserInfo() {
DBConnection dBConnection = new DBConnection();
Connection conn = dBConnection.getOracleConn();
List<Member> list = new ArrayList();

try {
Statement stmt = conn.createStatement();
String qureyproduct = "select u.loginid username,u.email email from t_user u order by u.id";
ResultSet rs = stmt.executeQuery(qureyproduct);

Member member = null;
while (rs.next()) {
member = new Member();
member.setUsername(rs.getString("username"));
member.setEmail(rs.getString("email"));
list.add(member);
System.out.println(rs.getString("username") + "---->>>"
+ rs.getString("email") + "读取并添加到list成功");
}
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}

// 写入用户信息
public static void insertUserInfo() {
DBConnection dBConnection = new DBConnection();
Connection conn = dBConnection.getMySqlConn();
List<Member> list = getUserInfo();

try {
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();

for (int i = 0; i < list.size(); i++) {
Member member = (Member) list.get(i);
String username = member.getUsername();
String password = member.getPassword();
String email = member.getEmail();
System.out.println("username:" + username + " email:" + email);

StringBuffer sqlStr = new StringBuffer(
"insert into pre_ucenter_members"
+ "(username,password,email,salt) "
+ "values('" + replace(username)
+ "','526fb9e39602de12ab46eda89cb24b5c','"
+ replace(email) + "','a8cb2d')");

if (!"admin".equals(username) && !"zhaohe".equals(username)) {
stmt.execute(sqlStr.toString());
System.out.println(username + "写入ucenter表成功");
conn.commit();
}
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
System.out.println("DONE!");
}

// 过滤特殊字符
public static String replace(String str) {
if (str != null) {
str = str.replaceAll("\\\\", "vukjh");
if (isHave(str, "'")) {
str = str.replaceAll("'", "ert");
} else if (isHave(str, "\"")) {
str = str.replaceAll("\"", "bn");
} else if (isHave(str, " ")) {
str = str.replaceAll(" ", "fc");
} else if (isHave(str, "<")) {
str = str.replaceAll("<", "dfg");
} else if (isHave(str, ")")) {
str = str.replace(")", "dhgv");
} else if (isHave(str, "(")) {
str = str.replace("(", "kjh");
} else if (isHave(str, "(")) {
str = str.replace("(", "chs");
} else if (isHave(str, ")")) {
str = str.replace(")", "ajhk");
} else if (isHave(str, "#")) {
str = str.replaceAll("#", "hjk");
} else if (isHave(str, "$")) {
str = str.replaceAll("$", "vnm");
} else if (isHave(str, "%")) {
str = str.replaceAll("%", "erv");
} else if (isHave(str, "^")) {
str = str.replaceAll("^", "jkfg");
} else if (isHave(str, "&")) {
str = str.replaceAll("&", "yui");
} else if (isHave(str, "*")) {
str = str.replace("*", "vmjk");
} else if (isHave(str, "\\")) {
str = str.replaceAll("\\", "vukjh");
} else if (isHave(str, "/")) {
str = str.replace("/", "vukjh");
} else if (isHave(str, "\\\\")) {
str = str.replaceAll("\\\\", "vukjh");
} else if (isHave(str, "//")) {
str = str.replaceAll("//", "vu423h");
}
}
return str;
}

// 判断是否存在特殊字符
public static boolean isHave(String str, String s) {
if (str.indexOf(s) != -1) {
return true;
}
return false;
}

public static void main(String[] args) {
insertUserInfo();
}
}

  评论这张
 
阅读(3389)| 评论(2)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017