java.sql.SQLException:没有为jdbc找到合适的驱动程序:oracle:thin

时间:2014-07-30 19:00:15

标签: java sql oracle jdbc

我目前有一个网络应用程序,某些类可以连接到我的数据库,但另一个类不能。无论出于何种原因,我已经开始为我的一个类获取此异常。这就是我所做的:

  1. 我正在使用Java 7,并且在我的类路径和lib文件夹
  2. 中都有ojdbc7
  3. 我有多个可以连接到url文件的类,但我的其他类不能抛出“java.sql.SQLException:找不到合适的jdbc驱动程序:oracle:thin:@someurl”
  4. 我的班级能够在我连接到另一个班级
  5. 后访问数据库
  6. 我有一个包含URL的config.properties文件,所以我知道网址没有错,从不更改
  7. 这是程序失败的代码。在我调用此方法之前,我没有连接到数据库:

        public SimpleEntry<String, String> loadOrders(File file) {
        SimpleEntry<String, String> response = new SimpleEntry<String, String>(
                "", "");
        PreparedStatement st = null;
        ResultSet rs = null;
        Connection con = null;
    
        try {
            Scanner scan = new Scanner(file);
            String header = "";
            String trailer = "";
            int orderCtr = 0;
            int valueCtr = 0;
            String request = "";
    
            ArrayList<HashMap<String, String>> orders = 
    new ArrayList<HashMap<String, String>>();
    
            log.info("Scanning content");
            // start to scan content
            while (scan.hasNextLine()) {
                String line = scan.nextLine();
                Scanner sc = new Scanner(line);
                sc.useDelimiter("\t");
                String type = sc.next();
    
                // differentiates between header, trailer, and content
                if (type.equals("H")) {
                    header = line;
                } else if (type.equals("T")) {
                    trailer = line;
                    break;
                } else {
                    // begin to enter in data appropriately from method's
                    // parameter
                    // key of map = column, value = data being inserted
                    HashMap<String, String> order = new HashMap<String, String>();
    
                    // Format:
                    /*
                     * CLIENT_CUSTOMER_ID, FIRST_NAME, LAST_NAME,___, ADDRESS1,
                     * ADDRESS2, CITY, STATE, POSTAL_CODE, PHONE, EMAIL_ADDRESS,
                     * STORE_NUMBER, DELIVERY_METHOD,___, REWARD_VALUE,___,___,
                     * CARD_ACTIVE_DATE, CARD_EXPIRED_DATE, REQUEST
                     */
    
                    order.put("CLIENT_CUSTOMER_ID", sc.next());
                    String name = sc.next();
                    order.put("FIRST_NAME", name.substring(0, 1).toUpperCase()
                            + name.substring(1).toLowerCase());
                    name = sc.next();
                    order.put("LAST_NAME", name.substring(0, 1).toUpperCase()
                            + name.substring(1).toLowerCase());
                    sc.next();
                    order.put("ADDRESS1", sc.next());
                    order.put("ADDRESS2", sc.next());
                    order.put("CITY", sc.next());
                    order.put("STATE", sc.next());
                    order.put("POSTAL_CODE", sc.next());
                    order.put("PHONE", sc.next());
                    order.put("EMAIL_ADDRESS", sc.next());
                    order.put("STORE_NUMBER", sc.next());
                    order.put("DELIVERY_METHOD", sc.next());
                    sc.next();
    
                    // purpose of valueCtr -> count reward values for a total
                    String oVal = sc.next();
                    valueCtr += Integer.parseInt(oVal);
                    order.put("REWARD_VALUE", oVal);
                    sc.next();
                    sc.next();
                    order.put("CARD_ACTIVE_DATE", sc.next());
                    order.put("CARD_EXPIRED_DATE", sc.next());
                    // used later in program
                    request = sc.next();
                    order.put("REQUEST", request);
    
                    orders.add(order);
    
                    // count number of orders
                    orderCtr++;
                }
                sc.close();
            }
            scan.close();
    
            log.info("Scanning complete");
    
            // finds if the trailer contains the correct value and order amount
            boolean ok = true;
            if (!trailer.contains(Integer.toString(valueCtr))) {
                ok = false;
            }
            if (!trailer.contains(Integer.toString(orderCtr))) {
                ok = false;
            }
    
            // if the trailer doesnt, throw error
            if (!ok) {
                log.error("Error in loadOrders: Order Count
    and/or total value of file \n does not match trailer amounts");
                response = new SimpleEntry<String, String>("ERROR",
                        "Order Count and/or total value of file \n does 
    not match trailer amounts");
                return response;
            }
    
            String className = "oracle.jdbc.driver.OracleDriver";
            Class.forName(className);
            con = DriverManager.getConnection(Env.getCardUrl(),
                    Env.getCardUser(), Env.getCardPass());
    
            log.info("Starting insertion statement");
            String query = "SELECT MAX(BATCH_ID) + 1 FROM INTEGRATION.OL_ORDER";
            st = con.prepareStatement(query);
            rs = st.executeQuery();
    
            rs.next();
            int batch = rs.getInt(1);
    
            String insert = "INSERT INTO INTEGRATION.OL_ORDER ("
                    + "BATCH_ID, "
                    + "CLIENT_CUSTOMER_ID, "
                    + "FIRST_NAME, "
                    + "LAST_NAME, "
                    + "ADDRESS1, "
                    + "ADDRESS2, "
                    + "CITY, "
                    + "STATE, "
                    + "POSTAL_CODE, "
                    + "PHONE, "
                    + "EMAIL_ADDRESS, "
                    + "STORE_NUMBER, "
                    + "DELIVERY_METHOD, "
                    + "REWARD_VALUE, "
                    + "CARD_ACTIVE_DATE, "
                    + "CARD_EXPIRED_DATE, "
                    + "REQUEST,"
                    + "PASSPHRASE, "
                    + "ITEM_CODE, "
                    + "CONFIRMED_MAILADDR, "
                    + "JET_CLIENT_ID) VALUES(?, ?, ?, ?, ?, ?, ?, 
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
            st = con.prepareStatement(insert);
    
            log.info("Insertion complete, reorganizing data");
            // add batch
            for (HashMap<String, String> o : orders) {
    
                st.setInt(1, batch);
    
                // first, last, address1, address2, city, state, postal, phone,
                // storenum, delivery
                // validation and formatting
                ArrayList<String> content = validateContent(
                        o.get("FIRST_NAME"), o.get("LAST_NAME"),
                        o.get("ADDRESS1"), o.get("ADDRESS2"), o.get("CITY"),
                        o.get("STATE"), o.get("POSTAL_CODE"), o.get("PHONE"),
                        o.get("STORE_NUMBER"), o.get("DELIVERY_METHOD"));
    
                st.setString(2, o.get("CLIENT_CUSTOMER_ID"));
                st.setString(3, content.get(0));
                st.setString(4, content.get(1));
                st.setString(5, content.get(2));
                st.setString(6, content.get(3));
                st.setString(7, content.get(4));
                st.setString(8, content.get(5));
                st.setString(9, content.get(6));
                st.setString(10, content.get(7));
                st.setString(11, o.get("EMAIL_ADDRESS"));
                st.setString(12, content.get(8));
                st.setString(13, content.get(9));
                st.setInt(14, Integer.parseInt(o.get("REWARD_VALUE")));
                st.setDate(15, stringToDate(o.get("CARD_ACTIVE_DATE")));
                st.setDate(16, stringToDate(o.get("CARD_EXPIRED_DATE")));
                st.setString(17, o.get("REQUEST"));
                st.setString(18, getRandom());
                st.setString(19, "17331-000002");
                st.setString(20, "0");
                st.setInt(21, 97);
    
                st.addBatch();
            }
    
            st.executeBatch();
    
            log.info("Reorganization complete");
            response = new SimpleEntry<String, String>(Integer.toString(batch),
                    request);
    
        } catch (Exception e) {
            StringWriter errors = new StringWriter();
            e.printStackTrace(new PrintWriter(errors));
            String temp = errors.toString();
            log.fatal("Error inside loadOrders(): " + temp);
            return null;
        } finally {
            Dir.close(rs, st, con);
        }
    
        return response;
    }
    

    我的Dir.close代码:

    public static void close(ResultSet rs, Statement ps, Connection conn) {
    
        Logger logger =LogManager.getLogger("AE");
    
        if (rs != null) {
            try {
                rs.close();
    
            } catch (SQLException e) {
                logger.error("The result set cannot be closed.", e);
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                logger.error("The statement cannot be closed.", e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                logger.error("The data source connection cannot be closed.", e);
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

正如我在上一篇评论中提到的,JAR是在webapp的课程上,这意味着错误不会由于缺乏驱动程序而导致。显然,在部署我的WAR文件时,tomcat引用了较旧的WAR文件或代码未使用我上面发布的代码更新的代码。导出我的WAR文件并重命名为任意名称,然后在该名称下部署之后,我再也没有收到错误。要考虑的是我之前的war文件中的代码混乱;我没有正确关闭PreparedStatement,Result,甚至是Connection变量。使用上面发布的代码用不同的名称更新战争。