java excel導入oracle數據庫


參考:http://blog.csdn.net/qq_40576686/article/details/78618846,只做了一些小的修改。

java把excel數據寫入Oracle數據庫,此處需要引入兩個文件:ojdbc14.jar,jxl-2.6.10.jar,,excel文件內容與數據庫內表字段對應,此處注意:(excel版本太高了 報錯:Exception in thread "main" jxl.read.biff.BiffException: Unable to recognize,把文件保存成excel 97-2003即可)

 

另外,excel中數據量有些多時,代碼循環體內,不能帶有數據庫連接的創建及關閉,否則會導致連接錯誤:

Listener refused the connection with the follow
ing error:
ORA-12519, TNS:no appropriate service handler found
The Connection descriptor used by the client was:


/**
* excel數據導入到oracle
* @author
*
*/
public class excelToOracle {
public static void main(String[] args) throws Exception {

excelToOracle in = new excelToOracle();

// excel文件路徑,數據庫表名
in.insert("D:/BM_SPFL_TEST.xls","BM_SPFL");

}

/**
*
* @param path
* 要解析的excel文件路徑
* @param dataTable
* 要寫入到數據庫中的表名
* @throws BiffException
* @throws IOException
* @throws SQLException
*/
public void insert(String path,String dataTable) throws Exception{

File file = new File(path);
// 創建新的Excel 工作簿
Workbook rwb = null;
rwb = Workbook.getWorkbook(file);

// 得到工作簿中的第一個表索引即為excel下的sheet1,sheet2,sheet3...
Sheet sheet = rwb.getSheets()[0];
int rsColumns = sheet.getColumns();// 列數
int rsRows = sheet.getRows();// 行數
String simNumber = "" ;//每個單元格中的數據

DBUtils jdbc=new DBUtils();
// 獲取數據庫連接,避免循環體內多次調用、關閉連接,導致報數據庫連接錯誤
jdbc.getConnection();


String str="";//拼接要插入的列
for (int j = 0; j <rsColumns; j++) {
Cell cell = sheet.getCell(j, 0);
simNumber = cell.getContents();
if(j==rsColumns-1){
str += simNumber ;
}else{
str += simNumber+",";
}

}
for (int i = 1; i < rsRows; i++) {

String sql = "insert into "+dataTable+"("+str+") values(";//拼接sql
System.out.println(str);
for (int j = 0; j < rsColumns; j++) {
Cell cell = sheet.getCell(j, i);
simNumber = cell.getContents();
if(j==rsColumns-1){
sql += "'"+ simNumber+"'" ;
}else{
sql +="'"+ simNumber+"',";
}

}
sql += " )";
jdbc.executeUpdate(sql);//執行sql
}
jdbc.closeStmt();
jdbc.closeConnection();
}

}

 


/**
* Oracle數據庫連接
*
* @author
*/
public class DBUtils {

private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;

/** Oracle數據庫連接 URL */
private final static String DB_URL = "jdbc:oracle:thin:@192.168.201.106:1521:orcl";

/** Oracle數據庫連接驅動 */
private final static String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";

/** 數據庫用戶名 */
private final static String DB_USERNAME = "xnw_dev";

/** 數據庫密碼 */
private final static String DB_PASSWORD = "xnw_dev";

/**
* 獲取數據庫連接
*
* @return
*/
public Connection getConnection() {
/** 聲明Connection連接對象 */
Connection conn = null;
try {
/** 使用 Class.forName()方法自動創建這個驅動程序的實例且自動調用DriverManager來注冊它 */
Class.forName(DB_DRIVER);
/** 通過 DriverManager的getConnection()方法獲取數據庫連接 */
conn = DriverManager
.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
stmt = conn.createStatement();
} catch (Exception ex) {
ex.printStackTrace();
}
return conn;
}

/**
* 查詢數據部分
*
* @return ResultSet
*/
public ResultSet executeQuery(String sqlStr) {
if (sqlStr == null || sqlStr.length() == 0)
return null;
try {
this.getConnection();
rs = stmt.executeQuery(sqlStr);
return rs;
} catch (SQLException ex) {
ex.printStackTrace();
return null;
}

}

/**
* 更新數據部分
*
* @return 更新是否成功
*/
public boolean executeUpdate(String sqlStr) {

if (sqlStr == null || sqlStr.length() == 0)
return false;
try {
// 此處及finally內,不進行建立數據庫連接及關閉,避免被循環調用時,多次創建占用數據庫連接導致報錯
//this.getConnection();
stmt.executeUpdate(sqlStr);
return true;
} catch (SQLException ex) {
ex.printStackTrace();
return false;
} finally {
/* try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}*/

}

}

public void closeStmt() {
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 關閉數據庫連接
*
* @param connect
*/
public void closeConnection() {
try {
if (conn != null) {
/** 判斷當前連接連接對象如果沒有被關閉就調用關閉方法 */
if (!conn.isClosed()) {
conn.close();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM