-2
#code of servlet  
public class EmployeeServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        String username=request.getParameter("t1");
        int pass=Integer.parseInt(request.getParameter("t2"));
   
        Connection con=null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con=DriverManager.getConnection("jdbc:oracle:thin:HR/hr@localhost:1521:orcl\r\n"+ "","system","system");
            Statement stmt =con.createStatement();
            String query="select * from employee where emp_id="+pass;
            ResultSet rs=stmt.executeQuery(query);
            
            while(rs.next()) {
                System.out.println(rs.getInt(1));
                System.out.println(rs.getString(2));
            }

        stmt.close();
        con.close();

        // Redirect the response to success page
         
    } catch (Exception e) {
        e.printStackTrace();
    }

}

output:: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

Stacktrace::

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1407) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1215) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:377) at com.dynamic.employee.controller.EmployeeServlet.doGet(EmployeeServlet.java:88) at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:359) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1735) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:832)

Not me
  • 1
  • 1
  • You seem to have no active database connection since the`con` variable stays `null`. There is nothing wrong with the code you posted. If you want to catch this problem, use an if statement to check if `con` is `null`. – Japhei Jul 07 '22 at 19:04
  • code is clearly running when i create main method in servet but when i get values though html in doget method its not working ,its showing error – Not me Jul 07 '22 at 19:17
  • You are looking in the wrong file. The problem is caused because this method `dbConnection.getConections()` returnes `null`. – Japhei Jul 07 '22 at 19:30
  • Watch out the way you construct your query is vulnerable to SQL injection attacks. Your only safety is that you parsed an Integer beforehand. – Queeg Jul 07 '22 at 19:37
  • i dont understand why dbconnections.getconnections() is returning null and i checked every code perfectly yet while getting through doget method its showing nullexception – Not me Jul 07 '22 at 19:41
  • will parsing an integer truely save from sql injection? – Not me Jul 07 '22 at 19:45
  • Learn about SQL injection: https://www.stackhawk.com/blog/java-sql-injection-guide-examples-and-prevention/ – Queeg Jul 07 '22 at 19:50
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Jul 09 '22 at 19:50

1 Answers1

1

getConections() may have silently returned null - in that case you will have to share the code or analyze yourself. getConections() may also have thrown an exception - you will have to check stdout where you printed the error.

Anyway you need to check stdout closely as you cannot expect any result in the browser. If you want that, ensure to write something into the HttpServletResponse object - I can not see that in your current code.

Edit:

  • The ClassNotFoundException indicates the JDBC driver is not on your classpath. Not a code issue itself, but check how you run your code.

  • Could you also check why your JDBC url contains a linefeed?

Queeg
  • 7,748
  • 1
  • 16
  • 42
  • i have eddited my code please check once , null error is gone only class not found exception is there – Not me Jul 07 '22 at 20:01
  • @Notme what is this supposed to do? `Class.forName("oracle.jdbc.driver.OracleDriver")` Can you edit your post to include the stacktrace? – Japhei Jul 07 '22 at 20:16
  • 1
    That line loads the class, and the class initializer often registers the driver class in the DriverManager to ensure the next line succeeds. Today this is less and less necessary but still does not harm. – Queeg Jul 07 '22 at 20:20
  • now what is stack trace? – Not me Jul 07 '22 at 20:36
  • is this error anything related to web.xml? – Not me Jul 07 '22 at 20:38
  • i tried every variation and still error wont get sorted; – Not me Jul 07 '22 at 20:40
  • @Notme look at https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Japhei Jul 07 '22 at 20:51
  • i added stack trace kindly check once, i tried every last of options and variations still coudnt get it and its been 3 days – Not me Jul 08 '22 at 07:06
  • 1
    Thanks for adding. No surprise there. My answer is still valid. – Queeg Jul 08 '22 at 09:51
  • my error got solved , i appreciate all of your help , thanking you all ;) – Not me Jul 13 '22 at 14:12