自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

DB2數(shù)據(jù)庫從Excel中讀取數(shù)據(jù)舉例

數(shù)據(jù)庫
您是否遇到過需要從Excel中讀取數(shù)據(jù)呢?將數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫保存,最終可以用報表讀取數(shù)據(jù)庫并顯示,比較簡單,本文為您舉例了DB2數(shù)據(jù)庫從從Excel中讀取數(shù)據(jù)并導(dǎo)入,供您參考,希望能對您有所啟迪。

本文為您舉例了DB2數(shù)據(jù)庫從從Excel中讀取數(shù)據(jù)并導(dǎo)入,例子的功能有兩個:一是POI讀取Excel,二是DB2數(shù)據(jù)庫的連接和SQL執(zhí)行。供您參考,希望能對您有所啟迪。

您是否遇到過需要從Excel中讀取數(shù)據(jù)呢?將數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫保存,最終可以用報表讀取數(shù)據(jù)庫并顯示,比較簡單。查了一下兩者的資料,覺的也無所謂,需要的功能比較簡單,只要能讀取Excel數(shù)據(jù)就行,***決定使用POI,比較信任apache,使用果然很簡單,上網(wǎng)找了個源碼,Copy過來稍微改改就能用了。

下面附的代碼是一個Demo,功能有兩個:一是POI讀取Excel,二是DB2數(shù)據(jù)庫的連接和SQL執(zhí)行。
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";
//加載驅(qū)動程序以連接數(shù)據(jù)庫try
{//添加類庫驅(qū)動包db2jcc.jar和db2jcc_license_cu.jarClass.forName
("com.ibm.db2.jcc.DB2Driver");
url = "JDBC:db2://192.168.0.1:50000/dbname";#p#
//添加類庫驅(qū)動包db2java.jar//
Class.forName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();
//url = "jdbc:db2:njtcdata";
conn = DriverManager.getConnection(url,
username, password);stmt = conn.createStatement();}
//捕獲加載驅(qū)動程序異常catch
(ClassNotFoundException cnfex)
{System.err.println("裝載JDBC驅(qū)動程序失敗。");
cnfex.printStackTrace();return false;}
//捕獲連接數(shù)據(jù)庫異常catch (SQLException sqlex)
{System.err.println("無法連接數(shù)據(jù)庫");
sqlex.printStackTrace();//System.exit(1);
// terminate programreturn 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;#p#
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("數(shù)據(jù)導(dǎo)入成功");
elseSystem.out.println("數(shù)據(jù)導(dǎo)入失敗");
}else{System.out.println("數(shù)據(jù)庫連接失敗");}}}

 

責(zé)任編輯:段燃 來源: 賽迪網(wǎng)
相關(guān)推薦

2011-03-15 11:19:45

DB2數(shù)據(jù)庫catalog

2010-09-01 10:17:14

DB2日志

2010-08-26 11:32:25

DB2時間函數(shù)

2010-09-01 15:23:59

DB2字段類型

2010-08-25 10:50:48

DB2數(shù)據(jù)庫

2011-03-11 16:02:03

DB2數(shù)據(jù)庫安裝

2010-08-26 16:15:25

DB2數(shù)據(jù)庫管理

2010-11-01 11:30:41

DB2數(shù)據(jù)庫權(quán)限

2010-09-30 11:49:21

DB2數(shù)據(jù)庫權(quán)限

2010-11-03 16:21:18

DB2數(shù)據(jù)庫授權(quán)

2010-08-04 16:18:48

DB2數(shù)據(jù)庫

2010-09-06 10:00:00

DB2數(shù)據(jù)庫

2010-09-01 13:38:41

DB2數(shù)據(jù)復(fù)制

2010-11-02 11:49:18

SQL SERVER連

2010-08-31 13:06:49

DB2數(shù)據(jù)庫

2010-08-31 11:26:59

DB2數(shù)據(jù)庫

2010-11-03 16:32:10

DB2創(chuàng)建數(shù)據(jù)庫

2009-07-06 17:34:26

遠(yuǎn)程復(fù)制DB2

2010-09-07 09:31:03

DB2數(shù)據(jù)庫鎖表

2010-08-31 14:04:49

DB2驅(qū)動
點贊
收藏

51CTO技術(shù)棧公眾號