0

I can't connect to my data base while I run my .jar,but I can do it if I run my project in NetBeans. I'm using the JDBC-ODBC Bridge for the connection. Here's the function I use for the connection.

public Statement connection(Statement st){
    try {
        // connection avec la base de donnée DataBase.
        // On charge le driver ODBC
        Properties props = new Properties();
        // pour pouvoir afficher les accents et les caractères spéciaux!!
        props.put ("charSet", "ISO-8859-15");
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        cnx = DriverManager.getConnection("jdbc:odbc:DataBase", props);
        st=cnx.createStatement();
        //JOptionPane.showMessageDialog(null,"connection ouverte avec     succès");
    }catch(Exception e)
    {
        System.out.println(e.getLocalizedMessage());
        return st;
    }
    return st;
}

Sample Image:

enter image description here

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
A.JO
  • 235
  • 4
  • 14
  • 1
    The slow and buggy JDBC/ODBC brigde has been removed in Java 8 –  Dec 14 '16 at 10:30
  • And the error is? Post full stacktrace, not just `e.getLocalizedMessage()`. And not in French please. :) – lexicore Dec 14 '16 at 10:33
  • try to show the stacktrace with e.printStackTrace() and show to us the content of exception – thepaulo Dec 14 '16 at 10:34
  • I think he wants to say that his application is encapsulated in a jar file when tries to run his jar file then the application cannot connect to the MS Access Database, but when he runs his application from netbeans project it successfully connects to the Database, i don't think he has got any errors – Manish Sakpal Dec 14 '16 at 10:41
  • @Sonor I'have put an image of the full StackTrace – A.JO Dec 14 '16 at 10:47
  • @Manishsakpal You are right. But I don't know how to connect to my data base successfully from my .jar File – A.JO Dec 14 '16 at 10:49
  • Some libs are not present in your runnable jar file. follow this article: https://netbeans.org/kb/articles/javase-deploy.html – thepaulo Dec 14 '16 at 10:50
  • Have your application retrieve the string value `System.getProperty("java.version")` and display it to you. When you run the application from the jar file does it display a value that is different from when you run it in NetBeans? – Gord Thompson Dec 14 '16 at 12:24

1 Answers1

0

When a Java application is distributed to other users as a .jar file, we may not have control over the Java Runtime Environment (JRE) under which it runs. If the application has specific requirements for the JRE version then it should check the version explicitly.

For example, an application that uses the JDBC-ODBC Bridge must run under Java 7 or earlier because that component was removed from Java 8. So, the application code should do something like this:

int javaVersion = Integer.valueOf(
        System.getProperty("java.runtime.version").split("\\.")[1]);
if (javaVersion > 7) {
    System.out.printf(
            "This application is running under Java %d. " + 
            "Java 7 or earlier is required.%n", 
            javaVersion);
    System.exit(1);
}

java.com now distributes Java 8 by default. Most users who go to that website and click the handy "Free Java Download" button will almost certainly end up with Java 8 (or newer). If the application does not check the Java version and blithely tries to use the JDBC-ODBC Bridge then it will crash.

With the above check in place the user will see the considerably more helpful message

C:\__tmp>java -jar JdbcOdbcTest.jar
This application is running under Java 8. Java 7 or earlier is required.

If they run it using a JRE for Java 7 or earlier it will just work:

C:\__tmp>"C:\Program Files\Java\jre6\bin\java" -jar JdbcOdbcTest.jar
Welcome to our app!
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418