0

Brushing up on my Java skills guys. I used to declare my connection details in Java inside classes. I decided to try to use context and @Resource method this time and is having some errors

INFO: Server startup in 1701 ms java.sql.SQLException: Cannot create JDBC driver of class 'oracle.jdbc.driver.OracleDriver' for connect URL 'jdbc:oracle:thin@//10.30.30.30:1521/mydatabase?useSSL=false' at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2065) at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1939) at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1412) at com.pldt.itms.FirstServlet.doGet(FirstServlet.java:40) at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source) Caused by: java.sql.SQLException: No suitable driver at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2056) ... 27 more

Below is my context.xml inside WebContent/META-INF

<?xml version="1.0"?>
<Context>

<Resource url="jdbc:oracle:thin@//10.30.30.30:1521/mydatabase?useSSL=false" driverClassName="oracle.jdbc.driver.OracleDriver" password="password" username="username" maxWait="10000" maxIdle="5" maxActive="20" type="javax.sql.DataSource" auth="Container" name="jdbc/web_my_context"/>

</Context>

Here is my Java Class

package com.company.project;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Servlet implementation class FirstServlet
 */
@WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Resource(name="jdbc/web_my_context")
    DataSource dataSource;
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1 printwriter
        PrintWriter out = response.getWriter();
        response.setContentType("text/plain");
        //2 connect to db
        Connection myConn = null;
        Statement myStmt = null;
        ResultSet myRs = null;

        try{
            myConn = dataSource.getConnection();

        //3 create sql
            String sql = "select * from table_authorities";
            myStmt = myConn.createStatement();
        //4 execute sql
            myRs = myStmt.executeQuery(sql);
        //5 process resultset of sql statment

            while (myRs.next()){
                String email = myRs.getString("username");
                out.println(email);
            }

        } catch (Exception exc) {
            exc.printStackTrace();
        }
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

}

enter image description here

EDIT: I corrected my xml

<?xml version="1.0"?>

<Context>

<Resource url="jdbc:oracle:thin:@//10.30.30.30:1521/mydatabase?useSSL=false" driverClassName="oracle.jdbc.driver.OracleDriver" password="password" username="username" maxWait="10000" maxIdle="5" maxActive="20" type="javax.sql.DataSource" auth="Container" name="jdbc/web_my_context"/>

</Context>

But now it is still throwing an error:

Caused by: oracle.net.ns.NetException: Listener refused the connection with the following error: ORA-12514, TNS:listener does not currently know of service requested in connect descriptor at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:272) at oracle.net.ns.NSProtocol.connect(NSProtocol.java:263) at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1360) at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486) ... 35 more

TwoThumbSticks
  • 1,086
  • 3
  • 23
  • 38
  • Is the Oracle JDBC driver library in your class path ? – Titus Feb 20 '17 at 08:35
  • I added a screenshot of my buildpath library. I can see it in the Libraries tab – TwoThumbSticks Feb 20 '17 at 08:46
  • Show *full* stack trace. The error message you're showing is likely a derived message and not the real error. The real error will be listed as `Caused by:`. – Andreas Feb 20 '17 at 08:49
  • Your URL is wrong. Where did you see that URL syntax? It supposed to be `jdbc:oracle:thin:@//[:]/`. Did you copy an MS SQL or MySQL URL and use it for Oracle, without *bothering* to **read the [documentation](http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#05_03)** of how the URL should be? – Andreas Feb 20 '17 at 09:10
  • Corrected my code with the jdbc:oracle:thin:@//[:]/ still with the same error – TwoThumbSticks Feb 20 '17 at 09:16
  • @Andreas I am switching between an Oracle and MySQL database both with same errors. I am now thinking if I need to copy my ojdbc to tomcat lib? like the one suggested here. Will try the solution and update the answer. http://stackoverflow.com/questions/5556664/how-to-fix-no-suitable-driver-found-for-jdbcmysql-localhost-dbname-error-w – TwoThumbSticks Feb 20 '17 at 09:32
  • 2
    According to your last edit, you are still using the wrong URL, note the absence of a colon (`:`) between `thin` and `@`. – Mark Rotteveel Feb 20 '17 at 15:53

1 Answers1

3

As others have pointed out you have a syntax error in your xml the proper syntax is

jdbc:oracle:thin:@//HOSTNAME:PORT/SERVICENAME

I think your just having typos from switching between Oracle and MySQL, I recommend not doing that when posting a question so we can help you accurately and to do that we need the most current syntax.

updated answer

I think the one causing your error is actually this line over here

'jdbc:oracle:thin@//10.30.30.30:1521/mydatabase?useSSL=false'

AFAIK useSSL=false only works for MySQL and not for Oracle database.

Try removing useSSL=false in your xml. Your code should now look like this

'jdbc:oracle:thin@//10.30.30.30:1521/mydatabase'

I believe that by default Oracle does not use SSL you no longer need to explicityly say it to not use SSL. Just in case you want to use SSL for Oracle you will have to use the tcps protocol, you can do this by using the long detailed TNS style like this.

jdbc:oracle:thin:@(description=(address=(host=HOSTNAME)(protocol=tcps)(port=PORT))(connect_data=(service_name=SERVICENAME)(server=SHARED)))

If your interested in configuring your Oracle connection to use SSL you can check their documentation over at this LINK.

royjavelosa
  • 2,048
  • 6
  • 30
  • 46
  • corrected my context.xml but now it is throwing a different error " Listener refused the connection with the following error: ORA-12514, TNS:listener does not currently know of service requested in connect descriptor" I have updated my question to show the new error – TwoThumbSticks Feb 21 '17 at 01:59