📄 insertrecordstodb.java
字号:
package com.unimas.dbsync.output;
import com.unimas.dbsync.entity.Recordbean;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.io.IOException;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2008-12-10
* Time: 11:17:20.
*/
/**
* 将文件中读出的数据插入数据库
* !!!!
*/
public class InsertRecordsToDb {
String TabName = "";//从配置文件中获取的目标端的表名
String[] ColumnName;//从配置文件中获取字段名
public void outInf(Recordbean rb, ReadFile rf) {
Connection conn = null;
PreparedStatement pstmt = null;
String[] records1;
String[] records2;
//(待改)将配置文件中的列名匹配 .
try {
// 从文件中读出所有记录和源端表的字段名
rf.readFile(rb);
String s1 = rb.getTabColumnName();
String s2 = rb.getRecords();
int count = rb.getCount();
records1 = s2.split("\n\n");
//得到需要在sql语句中使用的 ?个数
String[] str = new String[100];
StringBuffer strNum = new StringBuffer("?");
for (int i = 0; i < count - 1; i++) {
str[i] = ",?";
strNum.append(str[i]);
}
/**
* 运用循环插入全部记录信息
*/
String sql = "insert into studentcopy values(" + strNum + ")";
conn = TargetDataBase.getInstance().getConnection();//连接目标端数据库
pstmt = conn.prepareStatement(sql); //建立预编译的对象
for (int i = 0; i < records1.length; i++) {
records2 = records1[i].split("\n");
for (int j = 0; j < count; j++) {
pstmt.setString(j + 1, records2[j]);
}
pstmt.executeUpdate(); //进行预编译 将一组记录一组记录存入数据库中
}
//处理异常
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
TargetDataBase.getInstance().closeConnection(conn);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -