How to connect Java to Mysql?

I got these errors for my Java program. I have already placed mysql-connector-java-5.1.14-bin.jarinside my classpath. How to solve this?

HSystemRDB.java:144: package com.mysql.jdbc does not exist
    Driver driver = new com.mysql.jdbc.Driver();
                                      ^
HTestClassRDB.java:99: package com.mysql.jdbc does not exist
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());

The code:

    String url = "jdbc:mysql://wire:3306/h?user="+pSystemRDB.USERNAME+"&password="+pSystemRDB.PASSWORD;
    Connection con;
    Statement stmt;
    String query1 = "Delete from dbase";
    String query2 = "Delete from id";


    try {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    } catch (Exception e) {
        System.out.println("Class Not Found Exception:");
        System.out.println(e.getMessage());
    }
+3
source share
3 answers

You need to download the mysql package from here and put it in the library, I will edit the steps in a few minutes

this is the correct syntax to connect to the database:

try
{
  // create a java mysql database connection
  String myDriver = "org.gjt.mm.mysql.Driver";
  String myUrl = "jdbc:mysql://localhost/test";
  Class.forName(myDriver);
  Connection conn = DriverManager.getConnection(myUrl, "root", "");

  // your prepstatements goes here...

  conn.close();
}
catch (Exception e)
{
  System.err.println("Got an exception! ");
  System.err.println(e.getMessage());
}

Hope this helps

+6
source

You need to add the MySQL Driver to your classpath and import the appropriate classes into your source.

Refer to this basic tutorial, article

+7
source

It seems that you do not have a jar file to connect the database. You can download mysql-connector-java-3.1.12.jar.

Please download it and place it in the folder of libyour web application. This should solve your problem.

If you have other problems, you can refer to this guide for steps to connect MySQL using JDBC

0
source

Source: https://habr.com/ru/post/1779139/


All Articles