mysql自动转库_JAVA自动操作0racle数据库转mysql数据库
1.要注意數(shù)據(jù)編碼,在建表時(shí)就應(yīng)該設(shè)置好默認(rèn)編碼,我這里是UTF-8;
CREATE TABLE `CODE_ZYB` (
`uid` int(64) NOT NULL AUTO_INCREMENT,
`XSH` varchar(5) DEFAULT NULL,
`ZYH` varchar(8) DEFAULT NULL,
`XKMLH` varchar(2) DEFAULT NULL,
`ZYM` varchar(40) DEFAULT NULL,
`YWZYM` varchar(80) DEFAULT NULL,
`BZ` varchar(255) DEFAULT NULL,
`XJZYH` varchar(8) DEFAULT NULL,
`XJZYM` varchar(40) DEFAULT NULL,
PRIMARY KEY (`uid`)
) engine=myisam default charset=utf8; 注意(如果是GBK就把下面的utf8改為gbk):
engine=myisam default charset=utf8;
下面是java的代碼:
package jw;
import java.io.UnsupportedEncodingException;
import java.sql.*;
public class Jw1 {
public static void main(String[] args) throws UnsupportedEncodingException {
// TODO code application logic here
String url = "jdbc:oracle:thin:@host:1521:test"; //test為自己數(shù)據(jù)庫(kù)的名字,1521為端口
String username = "name";
String password = "password";
String driver = "oracle.jdbc.driver.OracleDriver";
try {
Class.forName(driver); //加載驅(qū)動(dòng)
Connection conn = DriverManager.getConnection(url, username,
password); //連接數(shù)據(jù)庫(kù)
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM JW.CODE_ZYB"); // 預(yù)處理命令,比Statement 好一些
ResultSet rs = pstmt.executeQuery();
Connection con = DriverManager
.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8"
,"user", "pass"); //mysql數(shù)據(jù)庫(kù)連接并且設(shè)置為UTF8默認(rèn)連接;
PreparedStatement sta = con.prepareStatement("set names utf8"); //連接后轉(zhuǎn)為UTF8編碼
sta.executeQuery(); //執(zhí)行sql;
PreparedStatement st = con.prepareStatement("TRUNCATE TABLE CODE_ZYB"); //執(zhí)行重復(fù)插入數(shù)據(jù)
st.executeUpdate(); //插入,刪除,增加使用executeupdate;
while(rs.next())
{
//System.out.println(rs.getString(1));
String sql = "INSERT INTO CODE_ZYB(`XSH`,`ZYH`,`XKMLH`"
+ ",`ZYM`,`YWZYM`,`BZ`,`XJZYH`,`XJZYM`)"
+ "VALUES (?,?,?,?,?,?,?,?)";
sta = con.prepareStatement(sql);
for(int i=1;i<9;i++){
String s = rs.getString(i);
if (s == null || s.isEmpty() ) //處理oracle過(guò)來(lái)的為空的數(shù)據(jù)
s = "";
else
s = new String(s.getBytes("UTF-8"), "UTF-8"); //數(shù)據(jù)編碼轉(zhuǎn)換
System.out.println(s+","+i);
sta.setString(i,s); //占位符處理;
}
sta.execute();
sta.close(); //關(guān)閉sql
}
pstmt.close(); //Oracle sql查詢(xún)關(guān)閉
conn.close(); //Oracle 連接關(guān)閉
con.close(); //Mysql 連接關(guān)閉
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
System.out.print(e.getMessage());
}
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/test","user", "pass");
connection.close();
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
總結(jié)
以上是生活随笔為你收集整理的mysql自动转库_JAVA自动操作0racle数据库转mysql数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: boostrap 鼠标滚轮滑动图片_Bo
- 下一篇: mysql数据库有几种删除方式_sql有