将oracle驱动程序添加到Maven存储库时无法连接到数据库

时间:2017-11-15 19:44:13

标签: java hibernate maven jdbc ojdbc

我正在尝试编写一个简单的Web服务,它将信息读取/写入数据库。我认为oracle驱动程序ojdbc6不能作为pom.xml中的依赖项添加,所以我使用下面的mvn install命令添加它。

mvn install:install-file -Dfile=C:\ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar

ojdbc6.jar已成功添加 [ojdbc6.jar] [1]

我的DAO课程试图从数据库中检索产品信息:

    package com.nt.dao;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import com.nt.dto.ProductDTO;

public class ProductServiceDAO {

    public ProductDTO getProductInfo(long productId) {
        System.out.println("session---------1-");
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        System.out.println("session---------2-");
        Session session = sessionFactory.openSession();
        ProductDTO productDTO = new ProductDTO();
        productDTO.setProductName("Apple");
        Transaction transaction = session.beginTransaction();
        System.out.println("session---------3-");
        productDTO = (ProductDTO) session.get(ProductDTO.class, productId);
        session.close();
        return productDTO;
    }
}

当我尝试运行程序时,我收到以下异常。请帮忙。

 Caused by: org.hibernate.exception.JDBCConnectionException: Cannot open connection

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
at com.nt.dao.ProductServiceDAO.getProductInfo(ProductServiceDAO.java:17)
at com.nt.bo.ProductServiceBO.getProductInfo(ProductServiceBO.java:10)
at com.nt.ws.impl.ProductServiceImpl.getProductInfo(ProductServiceImpl.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:181)
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:97)
... 40 more
Caused by: java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521:orcl
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 53 more

1 个答案:

答案 0 :(得分:0)

它现在正在工作。我在我的课程中添加了以下代码行

Class.forName ("oracle.jdbc.OracleDriver");

但我在这里面临另一个问题。我的产品" table有一条记录,如下面的屏幕截图所示。products

当我尝试在SOAPUI中检索记录时,不会检索它。此外,记录被删除。下面是SOAPUI截图

soap

我的ProducDTO课程:

    package com.nt.dto;

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;

    @XmlType(name="ProductDTO")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class ProductDTO {
        @XmlElement
        private int productId;
        @XmlElement
        private String productName;
        @XmlElement
        private int productPrice;
        public int getProductId() {
            return productId;
        }
        public void setProductId(int productId) {
            this.productId = productId;
        }
        public String getProductName() {
            return productName;
        }
        public void setProductName(String productName) {
            this.productName = productName;
        }
        public int getProductPrice() {
            return productPrice;
        }
        public void setProductPrice(int productPrice) {
            this.productPrice = productPrice;
        }


    }

My DAO class retrieving the record:

package com.nt.dao;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import com.nt.dto.ProductDTO;

public class ProductServiceDAO {

    public ProductDTO getProductInfo(long productId) {
        System.out.println("session---------1-");
        try {
            Class.forName ("oracle.jdbc.OracleDriver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        System.out.println("session---------2-");
        Session session = sessionFactory.openSession();
        ProductDTO productDTO = new ProductDTO();

        Transaction transaction = session.beginTransaction();
        System.out.println("session---------3-"+productId);
        productDTO = (ProductDTO) session.get(ProductDTO.class, (int) productId);
        if(productDTO!=null){
            System.out.println("session---------4-"+productDTO.getProductId());
            System.out.println("session---------5-"+productDTO.getProductName());
            System.out.println("session---------6-"+productDTO.getProductPrice());
        }else{
            System.out.println("session---------7-");
        }

        session.close();
        return productDTO;
    }
}

我的日志文件:

    Payload: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.nt.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:getProductInfo>
         <productId>159</productId>
      </ws:getProductInfo>
   </soapenv:Body>
</soapenv:Envelope>
--------------------------------------
session---------1-
session---------2-
session---------3-159
Hibernate: 
    /* load com.nt.dto.ProductDTO */ select
        productdto0_.PRODUCT_ID as PRODUCT1_5_0_,
        productdto0_.PRODUCT_NAME as PRODUCT2_5_0_,
        productdto0_.PRODUCT_PRICE as PRODUCT3_5_0_ 
    from
        PRODUCTS productdto0_ 
    where
        productdto0_.PRODUCT_ID=?
session---------7-
Nov 19, 2017 7:58:18 PM org.apache.cxf.services.ProductServiceService.ProductServicePort.ProductService
INFO: Outbound Message
---------------------------
ID: 2
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getProductInfoResponse xmlns:ns2="http://ws.nt.com/"/></soap:Body></soap:Envelope>
--------------------------------------

我的DAO课程:

package com.nt.dao;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import com.nt.dto.ProductDTO;

public class ProductServiceDAO {

    public ProductDTO getProductInfo(long productId) {
        System.out.println("session---------1-");
        try {
            Class.forName ("oracle.jdbc.OracleDriver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        System.out.println("session---------2-");
        Session session = sessionFactory.openSession();
        ProductDTO productDTO = new ProductDTO();

        Transaction transaction = session.beginTransaction();
        System.out.println("session---------3-"+productId);
        productDTO = (ProductDTO) session.get(ProductDTO.class, (int) productId);
        if(productDTO!=null){
            System.out.println("session---------4-"+productDTO.getProductId());
            System.out.println("session---------5-"+productDTO.getProductName());
            System.out.println("session---------6-"+productDTO.getProductPrice());
        }else{
            System.out.println("session---------7-");
        }

        session.close();
        return productDTO;
    }
}