Connection to a derby database in eclipse [duplicate] - java

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

Related

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

JDBC connection doesn't work with Sql Server 2012 or higher

The code below works with Sql Server 2008 but not with Sql Server 2012 and 2014 (v 11 or 12)
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://myhost\\myinstance:1433;database=mydb";
connection = DriverManager.getConnection(url,"username", "pwd");
I'm using Java Runtime 1.7
I'm using the Microsoft Drivers as you can see in the Shell line below.
I've tried putting
sqljdbc_auth.dll (I've tried both 32 and 64 bit) in the jar drivers folder,
in JRE bin folder or even passed as java.library.path
I've checked in the configuration manager (on the Sql Server) that TCP/IP is enabled and responds at port 1433.
The firewall is off.
Here is the error I get:
c:\test\TestConn\Test2\bin>"C:\Program Files\Java\jre7\bin\java"
-classpath C:\libsqlsvr\sqljdbc_6.0\ita\sqljdbc4.1.jar;.
-Djava.library.path=C:\libsqlsvr\sqljdbc_6.0\ita\auth\x64 Test
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the hot myhost , port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(S
QLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExcepti
onToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:22
43)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLSer
verConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConne
ction.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerCon
nection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.
java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Test.testsqlsvr(Test.java:135)
at Test.main(Test.java:24)
Do you know what could cause the problem?
Since you're using HOST:PORT identification of the SQL Server in JDBC, you should provide a valid IP or hostname in the HOST part, not the instance name (i.e. no "myhost\myinstance", but the actual network hostname / IP).
The JDBC connection goes through normal TCP, so the HOST:PORT is an ordinary network address, no Microsoft magic. HOSTNAME\INSTANCE uses a dispatcher service (SQL Server Browser) that resides on a known port (UDP 1434) and forwards the connection according to the \INSTANCE part, and is not combined with port.

authenticating mongodb from a remote machine with ssh tunnelling

I have mongo db running in a remote server (debian) with authentication on. I need to connect to it with UMongo (UI tool for mongodb) from my local machine (windows) to visualize the data. I have not opened mongodb port in server machine, but have made a ssh tunnel from my localhost:9999 to the mongodb-host:port. As connection uri in UMongo, i have given
[username]:[password]#localhost:9002
(also tried with [username]:[password]#localhost:9002/dbname)
and tried to connect. It shows the database but doesn't authenticate, it throws authentication error. I have made sure that the credentials given are correct. Wondering whether the credentials are not being passed to the mongodb server properly with ssh tunnelling.
Update: I restarted mongo server with authentication disabled. After that i tried to access the remote mongo shell. When i tried with mongo --port 9002, it gave the below error:
MongoDB shell version: 2.4.4
connecting to: 127.0.0.1:9002/test
Thu Sep 05 22:55:21.107 Socket recv() errno:10054 An existing connection was forcibly
closed by the remote host. 127.0.0.1:9002
Thu Sep 05 22:55:21.107 SocketException: remote: 127.0.0.1:9002 error: 9001 socket
exception [1] server [127.0.0.1:9002]
Thu Sep 05 22:55:21.107 DBClientCursor::init call() failed
Thu Sep 05 22:55:21.107 JavaScript execution failed: Error: DBClientBase::findN:
transport error: 127.0.0.1:9002 ns: admin.$cmd query: { whatsmyuri: 1
} at src/mongo/shell/mongo.js:L114
exception: connect failed
But when i tried with mongo --port 9002 --nodb, i was able to get the mongo shell. Then i tried to use mydb which throws the below error:
Thu Sep 05 22:57:43.205 JavaScript execution failed: ReferenceError: db is not defined
at src/mongo/shell/utils.js:L691
Any help would be much appreciated.
Thanks.

Solr Read Timeout (only in production environment)

I am working with a Java application that uses SolrJ to index documents to a Solr server.
In my local test environment, I run a local Solr instance on a Tomcat server on my windows xp box. When I run the Java app from a different Windows box, the indexing completes successfully and the Solr log files look normal.
However, running the same Java application deployed on linux webserver communicating to another linux webserver running Solr, I receive "read timed out" messages after every solr update command:
Jul 14, 2011 3:12:31 AM org.apache.solr.core.SolrCore execute INFO: []
webapp=/solr path=/update params={wt=javabin&version=1} status=400
QTime=20020 Jul 14, 2011 3:12:51 AM
org.apache.solr.update.processor.LogUpdateProcessor finish INFO: {} 0
20021 Jul 14, 2011 3:12:51 AM org.apache.solr.common.SolrException log
SEVERE: org.apache.solr.common.SolrException:
java.net.SocketTimeoutException: Read timed out at
org.apache.solr.handler.XMLLoader.load(XMLLoader.java:72) at
org.apache.solr.handler.ContentStreamHandlerBase.handleRequestBody(ContentStreamHandlerBase.java:54)
at...
Caused by:
javax.xml.stream.XMLStreamException: java.net.SocketTimeoutException:
Read timed out
Any idea why this might be happening? My suspicion is that something is closing these connections after they are initiated (e.g. web filtering software, firewall...), but the network admins at my workplace say that no traffic is being blocked.
Is it getting timedout only with updates or even with querying?
Check the server settings on the linux server machine whether it has very less timeout value.

sending mail through Java

I am facing problem on a centOS server while sending java mails. Getting the following exception.
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25, response: -1
I used the following command from the command prompt and I got the mail as expected.
echo "testing" | mail -s"test subject" shantanu.oa#gmail.com
The relevant entry from maillog looks like this...
Mar 28 20:13:16 postfix/smtpd[10120]: fatal: no SASL authentication mechanisms
Mar 28 20:13:17 postfix/master[28163]: warning: process /usr/libexec/postfix/smtpd pid 10120 exit status 1
Mar 28 20:13:17 postfix/master[28163]: warning: /usr/libexec/postfix/smtpd: bad command startup -- throttling
Mar 28 20:26:22 postfix/smtpd[11001]: warning: SASL: Connect to private/auth failed: Connection refused
How do I correct the problem?
From the log entry
Mar 28 20:13:16 postfix/smtpd[10120]: fatal: no SASL authentication mechanisms
I suspect that the problem is on Postfix, of which I'm not an expert. Googling for "fatal: no SASL authentication mechanisms" gives lots of interesting links: maybe have a look here or here.
From the exception it seems like its not able to connect to the host.
Have you tried the telnet stuff?
One more thing to check would be whether the name to IP resolution is taking place properly.If not you can use the IP directly in place of host to see what happens.
does your smtp-server needs authentication?
Seems so, because the cmd call with mail works. In default postfix configuration a local user is allowed to send mails without authentication. And your Java application might not be a registered system user on this maschine.
Try to provide username and password in your code to login to the smtp server.

Categories

Resources