0

I am coding a server at the moment I am developing it on a windows system with intellij idea and I want to run it on a Linux server(Debian). On my windows PC all runs great I can connect my program to the SQL-server on my windows but when I "compile" the program and let it run on a Linux server I always get a java.sql.SQLException: No suitable driver found for jdbc:mysql/localhost:3306/... exception and a java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception.

My code

public class DataBaseHandler {




//Dieses Objekt stellt die Verbindung zur Datenbank da
private Connection connection;
private String dbName = "legitName";
private String hostName = "localhost:3306";
private String loginName = "legitName";
private String pw = "legitPW";




private Controller c;
public DataBaseHandler(Controller c){
    this.c = c;
    connectToDb();

}

//Methode um eine Verbindung mit der Datenbank herzustellen
private void connectToDb(){

    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("rude german words here");
        e.printStackTrace();
    }
    String connectCommand = "jdbc:mysql://"+hostName+"/"+dbName+""+"?useSSL=false&user="+loginName+"&password="+pw;
    try {

        connection = DriverManager.getConnection(connectCommand);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    //tstQuery();

}

so as already mentioned this runs perfect on my windows and I can even execute the "testQuery()" method which gives me back some resluts but on my server the line Class.forName("com.mysql.jdbc.Driver").newInstance(); causes the "ClassNotFoundException" and the DriverManager.getConnection() line causes the SQLexception. In Intellij under project structure I added the

mysql-connector-java-5.1.41-bin.jar

to the Classpath under SDK and also to the Global Libaries.

The Linux server is also running MySql DB.

alovaros
  • 476
  • 4
  • 23
  • 1
    When you run it on the Linux server, the mysql-connector-java-5.1.41-bin.jar must be in the classpath . – Arnaud May 03 '17 at 13:22
  • 1
    put the jar into the java classpath when your run `java -cp .:mysql-connector-java-5.1.41-bin.jar YourMainClass` – Sergey Benner May 03 '17 at 13:23
  • Please read the last sentences :) I've added it to the classpath of my project. But if I've done it wrong, what surely is possible, it'd be nice if you correct me because than I have no idea how to do it :D – alovaros May 03 '17 at 13:24
  • Are you generating a `.war` file from your project or a `.jar` ? – Erich Kitzmueller May 03 '17 at 13:26
  • @SergeyBenner thanks first for you comment. I am a real Linux noob so could you explain this maybe more pricisly? So if my server programm is named **server.jar** and my main is called **Controller.class** would it be like `java -cp server.jar .:mysql-connector-java-5.1.41-bin.jar Controller.class `? – alovaros May 03 '17 at 13:28
  • @ammoQ I am creating a .jar archive – alovaros May 03 '17 at 13:29
  • 1
    It would rather be `java -cp server.jar:/mein/persoenlicher/pfad/zu/mysql-connector-java-5.1.41-bin.jar Controller` – Erich Kitzmueller May 03 '17 at 13:30
  • 2
    `java -cp .:server.jar:mysql-connector-java-5.1.41-bin.jar full.package.name.if.any.Controller` – Sergey Benner May 03 '17 at 13:31
  • 1
    If you think you added that JAR to the CLASSPATH, and you still get CNF exception, it means you did it incorrectly. Believe the JVM. – duffymo May 03 '17 at 13:32
  • @duffymo sooooo I need to add the complete.zip instead of .jar fomr the connector? :'D – alovaros May 03 '17 at 13:36
  • No. Wrong. You need to add the MySQL connector JAR correctly at runtime. – duffymo May 03 '17 at 13:36
  • @SergeyBenner that sadly didn't help :/ – alovaros May 03 '17 at 13:37
  • @duffymo sorry could you explain this a bit more? – alovaros May 03 '17 at 13:37
  • 1
    You either understand Java CLASSPATH or you don't. Please do some research. You can't write Java without knowing how CLASSPATH works. – duffymo May 03 '17 at 13:38
  • 1
    Where on your linux box is `mysql-connector-java-‌​5.1.41-bin.jar` located? – Erich Kitzmueller May 03 '17 at 13:39
  • Make sure your connection URL format is correct: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html – duffymo May 03 '17 at 13:39
  • Thanks to all it works now :) wrote the wrong path for mysql-connector! – alovaros May 03 '17 at 13:41
  • @alovaros put all the jars into one folder for simplicity and use there the commandline i wrote above. you may also try and use `--classpath` instead of `-cp` just pretty much the same. – Sergey Benner May 03 '17 at 13:42
  • I'll do thank you :) – alovaros May 03 '17 at 13:44

0 Answers0