How to write directory of files in java for Commercial software? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to write directory of files in java for Commercial software?

think we use ms access database in us application, the directory of database is D:\Data\data.accdb. when our app is ready to install to other computers(client) this directory is not exist. so what is the best way to solve that?

23rd Jul 2017, 2:01 PM
hamid
hamid - avatar
6 Answers
+ 1
You could use relative paths in your application and store this database in your jar runtime path. Alternatively you could include a db file in the same path as the jar file and on app start, you could check if the db file exists in a specific folder for example the user's appdata folder\YourProgramName and copy the db file to that location if it does not exist.
23rd Jul 2017, 2:16 PM
Hassie
Hassie - avatar
+ 1
Let's say your jar was placed in C:\My Program And you placed your database file in C:\My Program\Data\data.accdb In the program you would do something like this to get the absolute path via a relative path method. File file = new File("Data/data.accdb"); String dbPath = file.getAbsolutePath(); I assume you would be using Jdbc to connect to the Ms access db. If yes, use the above method to set the Jdbc URL path.
23rd Jul 2017, 2:38 PM
Hassie
Hassie - avatar
0
ok, how to use relative paths? can you take an example about this question?
23rd Jul 2017, 2:34 PM
hamid
hamid - avatar
0
so now we should use dbPath. private static final String url = jdbc:ucanaccess://' "+dbPath+" ' "; is it true?
23rd Jul 2017, 3:19 PM
hamid
hamid - avatar
0
what is the problem in this error: public class ConnectionManager { File file = new File("Data/Uni.accdb"); String dbPath = file.getAbsolutePath(); public static String url = "jdbc:ucanaccess://' "+dbPath+" '"; private static String driverName = "net.ucanaccess.jdbc.UcanaccessDriver"; //private static String username = "root"; //private static String password = "triala"; private static Connection connection; private static String urlstring; public static Connection getConnection() { try { Class.forName(driverName); try { connection = DriverManager.getConnection(url); } catch (SQLException ex) { // log an exception. fro example: System.out.println("Failed to create the database connection."); } } catch (ClassNotFoundException ex) { // log an exception. for example: System.out.println("Driver not found."); } return connection; } }
23rd Jul 2017, 3:26 PM
hamid
hamid - avatar
0
In all of the exceptions can you also do ex. printStackTrace(), and paste the error.
23rd Jul 2017, 4:12 PM
Hassie
Hassie - avatar