Search code examples
javamysqljarconnectivity

database connectivity fails after creating jar file


I created a project with notepad.And running well in cmd with mysql server database.

But after creating a jar file of same project then jar file is not able to connect to the server database.so don't know what to do next? Even though other systems (only installed java not mysql) are unable to connect to the server database after running from either cmd or jar file.


Is it necessary for every systems to have java and mysql installed on other systems to run? How to overcome from such problem. I want to distribute it to the users but i am unable to do that... code--->Class.forName("com.mysql.jdbc.Driver");

Connection conn=DriverManager.getConnection("jdbc:mysql://serverhostname:3306/databasename"‌​,"username","password");

Solution

  • Here is the solution,
    following are the content of manifest.txt file

    Main-Class: filename
    Class-Path: . mysql-connector-java-5.1.33-bin.jar
    

    mysql-connector-java-5.1.33-bin.jar connector must be in the same directory so that the jar file include it.
    This connector can be easily downloaded or can be found in mysql folder under program files
    After that from the cmd, command must be executed

    jar -cvfm jarfilename.jar manifest.txt *.class
    

    A new jar file will be created based upon your class file and manifest,then it can be easily runnable with double click and the server will respond same as that of cmd mode.