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.