Java Database Connection without path - database

I have a question about Java and Databases. I am using an Microsoft Access Database and in order to be connected at the database i have to give full path at the driver.The Path Looks like the following. Also this path doesn't help me with the program portability.
String DBPath = "jdbc:ucanaccess://C://Users//theuser//Desktop//CostumerAppData.accdb";
Can i have database inside my project and give a simple path in order to be connected?
Thank you in advance about your responses.

Ucanaccess supports both relative and absolute path. If you're using a relative path, it must be relative to the current working directory. Path class (java.nio.file) may help . Maybe I'll be able to simplify this case solution in the next versions.

Related

H2 error [90011-187]

I am trying to create a database in linux where:
Its not in the user home
Don't require the client to inform the entire server path for the db file.
Needs to be different from the bin directory to prevent core dump failures.
The documentation says that you can use a url like this:
jdbc:h2:file:data/sample
but this simple url doesn't work and get the follow error:
Exception in thread "main" org.h2.jdbc.JdbcSQLException: A file path
that is implicitly relative to the current working directory is not
allowed in the database URL
"jdbc:h2:file:db/datadb;TRACE_LEVEL_FILE=3". Use an absolute path,
~/name, ./name, or the baseDir setting instead. [90011-187]
Observation: I know you can use ".", but what will be the url from the client in that case?
The documentation is wrong. I will update it.
jdbc:h2:file:data/sample
It should be:
jdbc:h2:file:./data/sample
Many users ran into problems because they used something like jdbc:h2:test and then either didn't find the database file, or created a second database when running the application in a different directory. That's why in version 1.4.x, now relative path only work when using ., as in jdb:h2:./test.
By the way, you have asked this question in the H2 Google Group as well.

Dart: Accessing a resource out side the project|web/ directory

I have a web-app(browser based) which needs to access a folder full of icons that resides outside the web folder.
This folder MUST be outside the web folder, and would ideally exist outside the project folder all together
however, when specifying the path to the folder neither "../" or making use of a symlink will work
when the page attempts to load the image I always get
"[web] GET /Project|web/icons/img.png => Could not find asset Project|web/icons/img.png."
however I set the image source to "../icons/img.png"
how can i get dart to access this file properly
PS: I attempted a symlink to another part of the filesystem (where the images would be kept ideally) however this did not work either.
The web server integrated into DartEditor or pub serve only serves directories that are added as folders to the files view. When you add the folder to DartEditor you should be able to access the files. This is just for development.
You have also to find a solution for when you deploy your server app. It would be a hazardous security issue when you could access files outside the project directory. Where should the server draw the line? If this would be possible your entire server would be accessible to the world.
Like #Robert asked, I also have a hard time imaging why the files must not be in the project folder.
If you want to reuse the icons/images between different projects you could create a resource package that contains only those images and add them as a dependency to your project.
If you want a better answer you need to provide more information about your requirements.
If you wrote your own server (by using the HttpServer class) it may be possible to use the VirtualDirectory to server your external files.
Looking at look the dartiverse_search example may give you some ideas.
You could put them in the lib directory and refer to them via /packages/Project/...
Or in another package, in which case they would be in a different place in the file system. But as other people have said, your requirement seems odd.

Database Location in Scheme

I have a program which make use of a local database (sqlite3 and i use db module). What i want to do is using the database without knowing where it is.
For example, if i code the location of database in my program (like C:/my documents/my program/localdb.db), it is working correctly.
But if i just write "localdb.db", it doesn't find the database even if database is in the same folder with the .rkt file which use the database. (i dont know how but in earlier versions of my program, it was working).
Thus, how can i ensure to use the database without coding its location?
Thanks a lot!
Instead of "localdb.db", try using a runtime-path like this:
(define-runtime-path localdb "localdb.db")
and use localdb in place of the path string. You'll need to (require racket/runtime-path).

2 files with same name in eclipse

I have 2 source folders in my project:
src/main/resources/sql/oracle
src/main/resources/sql/sqlserver
They both have a file called mh1.sql.
The project I'm working on used to support only oracle database, so it just use ClassPathResource("mh1.sql") to load the sql file directly, now I need to support different kinds of database, and switch to the correct sql file according to the database type we're using. So, is there any good way to go? without any big impact on the old project. any rough ideas?
BTW, I find that after compilation, I can only find one mh1.sql under bin folder, I'm a new guy in using Eclipse, and I'm curious to know if it's possible to output these 2 folder oracle and sqlserver to the bin folder and each contains its own mh1.sql file?
As for your second question without knowing your exact Eclipse project settings it's of course close to impossible to tell why you're not seeing the oracle and sqlserver subfolders in your bin folder. However, it should be obvious that this being fixed is a prerequisite for your first problem.
Have a look at the ClassPathResource docs, they tell you that you can/should provide a path to your resource rather than just the name. Hence, you can use ClassPathResource("sql/oracle/mh1.sql").
Having said all that you might also just dump the two files in src/main/resources/sql/ (omitting the subfolders) and give them unique names: ora-mh1.sql and mssql-mh1.sql.

When storing Images in the File System, Use relative paths or absolute paths?

I am working on a web app and I decided (after reading many post on SO) to store the actual images in the file system and to store the metadata in the DB.
Should I store a relative path or an absolute path.
I can think of some advantages for each of the choices.
Absolute:
Pros:
It is obvious where the file is even to other apps reading the DB
Can put the photos anywhere on the drive (would require an handler)
Cons:
Need to convert the absoulte path to a relative path for use in the site or create a handler
If I migrate to another server I may have to change all the paths
Relative:
Pros:
Simply add the link to the html and it works
Cons:
If I change the app root I have to move the pictures or change all the paths
Have to put the pictures in a public directory (Or I gain nothing over the absolute path)
Ok these are some of things going on in my head right now.
I can't decide.
I would store a relative path in the database. This gives you the greatest flexibility. Loading images is a simple matter of prepending an "IMAGE_ROOT" variable (which should probably be configurable) to get the filesystem path. This is important because you might want to move where the images are stored (put them on a faster drive, for example). Then it is simply changing the configurable IMAGE_ROOT.
When putting a reference to the image into a page, I'd put the full URL. Again, this is simply adding a URL_ROOT to the relative path. This gives you the advantage of being able to easily switch servers if you find load requires dedicated servers for serving images.
If you are interested in the portability of the info in your database, just store a base path in the database as well. That way, when you move the files, all you need to do is modify the base path that you've stored in the DB. This information should be stored separately from the file paths. Storing such information in the same row would create a lot of unnecessary duplication.
I prefer to store them as relative paths so the application is not dependent on its location. With ASP.NET there is the "~" to automatically signify the application root, you could do the same and then simply replace it with a constant when using it, that way you do not have to worry if you change the app root.
If you're using SQL Server 2008, you can solve this problem neatly with the new FILESTREAM data type.
I concur. Storing relative paths means less info in the db and if you store a base path somewhere, you only need to modify it once. You then build your full urls by adding the shorter link from the database to your basepath.

Resources