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

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

Java代码  
  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. }  

转载于:https://www.cnblogs.com/husam/p/3830231.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值