0

I can't establish a connection with database. Even though the same code is working fine in Swing projects, it doesn't work in dynamic web project

public class RegisterDao {
    
    public static final String URL = "jdbc:mysql://localhost:3306/userdb";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "";
    public static final String DRIVER_CLASSNAME = "com.mysql.jdbc.Driver";
    public static Connection con = null;
    
    
    public void loadDriver(String DRIVER_CLASSNAME) {
        try {
            Class.forName(DRIVER_CLASSNAME);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static Connection getConnection() {
        try {
            String name = "Bibek";
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            System.out.println(name);
            System.out.println(con);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return con;
    }

I am getting null for the connection return. I have looked in every aspect and nothing seems to be wrong with the code, but I keep on getting connection is null.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I'm not familiar with what you're doing, but is the connection established? It seems that you are trying to get a connection but there is no connection, leading it to be null. Make sure the connection is established before getting it. – danky Jun 25 '22 at 20:01
  • I found the solution. I had configured jar in buildpath only. When i later copied it to lib folder, everything worked. – Bibek Bhattarai Jun 25 '22 at 20:05
  • Don't catch exceptions and continue on as if nothing happened, and please include any exception stacktrace in your question. – Mark Rotteveel Jun 26 '22 at 17:31

0 Answers0