java - How to get realative path for database? -
i developing jframe store data in database using textfields , all. have following code database connection. using sqlite database.
**connectdatabase.java** /* * change template, choose tools | templates * , open template in editor. */ package shreesai; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import javax.swing.joptionpane; /** * * @author deeprocks */ public class connectdatabase { connection con = null; public static connection connecrdb(){ try{ class.forname("org.sqlite.jdbc"); connection con = drivermanager.getconnection("jdbc:sqlite:g:\\development\\project\\database\\shreesai.sqlite"); return con; } catch(classnotfoundexception | sqlexception e){ joptionpane.showmessagedialog(null,"problem connection of database"); return null; } } }****
/* working fine want if export project jar , client run jar different path then????*/
i've tried using code
public class connectdatabase { connection con = null; public static connection connecrdb(){ try{ string dir = system.getproperty("user.dir"); class.forname("org.sqlite.jdbc"); connection con = drivermanager.getconnection("jdbc:sqlite:"+dir+"shreesai.sqlite"); return con; } catch(classnotfoundexception | sqlexception e){ joptionpane.showmessagedialog(null,"problem connection of database"); return null; } } }
even tried 1 :
public class connectdatabase { connection con = null; public static connection connecrdb(){ try{ string dir = system.getproperty("user.dir"); string maindir = dir + file.separator + file.separator ; class.forname("org.sqlite.jdbc"); connection con = drivermanager.getconnection("jdbc:sqlite:"+maindir+"shreesai.sqlite"); return con; } catch(classnotfoundexception | sqlexception e){ joptionpane.showmessagedialog(null,"problem connection of database"); return null; } } }
but nothing think there must problem double backslashes cause dir returning : g:\development\project\database. , want g:\\development\\project\\database\\
any help?
the ways you're tried not wrong, it's right on computer, so, solution easy, bring .sqlite
file inside src
, maybe want create package named database
, it's not required, it's better.
if .sqlite
file inside source that:
src/shreesai.sqlite
....=drivermanager.getconnection("jdbc:sqlite:src/shreesai.sqlite");
don't want absolute path too, , avoid using backslashes directories( \\
) because of different windowing platforms, use slash(/
) instead.
Comments
Post a Comment