简体   繁体   中英

How do I add the JDBC MySQL connector in my Gradle build?

I'm having trouble getting my .jar from my Gradle build to use the JDBC MySQL connector to connect to a remote database. I get a ClassNotFoundException, which leads me to believe that the .jar is not in the classpath. I'm new to Gradle so I've been trying to modify the default build.gradle that is created in a new Eclipse project.

Java Object:

package com.blu.generator;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class MySQLConnection {

    private Connection _conn;

    // init
    MySQLConnection(String url, String user, String pass) throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        this._conn = DriverManager.getConnection(url, user, pass);
        System.out.println("Database connection established.");
    }

    public void getDrugsClaims() throws SQLException {
        String query = "select * from drugs";
        Statement st = this._conn.createStatement();
        ResultSet rs = st.executeQuery(query);
        ResultSetMetaData rsmd = rs.getMetaData();

        int columnsNumber = rsmd.getColumnCount();
        while (rs.next()) {
            //Print one row          
            for(int i = 1 ; i <= columnsNumber; i++){
                  System.out.print(rs.getString(i) + " ");
            }
            System.out.println();        
        }
        return;

    }

}

build.gradle:

apply plugin: 'java-library'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

configurations {
    driver
}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:23.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

    // this is for mysql java connector
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'
    runtime "mysql:mysql-connector-java:8.0.15"
}

jar {
    manifest {
        attributes(
                'Main-Class': 'com.blu.generator.Main'
        )
    }
}

I believe your import is working fine, however you call it incorrectly with Class.forName . Instead of Class.forName("com.mysql.cj.jdbc.Driver"); , it should be Class.forName("com.mysql.jdbc.Driver");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM