I am having trouble accessing my database on a server. I am using code snippet this to try to connect.
String currentDir = new File(".").getAbsolutePath();
String currdir = System.getProperty("user.dir");
System.out.println("EPIC currentDir = " + currentDir);
System.out.println("EPIC currdir = " + currdir);
String builder = "jdbc:sqlite:"+ currentDir +"dbname";
Now.. the printlns are telling me that:
currentDir = /usr/local/cpanel/base/.
currdir = /usr/local/cpanel/base
and I know that this is going to give SQLException because my db is located in ../WEB-INF/lib/dbname So no surprise that I get this error message:
java.sql.SQLException: opening db: '/usr/local/cpanel/base/.dbname': Permission denied
So my question is this.. what is after base and how does that relate to my directory (from where the servlet is run)? I don't have SSH to go looking for it (rolls eyes) and if anyone can shed some light on the subject I would be greatful! Thanks in advance!
Edit: Weird stuff
I am testing all this with these commands..
String currentDir = new File("dbname").getAbsolutePath();
String currdir = System.getProperty("user.dir");
System.out.println("EPIC currentDir = " + currentDir);
System.out.println("EPIC currdir = " + currdir);
System.out.println("EPICER = " + this.getClass().getResource("dbname").toString());
System.out.println("EPICEST! = ");
this is the output:
EPIC currentDir = /usr/local/cpanel/base/dbname
<< this one gives me "Permission Denied"
EPIC currdir = /usr/local/cpanel/base
<< this one gives me "Permission Denied"
EPICEST! =
<< this is ok.. but wait.. NO EPICER???
System.out.println("EPICER = " + this.getClass().getResource("dbname").toString());
DOES NOT PRINT!! what? is my log being filtered to prevent me from viewing the path? Also, none of those paths work :(
<<<<---UPDATE--->>>>
Did not print because "this" was returning null and i was actually getting NPE on this line; thus, no println. duh.
LAST EDIT: SOLVED!
Ok, so I was going about it all wrong and I'm posting this just in case some other poor guy is about to waste three days like I did..
this.getClass().getResource("dbname").toString())
"this" was coming back null because what? SURPRISE, no ServletContext so
getServletContext()
was also returning null ( something I experimented with later)
I learned from other resources on the internets that the way I was going about doing this was all wrong because, every time I re-deploy the WAR file, the DB will be overwritten.. so I created a little directory in my /home/username/ called dbfiles and used the commands:
String dir= "jdbc:sqlite:/home/username/dbfiles/dbname.db";
connection = DriverManager.getConnection(dir);
and it works!!
Thanks everyone