EXCEL向ORCL数据库导数据的相关程序

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.UUID;
import javax.swing.JOptionPane;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class TestP {

       static String UserName = "wangbin2"; // 生成的数据库表名字
        private static Connection conn = null;
       private static String drive = "oracle.jdbc.driver.OracleDriver";
       private static String DBurl = "jdbc: oracle: thin: @192.168.1.100:1521: orcl";
       private static String name = "username"; // 数据库账号
        private static String pwd = "password"; // 数据库,密码
        private static Statement st = null;

        public static void main(String[] args) {
                readExcel("D:/201004242005.xls");
                UserName = "UserName";
        }

        public static void readExcel(String url) {
                File filename = new File(url);
                Workbook wb = null;
                String create = "create table " + UserName + "(";
                String sql = "insert into " + UserName + "(";
                String parameter = "";
                String value = "";
                String insert = "";
                try {
                        wb = Workbook.getWorkbook(filename);
                        Sheet s = wb.getSheet(0);// 第1个sheet
                        Cell c = null;
                        int row = s.getRows();// 总行数
                        int col = s.getColumns();// 总列数
                        for (int i = 0; i < col; i++) {
                                if (i == col - 1) {
                                        create += s.getCell(i, 0).getContents() + " varchar(255)";
                                        parameter += s.getCell(i, 0).getContents();
                                } else {
                                        create += s.getCell(i, 0).getContents() + " varchar(255),";
                                        parameter += s.getCell(i, 0).getContents() + ",";
                                }
                        }
                        create += ")";
                        System.out.println("数据库生成表语句---" + create);
                        try {
                                Create(create);
                        } catch (Exception e) {
                                int a = JOptionPane.showConfirmDialog(null, "数据库已经存在的表,确定删除吗?",
                                                "温馨提示", JOptionPane.YES_NO_OPTION);
                                if (a == 0) {
                                        try {
                                                Create("drop table " + UserName + "");
                                                Create(create);
                                        } catch (Exception e1) {
                                                return;
                                        }
                                } else {
                                        int aa = JOptionPane.showConfirmDialog(null,
                                                        "是否继续添加到原来表单裏面", "温馨提示", JOptionPane.YES_NO_OPTION);
                                        if (aa == 1) {
                                                return;
                                        }
                                }
                        }
                        System.out.println("--------------------------------------");
                        for (int i = 1; i < row; i++) {
                                value = "";
                                for (int j = 0; j < col; j++) {
                                        c = s.getCell(j, i);
                                        if (j < col - 1) {
                                                if(j==0){
                                                        value += "'" + UUID.randomUUID().toString() + "',";//加入UUID可不用
                                                }else{
                                                        value += "'" + c.getContents() + "',";
                                                }
                                        } else {
                                                value += "'" + c.getContents() + "'";
                                        }
                                }
                                insert = sql + parameter + ") values(" + value + ")";
                                System.out.println("添加语句-------" + insert);
                                int a = insert(insert);
                                if (a > 0) {
                                        System.out.println("成功添加" + i + "次");
                                } else {
                                        System.out.println("第" + i + "次失败了");
                                }
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (BiffException e) {
                        e.printStackTrace();
                }
        }

        public static Connection getConn() {
                try {
                        Class.forName(drive);
                        conn = DriverManager.getConnection(DBurl, name, pwd);
                } catch (Exception e) {
                        e.printStackTrace();
                        JOptionPane.showMessageDialog(null, "数据库连接错误");
                }
                return conn;
        }

        public static void Close() {
                if (conn != null)
                        try {
                                conn.close();
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
        }

        public static int insert(String sql) {
                int result = 0;
                try {
                        st = getConn().createStatement();
                        result = st.executeUpdate(sql);
                } catch (Exception e) {
                        System.out.println("添加失败");
                } finally {
                        Close();
                }
                return result;
        }

        public static int Create(String sql) throws Exception {
                int result = 0;
                try {
                        st = getConn().createStatement();
                        result = st.executeUpdate(sql);
                } finally {
                        Close();
                }
                return result;
        }
}

 

你可能感兴趣的:(Excel)