Java code for importing Excel data into Oracle

Source: Internet
Author: User

1. First need two jar package Jxl.jar,ojdbc.jar (note version, version inappropriate will report version error)
2. Code:

Java code
  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 data Import into Oracle
  11. * @author SH
  12. * 2010-05-11
  13. */
  14. public class InsertData {
  15. public static void Main (string[] args) throws Exception {
  16. InsertData in = new InsertData ();
  17. In.insert ("F:/myjob/hah.xls", "Information");
  18. }
  19. /**
  20. *
  21. * @param path
  22. * Path of Excel file to parse
  23. * @param dataTable
  24. * The name of the table to write to the database
  25. * @throws biffexception
  26. * @throws IOException
  27. */
  28. public void Insert (String path,string dataTable) throws Biffexception, IOException {
  29. File File = new file (path);
  30. Create a new Excel workbook
  31. Workbook RWB = null;
  32. RWB = Workbook.getworkbook (file);
  33. Getting the first table index in the workbook is the Sheet1,sheet2,sheet3 under Excel ...
  34. Sheet Sheet = rwb.getsheets () [0];
  35. int rscolumns = Sheet.getcolumns ();//Number of columns
  36. int rsrows = Sheet.getrows ();//Number of rows
  37. String simnumber = "";//data in each cell
  38. Dbutils jdbc=new dbutils ();
  39. String str= "";//stitching the column to be inserted
  40. for (int j = 0; J <rsColumns; J + +) {
  41. Cell cell = Sheet.getcell (j, 0);
  42. Simnumber = Cell.getcontents ();
  43. if (j==rscolumns-1) {
  44. str + = Simnumber;
  45. }else{
  46. str + = simnumber+ ",";
  47. }
  48. }
  49. for (int i = 1; i < rsrows; i++) {
  50. String sql = "INSERT INTO" +datatable+ "(" +str+ ") VALUES (";//Splicing SQL
  51. System.out.println (str);
  52. for (int j = 0; J < Rscolumns; J + +) {
  53. Cell cell = Sheet.getcell (j, I);
  54. Simnumber = Cell.getcontents ();
  55. if (j==rscolumns-1) {
  56. SQL + = "'" + simnumber+ "'";
  57. }else{
  58. SQL + = "'" + simnumber+ "',";
  59. }
  60. }
  61. SQL + = ")";
  62. Jdbc.executeupdate (SQL);//Execute SQL
  63. }
  64. Jdbc.closestmt ();
  65. Jdbc.closeconnection ();
  66. }
  67. }

Util class

Java code
    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. * Oracle Database connection
    8. *
    9. * @author SH 2010-05-11
    10. */
    11. public class Dbutils {
    12. PRIVATE Connection conn = null;
    13. Private Statement stmt = null;
    14. Private ResultSet rs = null;
    15. /** Oracle Database Connection URL */
    16. Private final static String Db_url = "Jdbc:oracle:thin: @localhost: 1521:xe";
    17. /** Oracle Database Connection Driver */
    18. Private final static String Db_driver = "Oracle.jdbc.driver.OracleDriver";
    19. /** Database User name */
    20. Private final static String Db_username = "Test";
    21. /** Database Password */
    22. Private final static String Db_password = "Test";
    23. /**
    24. * Get database connection
    25. *
    26. * @return
    27. */
    28. Public Connection getconnection () {
    29. /** declaring Connection Connection object */
    30. Connection conn = null;
    31. try {
    32. /** uses the Class.forName () method to automatically create an instance of this driver and automatically calls DriverManager to register it */
    33. Class.forName (Db_driver);
    34. /** get the database connection via the DriverManager getconnection () method */
    35. conn = DriverManager
    36. . getconnection (Db_url, Db_username, Db_password);
    37. stmt = Conn.createstatement ();
    38. } catch (Exception ex) {
    39. Ex.printstacktrace ();
    40. }
    41. Return conn;
    42. }
    43. /**
    44. * Query Data section
    45. *
    46. * @return ResultSet
    47. */
    48. Public ResultSet executeQuery (String sqlstr) {
    49. if (sqlstr = = NULL | | sqlstr.length () = = 0)
    50. return null;
    51. try {
    52. This.getconnection ();
    53. rs = Stmt.executequery (SQLSTR);
    54. Return RS;
    55. } catch (SQLException ex) {
    56. Ex.printstacktrace ();
    57. return null;
    58. }
    59. }
    60. /**
    61. * Update Data section
    62. *
    63. * @return Update is successful
    64. */
    65. public boolean executeupdate (String sqlstr) {
    66. if (sqlstr = = NULL | | sqlstr.length () = = 0)
    67. return false;
    68. try {
    69. This.getconnection ();
    70. Stmt.executeupdate (SQLSTR);
    71. return true;
    72. } catch (SQLException ex) {
    73. Ex.printstacktrace ();
    74. return false;
    75. } finally {
    76. try {
    77. if (stmt! = null) {
    78. Stmt.close ();
    79. }
    80. } catch (SQLException e) {
    81. E.printstacktrace ();
    82. }
    83. try {
    84. IF (conn! = null) {
    85. Conn.close ();
    86. }
    87. } catch (SQLException e) {
    88. E.printstacktrace ();
    89. }
    90. }
    91. }
    92. public void closestmt () {
    93. try {
    94. if (stmt! = null) {
    95. Stmt.close ();
    96. }
    97. } catch (Exception e) {
    98. E.printstacktrace ();
    99. }
    100. }
    101. /**
    102. * Close Database connection
    103. *
    104. * @param Connect
    105. */
    106. public void CloseConnection () {
    107. try {
    108. IF (conn! = null) {
    109. /** determine if the current connection object is not closed, call the Close method */
    110. if (!conn.isclosed ()) {
    111. Conn.close ();
    112. }
    113. }
    114. } catch (Exception ex) {
    115. Ex.printstacktrace ();
    116. }
    117. }
    118. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.