How to connect to JavaDB from another computer? - java

I'm trying to learn some basic JDBC.
I've created a JavaDB server and made a Java program where i can read
and alter some tables in the database.
I thought it would be a fun next step to be able to connect to the
server from another computer running on the same network, or even
from the internet if it's similar.
So far I’ve just tried opening project on another computer to see if
it would connect to the localhost, but would expected it to be too
easy...
The current db name is "jdbc:derby://localhost:1527/test;user=db;password=db".
Could i replace the localhost with my ipv4 address to connect
locally? or
Do i really need the router's IP and do some port forwarding?
Connecting locally is really what I need. If this is easier, then that's what i would like to do.

You need to use either the hostname or the IP of the box you want to connect to:
URI: jdbc:derby://<hostname_or_ip>:1527/test;user=db;password=db
Replace <hostname_or_ip> for the real value and you are good to go.

You need to open the port 1527 on your router.
And you can try this answer
https://stackoverflow.com/a/12176840/2746009

So, i will recommend you to see the firewall settings, if firewall is not blocking the way or you can give a try to communicate to another port. If both of these things didn't work, let me know, how far it takes before spitting this error out. Also, in command prompt, try ping machineIP and see if it's sending and receiving data

I was doing the same configuration and after spending some hours I found the solution:
http://db.apache.org/derby/docs/10.2/adminguide/derbyadmin.pdf
Page 34:
By default, the Derby Network Server will only listen on the localhost (...)
One solution is to create a [derby.properties] file with the following content:
derby.drda.host=0.0.0.0
The place where you save the properties file may vary:
- in one machine for me it was directly under 'javadb', sibling of 'bin', 'lib', etc
- at my 2nd machine it was under the 'bin' directory, sibling of *.bat files.
To verify if your properties is being read (it doesn't exist at first, Derby doc explicitly tells that it won't create it for you), is to add this property as well:
derby.stream.error.file=derbyNewLog.log
If you execute
bin/startNetworkServer
it will create the new log file in case your properties is correctly being read.
After you confirm that your new properties file is being read, you can use the bin/ij tool to execute connect statements to your DB (both local and remote). This is how I could verify it quickly (my db name is 'differentRequestsAndPerformance'):
c:\glassfish4\javadb\bin>ij
ij version 10.10
ij> connect 'jdbc:derby://localhost:1527/differentRequestsAndPerformance';
ij> show tables;
TABLE_SCHEM |TABLE_NAME |REMARKS
------------------------------------------------------------------------
SYS |SYSALIASES |
(...)
ij> disconnect;
>> this 'connect' with localhost is expected to work always - you can append ';create=true' to the DB name in case it doesn't exist already; now change it to your remote ip (I'm assuming you previously confirmed with 'ping' command that you actually can see the server remote machine, otherwise it's a network problem, not a Derby problem)
ij> connect 'jdbc:derby://192.168.1.14:1527/differentRequestsAndPerformance';
ij> show tables;
TABLE_SCHEM |TABLE_NAME |REMARKS
------------------------------------------------------------------------
SYS |SYSALIASES |
(...)
ij> disconnect;
One additional comment: I'm not sure how much the 0.0.0.0 setting will open your machine to malicious software because I only wanted to verify my code and I disabled it right after I was satisfied. More research on this is required in case you want to use this for real programs.
In case your 'ij' tool complains about some driver, add the javadb/lib directory to the CLASSPATH system variable.
Another thing (obvious but better safe than sorry): this configuration must be set at the remote db (where your requests will arrive), in case like me you have 2 machines both with the database running.

Related

In android studio when running the application. I got this error Network Adapter could not establish the connection [duplicate]

