简体   繁体   中英

Error connecting to server localhost on port 1527 with message Connection refused

i am create a java project and i want to use derby data base and and i am configured database and create database with the name of /home/user/TestDB and create a table user and insert 3 to 4 values into it and write a code to get the data from database but when i connect dada base i got connection refuse error, i am use

 DB URL : jdbc:derby://localhost:1527/home/user/TestDB

error logs :

java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused.
    at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
    at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at test.derby.util.DerbyUtil.getConnecation(DerbyUtil.java:34)
    at test.derby.dao.TestDAO.getData(TestDAO.java:20)
    at test.derby.dao.TestDAO.main(TestDAO.java:39)
Caused by: org.apache.derby.client.am.DisconnectException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused.
    at org.apache.derby.client.net.NetAgent.<init>(Unknown Source)
    at org.apache.derby.client.net.NetConnection.newAgent_(Unknown Source)
    at org.apache.derby.client.am.Connection.<init>(Unknown Source)
    at org.apache.derby.client.net.NetConnection.<init>(Unknown Source)
    at org.apache.derby.client.net.NetConnection40.<init>(Unknown Source)
    at org.apache.derby.client.net.ClientJDBCObjectFactoryImpl40.newNetConnection(Unknown Source)
    ... 6 more
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)

i think you are not start your derver server in specific port which you are using. you need to start server using below command on localhost with default port 1527

startNetworkServer -h 0.0.0.0

if you want to change the port you can use below command :

startNetworkServer -p 3301  // new port number

for more you can refer below link;

http://www.vogella.com/tutorials/ApacheDerby/article.html

may be this error occur because of following reasons:

1) Firewall is not permitted for host-port combination

2) Client and Server, either or both of them are not in the network.

3) The server is running but not listening on the port, a client is trying to connect.

4) Server is not running.

5) Incorrect protocol in Connection String

This error occurs just because your database is not connected.

NETBEANS

Go to Services>Database>Java DB>(right click) After that simply right click on your database and connect database.

Best Solution

To execute the database program

Step 1: First database server needs to be started

Open a command shell and change to a directory that will hold the database

files(in this following example the database going to stored in

D:\\java2019\\Database)

D:\\java2019\\Database>"C:\\Program Files\\Java\\jdk1.8.0_60\\db\\bin\\startNetworkServer“

Wed Mar 13 10:36:36 IST 2019 : Security manager installed using the Basic server security policy. Wed Mar 13 10:36:37 IST 2019 : Apache Derby Network Server - 10.11.1.2 - (1629631) started and ready to accept connections on port 1527

The default port number is 1527 we can also change the port number by the following
cmd

D:\\java2019\\Database>"C:\\Program Files\\Java\\jdk1.8.0_60\\db\\bin\\startNetworkServer" -p 1567

Wed Mar 13 10:46:33 IST 2019 : Security manager installed using the Basic server security policy. Wed Mar 13 10:46:33 IST 2019 : Apache Derby Network Server - 10.11.1.2 - (1629631) started and ready to accept connections on port 1567

Step2: compile and execute the java program

Open another command shell and change to a directory that will hold the database files. set the classpath for derbyrun.jar

set classpath=".;C:\\Program Files\\Java\\jdk1.8.0_66\\db\\lib\\derbyrun.jar”

Step3: Finally the database server needs to be stopped

Open another command shell and change

D:\\java2019\\Database>"C:\\Program Files\\Java\\jdk1.8.0_60\\db\\bin\\stopNetworkServer" Wed Mar 13 10:56:04 IST 2019 : Apache Derby Network Server - 10.11.1.2 - (1629631) shutdown

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