0

I'm trying out Gradle and JDBC for the first time. I have the following gradle.build file:

apply plugin: 'java-library'
apply plugin: 'java'

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    compile 'mysql:mysql-connector-java:5.1.36'
    testImplementation 'junit:junit:4.12'
}

In my HelloMySQL.java file, I'm trying to establish a connection to the MySQL database WORLD(Verified the URL and port). I have the following code:

package Hello;
import java.sql.*;

public class HelloMySQL {
    public static void main(String[] args) {
        Connection conn = null;
        try{
            System.out.println("Trying to connect...");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/WORLD", "root", "root");
            System.out.println("Creating statement...");
                Statement stmt = conn.createStatement();
                String sql;
                sql = "SELECT * FROM City LIMIT 10";
                ResultSet rs = stmt.executeQuery(sql);
                while(rs.next()){
                  int id = rs.getInt("id");
                  String name = rs.getString("name");
                  String countryCode = rs.getString("CountryCode");
                  System.out.println(id + " " + name + "\t " + countryCode);
                }

        } catch(SQLException e){
            System.out.println(e.toString());
        }

    }
}

I've also tried adding this line before the DriverManager.getConnection:

Class.forName('com.mysql.jdbc.Driver');

but it throws a java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Am I doing something wrong with the gradle build file?

EDIT: I have not added the jar for MySQL connector to the project. Gradle will download it for me, right?

ayushgp
  • 4,891
  • 8
  • 40
  • 75
  • @anant666 It was boilerplate code generated by eclipse. I forgot to remove it from the code I pasted here. – ayushgp Apr 01 '17 at 06:26
  • How are you **running** it? The driver needs to be on the runtime classpath. – Mark Rotteveel Apr 01 '17 at 06:36
  • @MarkRotteveel I'm new to Java. I'm running it using Eclipse's _Run_. I've tried cleaning the project after every change I make to the build file. I also tried changing the dependency type from `compile` to `runtime`. – ayushgp Apr 01 '17 at 06:41
  • I've never used Gradle from Eclipse, so I don't know how it interacts. As Eclipse doesn't use Gradle by default: did you actually create it as a gradle project? – Mark Rotteveel Apr 01 '17 at 06:49
  • @MarkRotteveel Yes I created it as a gradle project. Do you know how I can compile and run using gradle CLI? – ayushgp Apr 01 '17 at 06:54

1 Answers1

1

I found the problem. Not exactly a problem but still posting it for people who might struggle with Eclipse.

After you make changes to the gradle.build file, Cleaning the project won't do. You'll need to refresh the gradle project as well.

Right click in the build.gradle file -> Gradle -> Refresh Gradle Project

ayushgp
  • 4,891
  • 8
  • 40
  • 75