-2

I am using Tomcat server 8

I have added Ojdbc7.jar in Lib

and using Oracle 11g (11.2.0.2) version

My code is :

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

public class PrintForm extends HttpServlet
{
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException
    {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        //Variables declaration
        String emp_name = req.getParameter("name");
        String s = "INSERT into Emp values(?,?)";

        try{
            //Making connection with Oracle

            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
            DriverManager.registerDriver (new oracle.jdbc.OracleDriver());

            //Inserting data into table
            PreparedStatement pst = con.prepareStatement(s);
            pst.setInt(1,1);
            pst.setString(2,emp_name);
            pst.executeUpdate();

            con.close();
        }catch(Exception e){
            out.println(e);
        }       
    }
}

And this is throwing ,

java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521:xe 

this exception.

So what driver, or what JDK version I should use ? current version is 1.7

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

4 Answers4

1

You should to load your driver first before you create your connection :

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", 
                 "system", "oracle");

You can find a good tutorial about that here : Connect to Oracle DB via JDBC driver

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1

You must add Class.forName("oracle.jdbc.driver.OracleDriver"); line before the Connection con = D....

Mustafa Çil
  • 766
  • 8
  • 15
  • 1
    Thanks a lot. I forgot to add, because as in JDK 1.8 it is no more supported, so I forgot to add it in 1.7 – Mrunmay Deswandikar Mar 19 '17 at 14:50
  • @MagentoDeveloper You're wrong, this works the same in Java 8. The problem is probably that you deployed the driver with the application and not in the tomcat `lib` folder. In that case JDBC 4 automatic driver loading (introduced in Java 6) won't work and you will need to load the driver manually. – Mark Rotteveel Mar 20 '17 at 07:56
1

According to the Oracle JDBC FAQ

  • If you are using Oracle 11.2 or 11gR2 with Java 8, 7 or 6 then you should use ojdbc6.jar. (The caveat is that JDK7 and JDK8 are supported in 11.2.0.3 and 11.2.0.4 only.)
  • If you are using Oracle 11.2 or 11gR2 with Java 5 then you should use ojdbc5.jar.
  • If you are using Oracle 12.1 or 12cR1 with Java 7 and 8 then you should use ojdbc7.jar
  • If you are using Oracle 12.1 or 12cR1 with Java 6 then you should use ojdbc6.jar
  • If you are using Oracle 12.2 or 12cR2 with Java 8 then you should use ojdbc8.jar.
  • If you are using Oracle 18.3 with Java 8 through 11 then you should use ojdbc8.jar.

The following have been removed from the FAQ because Oracle 11.1 and earlier are no longer supported except under the "Sustaining Support" program:

  • If you are using Oracle 11gR1 with Java 6, then you should use ojdbc6.jar.
  • If you are using Oracle 11gR1 with Java 5, then you should use ojdbc5.jar.

You are using 11gR2, so you need ojdbc6.jar, but note the caveat about the Oracle DB versions that support Java 7 and Java 8.

It is also advisable to use the most recent version of Java that is compatible with your version of Oracle DB; i.e. Java 8. (Java 7 and earlier are past their "end of life" ... unless you have an Oracle Java support contract.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You first need to register the driver

  DriverManager.registerDriver (new oracle.jdbc.OracleDriver());

Please read the javadocs of the oracle driver. There it explains this and how to get a connection in different scenarios: https://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html

Also, if you search online for the error No suitable driver found for you'll find plenty of sites explaining how to fix this issue in this scenario (and others).

Augusto
  • 28,839
  • 5
  • 58
  • 88