I make a connection for connect to the Database Server (other machine).
Then I found "An error was encountered performing the requested operation:
IO Error: The Network Adapter could not establish the connection
Vendor code 17002".
Please look the picture in URL below.
I tried to make a connection but can't access but my team can access it.
My friend used TNS connection type and I did everthing similar him but can't access. I tried to use JDBC thin for connect but can't also.
I had the old connection which I can connect but why I can't connect the new connection.
You're trying to connect to a machine on a network that SQL Developer is unable to reach.
For a TNS connection,
Look at the appropriate TNSNames entry (you will have a tnsnames.ora) file, and find the IP address or network name associated with the connection you're trying to establish -
And then start by trying to ping that resource.
In this case, i'm trying to talk to a database on MY machine, on port 1521. Yours should look quite different.
If you're using a basic connection, then you can look at the connection properties and see what machine/port you're trying to communicate with.
Ping
If you can't reach that machine from your machine, there's zero chance you can connect to a database there.
So, always start with a ping.
Once you see that you can get to that machine, if you're still getting that message, the next thing to think about is blocked ports, the listener defaults to port 1521, but you'll see that in the TNS descriptor as well.
It usually happens when a another process is running on the same port or there is an absence of listener.
Go to Run>services.msc>OracleXETNSListener>Start
Try to reconnect.. Even if the error still prevail then go to cmd
Use code:
npx kill-port <portnumber>
The port number by default is 1521, but you can check the port number in database properties.
It is possible, that your connection is forbidden by the firewall -
go to Control Panel\System and Security\Windows Defender Firewall - Advanced - Outbound Rules - Add a rule. (If you have some antivirus firewall, add rules there)
Create a temporary TCP rule for your port and another rule for UDP. Allow all nets and comps.
Check the telnet connection as cmd -> telnet ->
open remote.host.address PortNumber
If you can connect now, then the problem IS in firewall - edit your new rules, setting the hosts and nets exactly.
This error is because your sqldeveloper is not able to reach the database server's sql service.
One reason could be lsnrctl is down for some reason.
If you have access to machine where database is installed.
In Windows machine, follow: https://www.youtube.com/watch?v=r9pHqOfV2f0&ab_channel=TLinaTutorials
In Linux/macOS machine: Go to $ORACLE_HOME/bin and check status of lsnrctl
execute from $ORACLE_HOME/bin lsnrctl status, if it's down. Then restart it by lsnrctl start.
Check the details of connection in command output like SID, PORT, HOST etc. and try connecting again from sqldeveloper.

What exactly is my database URL for mysql server connecting remotely?

Trying to connect to a mysql database using JDBC automatic driver loading with syntax that requires the following: jdbc:subProtocolName:databaseURL
I know how to work it with a local host that's providing the mysql server, but I haven't done this remotely, and I want to get it right the first time, rather than coding my application and not being able to set breakpoints in the .xml file etc and confusing the source of other issues I might be having.
So far I have: "jdbc:mysql:[url???]:3306/dbname?autoReconnect=true"
Is it simply the ip address of the server, or is it myusername#[ipAddressofServer], or something else entirely? It doesn't sit with me that it would be name#localhost:[port#] because that would indicate the server resides locally. I've seen things online that say to use the 'server name' here. Is that equivalent to the hostname? Example would be great that doesn't involve mysql running on localhost. Thanks!
(Warning: Here be some slight oversimplifications.)
As you may be aware, computers (directly) connected to the Internet can be identified in two ways:
an "IP address", historically of the form 151.101.1.69 (but may be longer and scarier nowadays), or
a "DNS (Domain Name Service) name", e.g. stackoverflow.com, sometimes called a "host name".
When an application wants to connect to its own computer it can use two special cases of the above. 127.0.0.1 and localhost both mean "this here computer".
So if you want to connect to a remote machine you can use either its DNS name or its IP address as the "server name" in the connection string. For example, if there was a MySQL Server running on the machine in the examples above then your connection string could be
jdbc:mysql://151.101.1.69:3306/databasename?useUnicode=true&characterEncoding=utf8
or
jdbc:mysql://stackoverflow.com:3306/databasename?useUnicode=true&characterEncoding=utf8
Notes:
You don't actually need to specify port 3306 because that is the default port for MySQL. If you omit the port number from your connection string the driver will try to use 3306.
useUnicode=true&characterEncoding=utf8 are just examples of common attributes that are added to the connection string. There are lots of them, and you can read more about them here.

