关闭

用Java实现从Excel数据导入到Oracle数据库表中

标签: oracleexcel数据库javaimportexception
5966人阅读 评论(10) 收藏 举报


import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class InsertData {

        public static void main(String[] args){ 
                Connection myConnSDB = null;
                PreparedStatement psInsertData=null;
                int InsertData=0; 
                //Regist Oracle JDBC Driver
                try {
                        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
                        myConnSDB=
                                DriverManager.getConnection(
                                        "jdbc:oracle:thin:@127.0.0.1:1521:MYDB",
                                        "user",
                                        "user");

                        myConnSDB.setAutoCommit(false);

                } catch (SQLException e) {
                        e.printStackTrace();
                }

                try{
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        Connection con = DriverManager.getConnection( "jdbc:odbc:Book1","","" );
                        Statement st = con.createStatement();
                        ResultSet rs = st.executeQuery( "SELECT * FROM [Sheet1$]" );
                        psInsertData=
                                                                 myConnSDB.prepareStatement(" INSERT INTO TMP_TABLE"+

                                                                                                                        " (ACTION_ID,TIMESTAMP,USERID,USERNAME,ISSUE,ACTION)"+

                                                                                                                        "  VALUES "+

                                                                                                                        " (?,?,?,?,?,?) "); 

               
      

                        while (rs.next()) {
                                        psInsertData.clearParameters();
                                        psInsertData.setString(1,rs.getString(1)); 
                                        psInsertData.setString(2,rs.getString(2)); 
                                        psInsertData.setString(3,rs.getString(3)); 
                                        psInsertData.setString(4,rs.getString(4)); 
                                        psInsertData.setString(5,rs.getString(5)); 
                                        psInsertData.setString(6,rs.getString(6));
                                        InsertData=InsertData+psInsertData.executeUpdate();                                    

                        }
                        System.out.println(""+InsertData);
                        st.close();
                        con.close();
                }catch (Exception ex)
           {
                   System.err.print("Exception: ");
                   System.err.println(ex.getMessage());
                   ex.printStackTrace();
           } finally
                   {
                         try
                         {
                                if (psInsertData!= null)
                           {
                                psInsertData.close();
                           }
                                if (myConnSDB != null)
                           {
                                 myConnSDB.close();
                           }
       
       
                         } catch (SQLException e)
                         {
                           e.printStackTrace();
                         }
                   }
                }
        }
================

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:36965次
    • 积分:579
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:4篇
    • 译文:0篇
    • 评论:13条