简体   繁体   中英

Accessing Java DB outside NetBeans

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 .

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

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

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, ie your system.

What you need to do is tell your application to use JavaDB as an embedded database, ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM