3

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

tricknology
  • 933
  • 1
  • 14
  • 26

2 Answers2

1

user.dir is directory of system user. If you want to find file inside your application war, then use ServletContext.getRealPath()

Bartek Jablonski
  • 2,274
  • 18
  • 28
  • ServletContext was returning null because I'm a noob.. also I'm using tomcat7 and web.xml (from what I can see with my noobish eyes) is not needed. However, I did find instances of people putting web.xml in their /WEB-INF/ Why is that? and what does it do? If you have any resources that spell it out for n00bs like myself please share. Also, I fixed the problem and it's in the last edit. – tricknology Mar 15 '12 at 11:35
0

To access a database from an Applet using JDBC (read-only):

I made a connection similar to the one listed here http://www.shoaibinamdar.in/blog/?p=313 but insread of

    ds.setUrl("jdbc:sqlite::resource:"+
    getClass().getResource("sample.db").toString());

I put

    ds.setUrl("jdbc:sqlite:resource/sample.db");

the sample.db went in /myprojectname/resource/sample.db, with resource being in the same dir (myprojectname) as /myprojectname/src/

Hope this helps someone

tricknology
  • 933
  • 1
  • 14
  • 26