0

Friends please suggest a solution

I have completed a java swing maven project using eclipse IDE and MySql database in Ubuntu 20.04 OS. Exported the project as Runnable jar (Launch configuration as my main function class and library handling radio button selected is extract required libraries into generated jar).

The runnable jar then, I have opened in Windows 10 OS. By double click the main window opens and I can open only 2 windows from the menu bar of the main window and these two window class are not having sql connection. All other windows are having database connection and are not able to open.

In widows 10 I have installed MySql server 8.0.22 (Using legacy authentication method Retain MySql 5.x Compatibility)and Connector j 8.0.22 and I am able to access my database using Query Browser.

I am using maven dependency of mysql-connector-java 8.0.22 in my Project. And my connection Class is hereunder.`


public class MysqlConnector {
    Connection con=null;
    public static Connection dbConnector() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306","sarams", "password");
            return con;
    }
        catch (Exception e) {
            return null;
            
        }
             
        }
}     
Renoj Joseph
  • 93
  • 10
  • have you included the mysql driver lib in your classpath? maybe you can log your exception and execute the jar file via command line to have some clue of what the exception is about? – pedrohreis Oct 27 '20 at 10:49
  • dear pedrohreis I am using only one ,jar file (rs2xml.jar) and it is there in my lib class path and all athors including mysql driver are included as maven dependencies. Executed the jar in command line and again getting no exception. Then I have made a simple project with only one window, made runnable jar. and opened. Now getting Exception from java virtual launcher "Error: A JNI error has occured, please check your installation and try again". – Renoj Joseph Oct 27 '20 at 12:57

2 Answers2

0

First make sure you have all the drivers installed and added to the maven

Example Using void

public class MysqlConnector {

    /**
     * Connect to a sample database
     */
    public static void connect() {
        Connection con = null;
        try {
           // db parameters, where the database is being stored, so it could access it
            String url = "jdbc:mysql://localhost:3306","sarams", "password";
           
            // create a connection to the database
            con = DriverManager.getConnection(url);
    
            System.out.println("Connection to mysql has been established.");
    
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    
    // establishing connection
    public static void main(String[] args) {
        connect();
    }

Example Using Connection

private Connection connect() {
        // MySQL connection string
        String url = "jdbc:mysql://localhost:3306","sarams", "password";
        Connection con = null;
        try {
            con = DriverManager.getConnection(url);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return con;
    }
  • I have corrected my connection "as per example using connection". Still the problem was not solved. Then I have made a simple project with only one window, made runnable jar. and opened. Now getting Exception from java virtual launcher "Error: A JNI error has occured, please check your installation and try again". – Renoj Joseph Oct 27 '20 at 13:13
0

The problem was solved and it was just a java conflict. Compiled using jdk 11 and tried to run JRE 1.8. removed JRE and installed JDK 11 and the problem was solved.

Renoj Joseph
  • 93
  • 10