Oracle Database 12c in VM on Mac OSX

I'm attempting to get a development environment up and running on my OSX laptop, creating JSPs within Eclipse, running with a Tomcat server, and connecting to an Oracle database.
Eclipse and Tomcat are playing nice together, and I can make JSPs and so forth. The hard part now is getting an Oracle database up and running on Mountain Lion, and getting Eclipse to connect to it.
After extensive Googling, I have found a number of resources:
http://dimitrisli.wordpress.com/2012/08/08/how-to-install-oracle-database-on-mac-os-any-version/
http://barrymcgillin.blogspot.com/2011/12/using-oracle-developer-days-virtualbox.html
(or even https://docs.google.com/document/d/1Th5MSIhS13YIJYCD8W1GLnOQEfrfov-92-He1cluTec/pub)
Following these (rather thorough) guides, I have downloaded the latest Virtual Machine from Oracle, here:
http://www.oracle.com/technetwork/database/enterprise-edition/databaseappdev-vm-161299.html
Note: All three of the above tutorials use a slightly older version of the VM, which has Oracle DB 10 or 11, but the one available now is 12c. The new one also has a different version of Oracle Linux, and probably some other differences. Various usernames and passwords seem to be slightly different.
I installed VirtualBox, loaded in the VM, brought it online. I have the Network settings configured with just a NAT adapter, with the adapter type as "PCnet-FAST III", and port forwarding from 127.0.0.1 to 10.0.4.15 on port 1521 to 1521 and 2222 to 22.
Now, if I open Terminal on my host machine, I can SSH into my VM via: ssh -p 2222 oracle#localhost. I get the Terminal in the VM, and it gives me the same welcome message I get when I first boot up the VM in VirtualBox. Also, I can ping localhost or 127.0.0.1 and it responds with a variable but short lag.
Based on this I can only assume that my VM is working, and that my port forwarding (which is laid out in all 3 of the tutorials I linked to up there) is in good shape.
Now comes the part where I would want to connect Eclipse to the Oracle database that's within the VM. If you look under the first tutorial, dimitrisli sets up IntelliJ with a datasource pointing to the VM. I want to do the same within Eclipse.
I am new to Eclipse, so I looked around for exactly how to do this, and I came up with:
1) Make a new Database Driver.
I downloaded the JDBC for Oracle Database 12c, JDK 7, from Oracle, here. I stuck the .jar file in my Documents folder (in case that's wrong). Within Eclipse I made a new Database Driver, based off one of the other Oracle Driver templates, and changed the settings. Under "Driver Files" I pointed to my ojdbc7.jar file, and under "Properties" I have the following--let me know if this is wrong:
Catalog: USER
Connection URL: jdbc:oracle:thin:#localhost:1521:orcl
Database Name: pdb1
Driver Class: oracle.jdbc.OracleDriver
Password:
User ID:
I tried to use what came in the Oracle Driver template and just tweak to match my exact scenario (based on what was said in the walkthroughs and what I could divine from poking around the modern version of the VM).
2) Make a new Database Connection.
When I go to make a new Database Connection, I go to the Driver Properties area, and I can pick my custom Driver. There is also a Properties field, which I enter the following into: (some of it seems to overwrite or duplicate what I typed in when I made the Driver itself?)
SID: orcl
Host: 127.0.0.1
Port Number: 1521
User Name: hr // Gotten from one of the tutorials. Have also tried 'system/oracle', neither works yet.
Password: oracle
Connection URL: jdbc:oracle:thin:#127.0.0.1:1521:orcl // Immutable, set by the Driver I suppose.
Catalog: User
When I click Test Connection, it hangs for a while and then I get a 'Ping failed!' error, with details:
java.sql.SQLRecoverableException: IO Error: Socket read timed out
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:715)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:385)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:564)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.createConnection(JDBCConnection.java:298)
at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.open(JDBCConnection.java:81)
at org.eclipse.datatools.enablement.internal.oracle.JDBCOracleConnectionFactory.createConnection(JDBCOracleConnectionFactory.java:27)
at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
at org.eclipse.datatools.connectivity.ui.PingJob.createTestConnection(PingJob.java:76)
at org.eclipse.datatools.connectivity.ui.PingJob.run(PingJob.java:59)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
Caused by: oracle.net.ns.NetException: Socket read timed out
at oracle.net.ns.Packet.receive(Packet.java:350)
at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:153)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:263)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1360)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
... 14 more
Unfortunately, being new to Java and Eclipse and Oracle DB, I'm not quite sure what to do with this, and I have been poking around for several hours trying to determine what could be wrong. I'm hoping someone with some experience on this can point me in the right direction.
(Oh, and I'm not an inexperienced developer--just new to these tools.)
Thanks!
I solved my issue, after roughly 6 or 7 hours wasted.
Thanks to this post:
Virtualbox "port forward" from Guest to Host
And specifically to the comment from #Nicholas which made me realize that I had deactivated my Mac's firewall, but not the firewall in the guest OS. Turning that off made everything work suddenly.
I suppose that the default configuration of the guest OS's firewall did allow for SSH on port 2222, as that worked fine even with the firewall in place, which served to mislead me into thinking that there was something wrong with my database driver or connection.
Hopefully this will benefit posterity.
EDIT:
As #HarpreetDawar mentions, the correct connection string to access the database is:
jdbc:oracle:thin:#localhost:1521/PDB1
The one that I was using, ending in :orcl instead of /PDB1, connects to the "container database". I don't fully understand Oracle 12c and the "pluggable database" implementation, but the rough idea is that you can have multiple databases within a single one, and you can turn them on and off by plugging/unplugging them. Hence, "PDB1", Pluggable Database 1.
If you connect to the Container, you will find that you are unable to create a new User/Schema without prefixing it with "C##", which is a tad awkward. That's because the Container is not meant to be used as a normal DB. You connect to the Container to plug/unplug the pluggable dbs (and other things, no doubt).
So if you want to connect to the Container, use a colon and the "SID" (orcl in the VM) in the connection string, and if you want to connect to a Pluggable, use a forward slash and the "Service Name" (PDB1 in the VM) in the connection string.
Anyway, this is additional data that I learned after solving my original problem.
One more thing! A note on port forwarding.
Let's say you now have your VM up and running, and you can connect to your Oracle DB properly, etc. But you have a friend who wants to use the DB with you? Well, that's easy. He should just use the same connection string, with your IP address instead of localhost, and the port forwarding that you set up in VirtualBox should send his connection (using port 1521) straight into your VM. Right?
Well, if you set up your Port Forwarding as From: 127.0.0.1, it won't work, because your buddy's connection is going to your external IP address. So add an additional port forwarding setting, from your external IP address to your VM. Then it will work great!
-Matt
I am the author of the first guide Matt Mc is quoting and taking the liberty to post my comment to his answer as a separate answer for visibility purposes.
I've initially written this guide ~ 2 years ago explaining in detail the process to get the Oracle 11gR2 installed on a Mac using VirtualBox.
Yesterday (25 Apr 14), I've upgraded the same guide outlining all the extra steps needed to get Oracle 12cR1 installed on a Mac using VirtualBox.
Use the following url
url = jdbc:oracle:thin:#//127.0.01:1521/PDB1
to make it work.

dotcms - won't see new database configuration

I'm in the midst of migrating a dotCMS installation to a new server. Everything seems to be working properly except for the database configuration. I've updated tomcat/conf/Catalina/localhost/ROOT.xml with a new IP address for our MySQL server, saved it, run ant deploy-plugins, and restarted dotcms/tomcat.
When I check the log, I get a number of SQL exceptions, all with the same error message: Host 'xxx' is not allowed to connect to this MySQL server. I double-checked all of my MySQL credentials, which were fine.
When I run netstat -pant, I can see an outgoing connection trying to reach the OLD MySQL server.
I've literally grepped the entire dotCMS folder hierarchy for this IP address (or hostname) and nothing turns up. Is there some kind of cache file I need to delete so the ROOT.xml changes are seen?
Sounds like you should check your MySQL permissions. You might need to GRANT access from the new IP address.
Are you sure there is no root.xml in the com.dotcms.config plugin that overwrites your root.xml after running ant deploy-plugins?

IO Error: The Network Adapter could not establish the connection

I am new to Oracle, and am trying to run a simple example code with Java, but am getting this error when executing the code.. I am able to start up the listener via CMD and am also able to run SQL Plus. Can anyone give me a hand and tell me what I might be doing wrong?
Update:
I am using JDBC.
Database is local, and I actually had it working but it stopped working just today. I'm not really sure why though. Would you mind giving me some procedures to follow by since I don't know much.
Either:
The database isn't running
You got the URL wrong
There is a firewall in the way.
(This strange error message is produced by Oracle's JDBC driver when it can't connect to the database server. 'Network adapter' appears to refer to some component of their code, which isn't very useful. Real network adapters (NICs) don't establish connections at all: TCP protocol stacks do that. It would have been a lot more useful if they had just let the original ConnectException be thrown, or at least used its error message and let it appear in the stack trace.)
I had the same problem, and this is how I fixed it.
I was using the wrong port for my connection.
private final String DB_URL = "jdbc:oracle:thin:#localhost:1521:orcll"; // 1521 my wrong port
go to your localhost
(my localhost address) : https://localhost:1158/em
login
user name
password
connect as --> normal
Below 'General' click on LISTENER_localhost
look at you port number
Net Address (ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1522))
Connect to port 1522
Edit you connection
change port 1521 to 1522.
done
Another thing you might want to check that the listener.ora file matches the way you are trying to connect to the DB. If you were connecting via a localhost reference and your listener.ora file got changed from:
HOST = localhost
to
HOST = 192.168.XX.XX
then this can cause the error that you had unless you update your hosts file to accommodate for this. Someone might have made this change to allow for remote connections to the DB from other machines.
I figured out that in my case, my database was in different subnet than the subnet from where i was trying to access the db.
I had this error when i renamed the pc in the windows-properties. The pc-name must be updated in the listener.ora-file
Most probably you have listener configured wrongly, the hostname you specify in connection string must be the same as in the listener.
First check the Firewall and network related issues.
Check if Oracle Listener service is available and running. If not you may use Oracle Net Configuration Assistant tool to add and register new listener.
If the above steps are ok then you need to configure Oracle Listener appropriately. You may use Oracle Net Manager tool or edit “%ORACLE_HOME%\network\admin\listener.ora” file manually.
There are 2 options that need to be considered carefully:
Listening Locations associated with the Listener – Hostname(IP) and Port in Listening Location must exactly match the ones used in the connection string.
For example, if you use 192.168.74.139 as target hostname, then there must be Listening Location registered with the same IP address.
Also make sure the you use the same SID as indicated in Database Service associated with the Listener.
https://adhoctuts.com/fix-oracle-io-error-the-network-adapter-could-not-establish-the-connection-error/
IO Error: The Network Adapter could not establish the connection (CONNECTION_ID=iKQM6lBbSLiArrYuDqud8A==)
if you are facing this issue
1- make sure you have downloaded oracle databases like oracle 11g,19c, 21c, or any latest databases.
2- search for services in your computer or type win+r then services.mis then search for oracleservice you will find orcl or xe or any other sid like oracleserviceorcl;
after that you can test your connection using sql developer, sql plus or cmd
To resolve the Network Adapter Error I had to remove the - in the name of the computer name.
In my case, I needed to specify a viahost and viauser. Worth trying if you're in a complex system. :)
For me the basic oracle only was not installed. Please ensure you have oracle installed and then try checking host and port.
I was having issues with this as well. I was using the jdbc connection string to connect to the database. The hostname was incorrectly configured in the string. I am using Mac, and the same string was being used on Windows machines without an issue. On my connection string, I had to make sure that I had the full url with the appending "organizationname.com" to the end of the hostname.
Hope this helps.
Just try to re-create connection. In my situation one of jdbc connection stopped working for no reason. From console sqlplus was working ok.
It took me 2 hours to realize that If i create the same connection - it works.

Categories