#Hii trying to connect my servlet/jsp project with mysql connector.

1 messages · Page 1 of 1 (latest)

regal remnant
#

getting

getting "java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver”

here is the code

  Class.forName("com.mysql.cj.jdbc.Driver");

i have added the jar file using
build-path>modulepath>add jar file

but still getting exception

jade ginkgoBOT
#

<@&987246527741304832> please have a look, thanks.

valid reef
#

You should be using Maven/Gradle to manage your dependencies.

jade ginkgoBOT
#

Changed the category to Java.

#

<@&987246399047479336> please have a look, thanks.

regal remnant
#

do I have to create a new project for adding build tool?
also,it is working fine when I ran the code outside eclipse?(compiled and ran)
not sure what's happening with eclipse when using with dynamic web project

#

jar file showing here as well

valid reef
#

No, you can add it to an existing project. And it depends on how you manage your classpath.

regal remnant
#

update:
created a java file and it's working.
but getting error while calling the same method from jsp

#

this is the jsp

<%@page import="com.tech.blog.helpers.ConnectionProvider"%>
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp page reloaded</title>
</head>
<body>
    <h1>Hello world!</h1>
    <%
        Connection conn=ConnectionProvider.getConnection();
    %>
    <h1><%= conn %></h1>
</body>
</html>
```\
jade ginkgoBOT
# regal remnant this is the jsp ``` <%@page import="com.tech.blog.helpers.ConnectionProvider"%> ...

Detected code, here are some useful tools:

Formatted code
 <  % @page
import  = "com.tech.blog.helpers.ConnectionProvider" %  >  <  % @page language = "java"import  = "java.sql.*"contentType = "text/html; charset=UTF-8"pageEncoding = "UTF-8" %  >  < !DOCTYPE html > <html> <head>  < meta charset = "UTF-8" > <title> jsp page reloaded <  / title >  <  / head > <body> <h1> Hello world! <  / h1 >  <  % Connection conn = ConnectionProvider.getConnection();

 %  > <h1>  <  %= conn %  >  <  / h1 >  <  / body >  <  / html >
valid reef
#

but getting error while calling the same method from jsp You do need to share your error.

regal remnant
#

servlet

package com.tech.blog.helpers;
import java.sql.*;

public class ConnectionProvider {
    public static void main(String[] args) {
        Connection c=ConnectionProvider.getConnection();
        System.out.println(c);
    }
    public static Connection getConnection() {
        Connection conn = null;
        try {
        //System.out.println(Driver.class);
            if(conn==null) {
                Class.forName("com.mysql.cj.jdbc.Driver");
                conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/techblog","root","admin");
            }    
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return conn;
    }
}

when I run as "java application"

#

when I select run on server

valid reef
#

What do you mean with: I created a Java file and it's working?

regal remnant
#

I wanted to test whether it is working as java application?

valid reef
#

The error indicates the driver's not available. So is it being bundled properly.

#

Well yes, but what/how did you test it?

regal remnant
regal remnant
#

working with IDE is hard 😅

valid reef
#

No, it isn't.

#

You're just making it hard by ignoring my input.

#

You're managing your dependencies yourself, which is a lot more tedious.