乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 将excel中的数据导入DB2数据库有哪些方法呀

将excel中的数据导入DB2数据库有哪些方法呀

作者:乔山办公网日期:

返回目录:excel表格制作


1,bat文件(替换方括号中的内容)
db2 connect to [数据库别copy名] user [用户名] using [密码]
db2 import from 'D:\Temp\backup.del' of del insert_update into [表名(字段列表)]
db2 connect reset

2,调用
db2cmd bat文件

注意事项:
1,目标表需要定义主键
2,数据文件的字段顺序需要与语句中的字段顺序一一对应
3,若数据文件较大,需要在import命令增加COMMITCOUNT的选项
4,db2cmd的调用需要确认是否配置了环境变量,否则需要在db2客户端的目录下执行

先将Excel文件打开,然后保存为scv格式。
再在DB2命令行处理器中,使用import命令将数据导入数据库:
import from "d:\table.csv" OF DEL messages "d:\msg.out" INSERT INTO table 。

DB2的控制中心应该也能支持导入csv文件的。
在DB2数据库中,在导出DEL文件时,默认的字符分隔符是"、字段分隔符是, (逗号)。有一个需求是要为Oracle数据库提供数据,因此就想使用“|”作为数据的字段分隔符。在查看了export的使用说明后,发现export的file-mod模式中可以通过CHARDELx和COLDELx,分别设置字符分隔符和字段分隔符。
注意:导出文件的分隔符是和数据库的代码页有关联的,即在代码页为819的数据库下,可以使用任何的字符(ASCII码)作为数据的分隔符,但是代码页为1386的数据库只能使用ASCII码值在0x00 - 0x3F 范围内的字符。

下面附的代码是一个Demo,功能有两个:一是POI读取Excel,二是DB2数据库的连接和SQL执行。

import Java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class POITest {
private static Connection conn = null;
private static Statement stmt = null;
private static boolean connectDB2() {
String url = "";
String username = "username";
String password = "password";
//加载驱动程序以连接数据库
try {
//添加类库驱动包e799bee5baa6e78988e69d83339db2jcc.jar和db2jcc_license_cu.jar
Class.forName("com.ibm.db2.jcc.DB2Driver");
url = "JDBC:db2://192.168.0.1:50000/dbname";
//添加类库驱动包db2java.jar
//Class.forName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();
//url = "jdbc:db2:njtcdata";
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
}
//捕获加载驱动程序异常
catch (ClassNotFoundException cnfex) {
System.err.println("装载JDBC驱动程序失败。");
cnfex.printStackTrace();
return false;
}
//捕获连接数据库异常
catch (SQLException sqlex) {
System.err.println("无法连接数据库");
sqlex.printStackTrace();
//System.exit(1); // terminate program
return false;
}
return true;
}
private static boolean readExcelToDB2() {
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
try {
fs = new POIFSFileSystem(new FileInputStream("c:\\test.xls"));
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
return false;
}
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;
String name = "";
int id = 0;
int rowNum, cellNum;
int i;
rowNum = sheet.getLastRowNum();
for (i = 0; i <= rowNum; i++) {
row = sheet.getRow(i);
//cellNum = row.getLastCellNum();
cell = row.getCell((short) 0);
name = cell.getStringCellValue();
cell = row.getCell((short) 1);
id = (int) cell.getNumericCellValue();
String sql = "insert into TEST(ID, NAME) values(" + id + ",'" + name + "')";
try {
stmt.executeUpdate(sql);
} catch (SQLException e1) {
e1.printStackTrace();
return false;
}
}
return true;
}
public static void main(String[] args) {
if (connectDB2()==true){
if (readExcelToDB2()==true)
System.out.println("数据导入成功");
else
System.out.println("数据导入失败");
}
else{
System.out.println("数据库连接失败");
}
}
}

相关阅读