• Java代码实现excel数据导入到Oracle


    1.首先需要两个jar包jxl.jar,ojdbc.jar(注意版本,版本不合适会报版本错误)
    2.代码:

    Java代码  Java代码实现excel数据导入到Oracle - 第1张  | IT江湖
    1. import java.io.File;  
    2. import java.io.FileInputStream;  
    3. import java.io.IOException;  
    4. import java.io.InputStream;  
    5. import jxl.Cell;  
    6. import jxl.Sheet;  
    7. import jxl.Workbook;  
    8. import jxl.read.biff.BiffException;  
    9. /** 
    10.  * excel数据导入到oracle 
    11.  * @author sh 
    12.  * 2010-05-11 
    13.  */  
    14. public class InsertData {  
    15.     public static void main(String[] args) throws Exception {  
    16.   
    17.         InsertData in = new InsertData();  
    18.         in.insert(“F:/myJob/hah.xls”,“information”);  
    19.   
    20.     }  
    21.   
    22.     /** 
    23.      *  
    24.      * @param path 
    25.      *            要解析的excel文件路径 
    26.       * @param dataTable 
    27.      *            要写入到数据库中的表名           
    28.      * @throws BiffException 
    29.      * @throws IOException 
    30.      */  
    31.     public void insert(String path,String dataTable) throws BiffException, IOException { 
    32.           
    33.         File file = new File(path);           
    34.         // 创建新的Excel 工作簿  
    35.         Workbook rwb = null;  
    36.         rwb = Workbook.getWorkbook(file);  
    37.           
    38.         // 得到工作簿中的第一个表索引即为excel下的sheet1,sheet2,sheet3…  
    39.         Sheet sheet = rwb.getSheets()[0];  
    40.         int rsColumns = sheet.getColumns();// 列数  
    41.         int rsRows = sheet.getRows();// 行数  
    42.         String simNumber = “” ;//每个单元格中的数据  
    43.           
    44.         DBUtils jdbc=new DBUtils();  
    45.           
    46.         String str=“”;//拼接要插入的列  
    47.             for (int j = 0; j <rsColumns; j++) {  
    48.                 Cell cell = sheet.getCell(j, 0);  
    49.                 simNumber = cell.getContents();  
    50.                 if(j==rsColumns-1){  
    51.                     str +=  simNumber  ;  
    52.                 }else{  
    53.                     str +=  simNumber+“,”;  
    54.                 }  
    55.                   
    56.             }  
    57.         for (int i = 1; i < rsRows; i++) {  
    58.               
    59.             String sql = “insert into “+dataTable+“(“+str+“) values(“;//拼接sql  
    60.             System.out.println(str);  
    61.             for (int j = 0; j < rsColumns; j++) {  
    62.                 Cell cell = sheet.getCell(j, i);  
    63.                 simNumber = cell.getContents();  
    64.                 if(j==rsColumns-1){  
    65.                     sql += “‘”+ simNumber+“‘” ;  
    66.                 }else{  
    67.                     sql +=“‘”+ simNumber+“‘,”;  
    68.                 }  
    69.                   
    70.             }  
    71.             sql += ” )”;  
    72.             jdbc.executeUpdate(sql);//执行sql  
    73.               
    74.         }  
    75.         jdbc.closeStmt();  
    76.         jdbc.closeConnection();  
    77.     }  
    78.   
    79. }  

    Util类

    Java代码  
      1. import java.sql.Connection;  
      2. import java.sql.DriverManager;  
      3. import java.sql.ResultSet;  
      4. import java.sql.SQLException;  
      5. import java.sql.Statement;  
      6.   
      7. /** 
      8.  * Oracle数据库连接 
      9.  *  
      10.  * @author sh 2010-05-11 
      11.  */  
      12. public class DBUtils {  
      13.   
      14.     private Connection conn = null;  
      15.     private Statement stmt = null;  
      16.     private ResultSet rs = null;  
      17.   
      18.     /** Oracle数据库连接 URL */  
      19.     private final static String DB_URL = “jdbc:oracle:thin:@localhost:1521:XE”;  
      20.   
      21.     /** Oracle数据库连接驱动 */  
      22.     private final static String DB_DRIVER = “oracle.jdbc.driver.OracleDriver”;  
      23.   
      24.     /** 数据库用户名 */  
      25.     private final static String DB_USERNAME = “test”;  
      26.   
      27.     /** 数据库密码 */  
      28.     private final static String DB_PASSWORD = “test”;  
      29.   
      30.     /** 
      31.      * 获取数据库连接 
      32.      *  
      33.      * @return 
      34.      */  
      35.     public Connection getConnection() {  
      36.         /** 声明Connection连接对象 */  
      37.         Connection conn = null;  
      38.         try {  
      39.             /** 使用 Class.forName()方法自动创建这个驱动程序的实例且自动调用DriverManager来注册它 */  
      40.             Class.forName(DB_DRIVER);  
      41.             /** 通过 DriverManager的getConnection()方法获取数据库连接 */  
      42.             conn = DriverManager  
      43.                     .getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);  
      44.             stmt = conn.createStatement();  
      45.         } catch (Exception ex) {  
      46.             ex.printStackTrace();  
      47.         }  
      48.         return conn;  
      49.     }  
      50.   
      51.     /** 
      52.      * 查询数据部分 
      53.      *  
      54.      * @return ResultSet 
      55.      */  
      56.     public ResultSet executeQuery(String sqlStr) {  
      57.         if (sqlStr == null || sqlStr.length() == 0)  
      58.             return null;  
      59.         try {  
      60.             this.getConnection();  
      61.             rs = stmt.executeQuery(sqlStr);  
      62.             return rs;  
      63.         } catch (SQLException ex) {  
      64.             ex.printStackTrace();  
      65.             return null;  
      66.         }  
      67.   
      68.     }  
      69.   
      70.     /** 
      71.      * 更新数据部分 
      72.      *  
      73.      * @return 更新是否成功 
      74.      */  
      75.     public boolean executeUpdate(String sqlStr) {  
      76.   
      77.         if (sqlStr == null || sqlStr.length() == 0)  
      78.             return false;  
      79.         try {  
      80.             this.getConnection();  
      81.             stmt.executeUpdate(sqlStr);  
      82.             return true;  
      83.         } catch (SQLException ex) {  
      84.             ex.printStackTrace();  
      85.             return false;  
      86.         } finally {  
      87.             try {  
      88.                 if (stmt != null) {  
      89.                     stmt.close();  
      90.                 }  
      91.             } catch (SQLException e) {  
      92.                 e.printStackTrace();  
      93.             }  
      94.             try {  
      95.                 if (conn != null) {  
      96.                     conn.close();  
      97.                 }  
      98.             } catch (SQLException e) {  
      99.                 e.printStackTrace();  
      100.             }  
      101.   
      102.         }  
      103.   
      104.     }  
      105.   
      106.     public void closeStmt() {  
      107.         try {  
      108.             if (stmt != null) {  
      109.                 stmt.close();  
      110.             }  
      111.         } catch (Exception e) {  
      112.             e.printStackTrace();  
      113.         }  
      114.     }  
      115.   
      116.     /** 
      117.      * 关闭数据库连接 
      118.      *  
      119.      * @param connect 
      120.      */  
      121.     public void closeConnection() {  
      122.         try {  
      123.             if (conn != null) {  
      124.                 /** 判断当前连接连接对象如果没有被关闭就调用关闭方法 */  
      125.                 if (!conn.isClosed()) {  
      126.                     conn.close();  
      127.                 }  
      128.             }  
      129.         } catch (Exception ex) {  
      130.             ex.printStackTrace();  
      131.         }  
      132.     }  
      133.   
      134. }  
  • 相关阅读:
    第一次实习项目总结
    javascript整理笔记(一)-----写项目的小技巧
    Vue(项目踩坑)_These dependencies were not found: * !!vue-style-loader!css-loader?{"sourceMap":true}!../../../node_modules/vue-loader/lib/style-compiler/index?{"vue"
    JS(递归-记一次面试题)-写一个get函数,get({a:1}, 'a')输出1,get({a : {b:2}},‘a.b’)输出2,按照此规律写一个函数
    html5_音视频的兼容性写法
    canvas_画出图片的马赛克
    项目(踩坑)_移动端在使用touch滑动事件的时候会出现抖动现象
    vue+mongoose+node.js项目总结第七篇_express框架中使用socket.io插件实现在线聊天功能(前端消息发送-后端接收逻辑)
    网址
    RAII Theory && auto_ptr
  • 原文地址:https://www.cnblogs.com/husam/p/3830231.html
Copyright © 2020-2023  润新知