Export executable jar

0

I am creating a project that has a database (in Access 2016), the problem is that I do not know how to export the database with the executable jar, if it could be done, should I change the path of the base in the code? This is my class'

package Swing.pruebas.SqlFiles;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;  
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

public class ConnectDB {
private Connection con;
private Statement smt;

@SuppressWarnings("unused")
private final String controlador;
private final String nombre_db;
private final String usuario;
private final String contraseña;

public ConnectDB(){
    this.controlador = "sun.jdbc.odbc.JdbcOdbcDriver";
    this.nombre_db = "C:\Users\USUARIO\Desktop\eclipse_java\TutorialesBlog\src\Swing\pruebas\Usuarios.accdb";
    this.usuario = "";
    this.contraseña = "";
}

public boolean establecerConexion() throws SQLException{
    try{
        con = DriverManager.getConnection("jdbc:ucanaccess://"+this.nombre_db, this.usuario, this.contraseña);
    }catch(SQLException e){
        JOptionPane.showMessageDialog(null, "Error al realizar la conexión: "+e);
        System.out.println(e);
        return false;
    }

    try{
        this.smt = this.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    }catch(SQLException e){
        JOptionPane.showMessageDialog(null, "Error al realizar la conexión: "+e);
        return false;
    }
    return true;
}

public ResultSet ejecutarSentencia(String sql) throws SQLException{
    ResultSet rs;
    rs = this.smt.executeQuery(sql);
    return rs;
}

public int ejecutarSentenciaUpdate(String sql) throws SQLException{
    int rs;
    rs = this.smt.executeUpdate(sql);
    return rs;
}
}

Which path should I assign to the variable db_name so that the program can use the DB, on any computer. Will I have to put the DB in the executable JAR exported? The current code does work.

    
asked by bruno Diaz martin 22.10.2016 в 01:30
source

1 answer

4

Let's see, assuming that Access database is only your application and no one else uses it, your thing is to package it within the JAR in which you are going to distribute your application, as well as to point out the problem (for example , you could have a subfolder called "resources", copy it there and have this subfolder be included in the JAR).

From there, that db_name is calculated in the following way:

URL url = Solution.class.getResource("resources/Usuarios.accdb");
nombre_db = url.getPath();

To create the jar with that folder is as simple as

jar -cvf nombre_del_jar.jar bin/

Assuming that the .class is in the bin folder and that the resources folder is also copied in there. If not, you just have to adapt your code to include or not the name of the folder and add it to the end of the jar command to include its contents.

I hope I have helped you.

    
answered by 24.10.2016 в 13:56