Java DB - Starting Server within application

2019-08-16 13:11发布

问题:

I am aware that there are tons of questions around about this problem but I tried every ungodly way to fix it, still no result.

I have created a "test" Derby database within Netbeans. Everything works fine, I can execute SQL commands, alter tables and all within application. But i have to start the server everytime when I open netbeans. But I want my application's itself to start the server with codes. I guess i tried everything to do it.

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

server = new NetworkServerControl(InetAddress.getLocalHost(),1527);
server.start(null);

conn = DriverManager.getConnection("jdbc:derby://localhost:1527/test;create=true;");

This is what i get if i dont start the server manually.

java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.

I tried different drivers, i tried putting jar and sdk files to my app file. I added permission to socket 1527 to java.security file.

No matter what i do, i must start the server manually or i get this chunk of code.