0

I'm trying to establish a connecting using intellij and I can connect to it usiing the database maven class but when using the java class

static final String JBDC_DRIVER="com.mysql.jbdc.Driver";
    static final String DB_URL="jdbc:mysql://35.247.85.196:3302";
    static final String USER="Nick";
    static final String PASS="poop";

public static void main (String[] args)
{
Connection conn=null;
        Statement stmt=null;

        //reister jbdc driver
        try {
            Class.forName(JBDC_DRIVER);
            System.out.println("Connecting to the database...");
            conn=DriverManager.getConnection(DB_URL,USER,PASS);
            System.out.println("Connected to database successfully");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

} I get the exception

java.lang.ClassNotFoundException: com.mysql.jbdc.Driver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:315)
    at DataGenerator.main(DataGenerator.java:33)

and line 33 being

Class.forName(JBDC_DRIVER);
Nick
  • 13
  • 2
  • Do you have a MySQL driver included on the classpath or as a dependency? – Compass Apr 16 '19 at 20:07
  • Just added it I think. Check my edit @Compass – Nick Apr 16 '19 at 20:08
  • @Nick Make sure mysql-jdbc.jar is present in the module dependencies: https://www.jetbrains.com/help/idea/creating-and-managing-modules.html#working-with-module-dependencies. – CrazyCoder Apr 16 '19 at 20:36

1 Answers1

0

Try removing the Class.forName(JDBC_DRIVER), add a import com.mysql.jdbc.*; onto your main class and also add the name of your database on the DB_URL like this:

static final String DB_URL="jdbc:mysql://35.247.85.196:3302/dataBaseName";

Henrique Sabino
  • 546
  • 3
  • 19
  • Make sure you are placing the import statement at the beginning of your `main class` file, right after the `package` declaration. Also, make sure that your project dependencies include the library – Henrique Sabino Apr 16 '19 at 20:39
  • If you don't know how to add a library to your project take a look at this link https://www.jetbrains.com/help/idea/library.html – Henrique Sabino Apr 16 '19 at 20:43
  • Ya it's where the other import statements are. How would you import the library? – Nick Apr 16 '19 at 20:44
  • This other link might help you out as well https://stackoverflow.com/questions/7065402/how-to-add-external-library-in-intellij-idea – Henrique Sabino Apr 16 '19 at 20:50