I have nearly finished a project developing a simple image catalogue application using Java and Netbeans.
I have only now realised that my database is not embedded so when I create the JAR file and try to run it (external to Netbenas) i get an error (error connecting to localhost).
This is the code at present that connects to the database. All works fine when run in Netbeans.
//conect to database
String host = "jdbc:derby://localhost:1527/Catalogue";
con = DriverManager.getConnection(host);
My question is (and I can't seem to find a simple solution) is there a way to just create a new embedded DB with the same name and table and rows etc and then just adjust the code so when i run the JAR file externally to Netbeans all will work OK?
I am imagining the code will be something like:
String host = "jdbc:derby:'path to db'/Catalogue;
Am I on the right track? I have tried this (I think) but it does not seem to work…
Thanks for any assistance
<<<<<< UPDATE >>>>>>>
I have now created an embedded database and created a table (named photos) with exactly the same details as my original (non-embedded) database.
I have changed the connection in the code to be:
String host = "jdbc:derby:catalogueEmbed;create=true";
Now when I run the program in Netbeans it says: "Table/view PHOTOS does not exist"
Do I need to run and sql statement in my code to create the table and row data?
Thanks for any assistance
Related
So I generated a new Spring boot application using jhipster. Upon navigating to the H2 console, the following information is shown.
I am able to login, and I can verify tables exist.
Next, I copy the link, jdbc:h2:file:./target/h2db/db/ikdoemee and try to connect to the H2 database using IntelliJ which shows successful.
However, the IntelliJ database view isn't showing any tables.
I tried adding the following parameters to my JDBC url, but no tables show up.
jdbc:h2:file:./target/h2db/db/ikdoemee;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1 ;DATABASE_TO_UPPER=false
Why am I unable to get my tables visible in IntelliJ?
My .h2.server.properties is
#H2 Server Properties
0=JHipster H2 (Disk)|org.h2.Driver|jdbc\:h2\:file\:./target/h2db/db/ikdoemee|ikdoemee
webAllowOthers=true
webPort=8082
webSSL=false
EDIT: My target folder:
You need to activate h2 "Automatic Mixed Mode" by changing the spring.datasource.url of the application-dev.yml file.
example URL: jdbc:h2:./target/h2db/db/'PROJECTNAME';AUTO_SERVER=TRUE
The URL for the connection to the h2 database should look like that:
jdbc:h2:/home/'USERNAME'/IdeaProjects/'PROJECT'/target/h2db/db/'PROJECTNAME';AUTO_SERVER=TRUE
if that doesn't work try also to delete the target folder and refresh the h2 connection
To resolve this issue I installed Buddy JPA plugin.
It created profile automatically and I compared it with mine.
the difference was in parameter DB_CLOSE_ON_EXIT=FALSE
I tried it with manually created profile and it works as well.
The result link is:
jdbc:h2:file:./work/h2db;DB_CLOSE_ON_EXIT=FALSE
The only solution that worked for me at the moment is to put the direct path to the file in Path field without extension.
After that I could see my tables in IDEA.
click to preview settings
Note:
Don't forget to also add parameter AUTO_SERVER=TRUE to datasource.url, as pointed out by #Unknown_Energy.
My apps versions:
Idea 2018.3.5 UE
h2 1.4.197
h2 base name: demo.mv.db
this bug is related to these issues:
Data Source detection
Intellij IDEA and H2 DB file recognition problem
When running the application, the log will show something like
n.w.config.DatabaseConfiguration 1: H2 database is available on port 18080.
Then change Connection Type to Remote and port to the port just showed,
append the db file location to the url (with out extension) just like ./build/h2db/db/projectname, User will be the ProjectName.
The final url will like jdbc:h2:tcp://localhost:18080/./build/h2db/db/projectname
This worked for me, I'm not a English native speaker, sorry for my poor English.
I'm currently working on a project that requires me to connect our Java Project to a SQLite database with jdbc.
We've been using a database.db file located in the main/resources folder for a while without any issues. Now we're trying to switch to a hosted database, because this project is intended for multiple users and so far, we've had to include the database.db file with every git commit.
So, the most obvious first choice would be to instead of using the local resource, link to a hosted file. We have hosted our database.db file on a public server (yeah, yeah, but data security is not relevant for this project) and changed the resource to the hosted file, but it doesn't seem to work and returns "Resource not found" errors. We can't seem to get the syntax right. Here's what we have:
[...]
public Connection connectOrCreateDatabase(String databaseName) throws ClassNotFoundException, SQLException{
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:mysql:http://server.com/"+databaseName+".db");
[...]
Is there any way to get this to work?
Or is http just not supported at all?
Thanks in advance!
No, you can't use it over HTTP and pretend it's on the file system.
Why? You can read the db file over HTTP, but you can't write to it, making inserts and updates impossible.
You need to set it up in server mode and connect to it like a "real" database.
From a java application (junit test) I am trying to restore a derby file database. The derby server has been started as a standalone server (so not embedded). I am using the following code.
String url = "jdbc:derby://localhost/V4_0_0/ambikas";
String urlToRestoreFrom = "C:/javadev/workspace/trunk/ambi-kas-lib/derby/V4_0_0_backup/ambikas";
DriverManager.getConnection(url + ";createFrom=" + urlToRestoreFrom);
log.debug("db restored ...");
When I run the code no exceptions are thrown from Derby's side. Everything seems to go fine but no restore has taken place. The directory "url" where I want to restore the db contains the current database. According to the derby documentation this should work however. The restore functionality should replace the current db.
I know there have been some threads before on this topic but none of them seem to solve my problem. I am stuck with it for weeks now.
It's not "createFrom=", it's "restoreFrom=".
See these docs for more information: http://db.apache.org/derby/docs/10.9/adminguide/cadminhubbkup98797.html
This is in continuation to my previous question
I'm trying to embed a derby database in my web application. I'm able to embed it, though I am facing glitches. Here's the snapshot of my project structure.
I wonder why class.forName("org.apache.derby.jdbc.EmbededDriver") is throwing exception?
java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbededDriver
To overcome this, I have used
DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
Which works well. Why is it so? Also, Where would it create database if coded like this. I cannot spot the database.
connection = DriverManager.getConnection("jdbc:derby:MyDbTest;create=true");
I checked in tomcat webapps and eclipse workspace, I didn't find database.
If given this way, I can spot it.
connection = DriverManager.getConnection("jdbc:derby:E:/MyDbTest;create=true");
class.forName("org.apache.derby.jdbc.EmbededDriver")
Threw an exception because there is a 'd' missing from embedded.
See here for information about specifying the location of databases on the file system.
connecting to a file-based derby database
I developed an application with a java db database .I cannot access the database records when I close the netbeans IDE with the message "Error connecting to server Localhost on port..." My connection code to the Database is :
String host="jdbc:derby://localhost:1527/Employee;create=true";
String user="admin";
String pass="admin";
con=DriverManager.getConnection(host,user,pass);
How do I fix the problem?
Netbeans automatically starts the Derby server; you can see that in the "Services" tab (Ctrl-5).
You'll have to start the database server by hand if you don't use Netbeans; see the doc.
Presumably your Derby database is hosted in NetBeans? You'll need to create a standalone database.
You would have to probably start your database before connecting (you are using server mode). Have a look into Vogella tutorial on Derby db connection from java application: http://www.vogella.de/articles/ApacheDerby/article.html
I guess NetBeans has an embedded DB instance.
Try to use
jdbc:derby:/MyFolder/MyDatabase/Employee;create=true
or
jdbc:derby:C:\MyFolder\MyDatabase\Employee;create=true
if you do not need to access the DB from multiple applications.
You can use JavaDB (aka Derby) either by connecting to a JavaDB Network Server or by using it as an embedded DB when your application opens the DB files itself.
Currently, your application is connecting to a Network Server started by NetBeans, as your URL is telling to connect to port 1527 on localhost, i.e. your system.
What you need to do is tell your application to use JavaDB as an embedded database, i.e. it should manage the database itself instead of getting Netbeans to do it instead. You can do this just be changing the URL to something like:
jdbc:derby:Employee;create=true
You may need to tweak that URL depending on where the database files are stored relative to your application's working directory.
Only one application can have the DB open at one time. So when you're doing this NetBeans won't be able to open the database, and if NetBeans has the database open your application won't be able to open it. So you may find you want to reconfigure NetBeans as a DB client.
the simplest way of dealing with these problems is creating batch files..
first of all build your java database program.. the tricky part is to start the server. the jderby is a server so it needs to be started.. that's why you start the server in netbeans. so download db derby files from "http://db.apache.org/derby/releases/release-10.8.2.2.html". after you download these files, copy your netbeans project in those db jderby files.. go and copy your database folders from where they will be saved.. and paste them in the db jderby files.. now open notepad and type
#echo
start (PATH)
start (PATH)
the first path take the path of the file named start network server.bat
the second path take the path of the jar file of your main project.
Now save your the notepad as setup.bat and run the batch file afterward.. and ur program will start the server and running your application at once...
NB: you can use a different name from setup, any of your choice but the extension bat must be available