connecting java application with online hosted mysql database - java

I have build an application in java, application is one and will be used on 3 different systems,And therefore the database of that application must be online to keep all 3 applications with up to date database...
In starting I developed my application based on localhost (wampserver) and used database in "PhpMyAdmin", and hopefully application is fully developed and ready to run.. but the problem is online database connectivity!
I have uploaded my database on a Site in PhpMyAdmin and they provided below information:
and the for connecting my app to this DB is:
String url = "jdbc:mysql://fdb12.biz.nf:3306/";
String dbName = "1738412_wstore";
String driver = "com.mysql.jdbc.Driver";
String userName = "1738412_wstore";
String password = "Password";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
now when I run my application it shows below error:
I dont know what the problem is there, please help me out through this..

I faced the same issue and the following helped me to solve.
http://support.hostgator.com/articles/cpanel/how-to-connect-to-the-mysql-database-remotely
Hopefully, your hosting provider should have same type of cpanel to configure MySQL database for remote connections.

Check for firewall.
Check if mysql is running.

2 things you can try:
Install MySql client locally on your machine and connect like: mysql -h fdb12.biz.nf -u 1738412_wstore -P<password>
This should work before you try anything else.
Make sure you're using the right imports in your code. See [here][1].
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

I think your DB server doesn't allow remote connections.
Try hosting in:
http://www.freesqldatabase.com/freemysqldatabase/
It's free and allow remote DB connections.
Good Luck!!!

Considering the Biz.nf FAQ:
How can I connect to my MySQL/PostgreSQL database? What settings
should I use for my script/software?
MySQL/PosgreSQL database connection can be established with script/software hosted only on your
web hosting account (meaning no remote access is allowed due to
security reasons). The following settings are needed:
So, since biz.nf does not allow remote access, it will be difficult for you to interact with their MYSQL server for your application.
The best solution and the most simple is probably to find a new provider which will allow the remote access. Heroku.com works very well.
If you really want to use the biz.nf services, it will be more difficult.
It's not a script or credential problem.
Maybe you can (probably not), try to login by SSH to configure your server to enable remote access to your MYSQL database server.
From the free plan, you will not have any access to the online SSH tool and you will need to configure your domain for a SSH connection.
In SSH, and it probably will not work considering the FAQ, you could try to simply modify your /etc/mysql/my.cnf file by commenting the bind-address line wich by default only allows local access (127.0.0.1). Then, try to restart your MYSQL service with: a simple service mysql restart to verify if the remote connection works.
If the remote connection still doesn't work after this, the only way I can find for the moment (unless you completely change your hosting provider) would be to create a kind of API hosted directly on the server and your Java program could interacts with your services (In JSON, by example).

Related

Connecting to Google Cloud SQL with Java

I've been trying for days and have gotten nowhere. I am trying to connect to my MySQL database through a JavaFX program I'm building, without requiring me to whitelist every IP that attempts to connect. The GCP support team has replied to me once but completely misinterpreted the issue (gave examples of logs that only occurred after I whitelisted my own IP to test the other aspects of my program).
​
I found instructions at https://cloud.google.com/sql/docs/mysql/connect-external-app#java, and pasted the following code into my main method (substituting the appropriate values for databaseName, instanceConnectionName, username, and password):
String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s"
+ "&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false",
databaseName,
instanceConnectionName);
Connection connection = DriverManager.getConnection(jdbcUrl, username,
password);
​
I then enabled the Cloud API and, to the best of my knowledge, installed and authenticated the Cloud SDK, as directed. Yet despite all of that, I still cannot connect to the instance without a whitelisted IP address, even though the documentation says this is the workaround for that. Does anyone see an issue with how I'm attempting to connect or know how to make this work?
There are some more detailed instructions in the README for the repo of the project.
Some potential tripping points:
Use Application Default Credentials to provide credentials to the factory.
Make sure you have the Cloud SQL API enabled to your project (and if you are using a service account, make sure to have the Cloud SQL Client role added to it).
Add the library as a dependency in your POM or gradlefile.
Make sure your firewall allows out on port 3307 to your Cloud SQL instance.

How to authenticate to sql server as Windows User from java application? [duplicate]

I am currently investigating how to make a connection to a SQL Server database from my Java EE web application using Windows Authentication instead of SQL Server authentication. I am running this app off of Tomcat 6.0, and am utilizing the Microsoft JDBC driver. My connection properties file looks as follows:
dbDriver = com.microsoft.sqlserver.jdbc.SQLServerDriver
dbUser = user
dbPass = password
dbServer = localhost:1433;databaseName=testDb
dbUrl = jdbc:sqlserver://localhost:1433
I have zero problems with connecting to a SQL Server database in this fashion when using SQL Server authentication.
Is there any way I can retrieve the credentials of the user's Windows Authentication and use that authentication for SQL Server?
UPDATE: I know in ASP.net there is a way to set up Windows Authentication for access to the webapp, which is exactly what I am looking for, except I want to pass that token off to SQL Server for access to the database.
I do not think one can push the user credentials from the browser to the database (and does it makes sense ? I think not)
But if you want to use the credentials of the user running Tomcat to connect to SQL Server then you can use Microsoft's JDBC Driver.
Just build your JDBC URL like this:
jdbc:sqlserver://localhost;integratedSecurity=true;
And copy the appropriate DLL to Tomcat's bin directory (sqljdbc_auth.dll provided with the driver)
MSDN > Connecting to SQL Server with the JDBC Driver > Building the Connection URL
look at
http://jtds.sourceforge.net/faq.html#driverImplementation
What is the URL format used by jTDS?
The URL format for jTDS is:
jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
...
domain
Specifies the Windows domain to authenticate in. If present and the user name and password are provided, jTDS uses Windows (NTLM) authentication instead of the usual SQL Server authentication (i.e. the user and password provided are the domain user and password). This allows non-Windows clients to log in to servers which are only configured to accept Windows authentication.
If the domain parameter is present but no user name and password are provided, jTDS uses its native Single-Sign-On library and logs in with the logged Windows user's credentials (for this to work one would obviously need to be on Windows, logged into a domain, and also have the SSO library installed -- consult README.SSO in the distribution on how to do this).
This actually works for me:
Per the README.SSO that comes with the jtdsd distribution:
In order for Single Sign On to work, jTDS must be able to load the native SPPI library ntlmauth.dll. Place this DLL anywhere in the system path (defined by the PATH system variable) and you're all set.
I placed it in my jre/bin folder
I configured a port dedicated the sql server instance (2302) to alleviate the need for an instance name - just something I do. lportal is my database name.
jdbc.default.url=jdbc:jtds:sqlserver://192.168.0.147:2302/lportal;useNTLMv2=true;domain=mydomain.local
Unless you have some really compelling reason not to, I suggest ditching the MS JDBC driver.
Instead, use the jtds jdbc driver. Read the README.SSO file in the jtds distribution on how to configure for single-sign-on (native authentication) and where to put the native DLL to ensure it can be loaded by the JVM.
I was having issue with connecting to MS SQL 2005 using Windows Authentication. I was able to solve the issue with help from this and other forums. Here is what I did:
Install the JTDS driver
Do not use the "domain= " property in the jdbc:jtds:://[:][/][;=[;...]] string
Install the ntlmauth.dll in c:\windows\system32 directory (registration of the dll was not required) on the web server machine.
Change the logon identity for the Apache Tomcat service to a domain User with access to the SQL database server (it was not necessary for the user to have access to the dbo.master).
My environment:
Windows XP clinet hosting Apache Tomcat 6 with MS SQL 2005 backend on Windows 2003

Error connecting to Oracle database using JDBC [duplicate]

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.

Connecting to hosted MySQL server with Java

I've been recently trying to connect to a hosted MySQL using Java but can't get it to work. I can connect to a local MySQL with localhost using:
connect = DriverManager.getConnection("jdbc:mysql://localhost/lego?"
+ "user=******&password=*******");
(Replacing the astrisks withmy username and password)
I can connect to the hosted MySQL database fine with PHP using:
mysql_connect('mysql.hosts.co.uk','******','**********');
mysql_select_db('test');
My problem is, I cannot connect via Java. I have an Exception which is caught if the connection doesn't work and this is always printed out.
Any ideas why it isn't working? Am I doing something wrong?
Thanks for your time,
InfinitiFizz
since it works in php (i guess you didn't try to connect from a local place with php???) it shouldn't be a port problem... but you should check that port 3306 is open... and ask the hosts company about that.
Have you noticed that in the DriverManager
http://java.sun.com/javase/6/docs/api/java/sql/DriverManager.html
you have:
getConnection(String url)
but also:
getConnection(String url, String user, String password)
Perhaps it would work better...
My guess is that you need to select a non-standard port, since I'd imagine the hosting server is serving lots of MySQL instances and they can't all use the normal one. I don't see selection of a port here.
If that's not it, perhaps there is a firewall issue somewhere along the way that's blocking the port or connection.

How to run a HSQLDB server in memory-only mode

In the documentation of the HSQLDB is a command line statement to start a HSQLDB server (HSQLDB Doc). But there is this "file:mydb" property, so I assume its not in memory-only mode.
How do I run a memory-only HSQLDB server?
I ran the following but get no clue.
java -cp ../lib/hsqldb.jar org.hsqldb.Server -?
It took around 2 days for me to figure out on how to start a server in-memory and then access from outside. Hope this will save someone's time.
Server server = new Server();
server.setDatabaseName(0, "mainDb");
server.setDatabasePath(0, "mem:mainDb");
server.setDatabaseName(1, "standbyDb");
server.setDatabasePath(1, "mem:standbyDb");
server.setPort(9001); // this is the default port
server.start();
When you have to access the in-memory database for any CRUD, here is what you need to do :-
String url="jdbc:hsqldb:hsql://192.168.5.1:9001/mainDb";
Class.forName("org.hsqldb.jdbc.JDBCDriver");
Connection conn = DriverManager.getConnection(url, "SA", "");
where 192.168.5.1 is the server ip where HSQL is running. To connect to the standbyDb, replace mainDb with standbyDb in the first line. Once you get the connection, you can perform all database related operations.
To connect to the server from remote using DatabaseManagerSwing, here is what you need to do.
Download hsqldb-x.x.x jar and copy it to a folder (x.x.x is the version)
open a terminal or command prompt and cd to the folder and run
java -cp hsqldb-x.x.x.jar org.hsqldb.util.DatabaseManagerSwing
Select "HSQL Database Engine Server" from the Type drop down and give "jdbc:hsqldb:hsql://192.168.5.1:9001/mainDb" as the URL. This will connect you to the remote HSQL in-memory Server instance.
Happy Coding !!
DbManagerSwing UI
use java -cp .\hsqldb-1.8.0.10.jar org.hsqldb.Server -database.0 mem:aname
In memory mode is specified by the connection url - so if you want, you can just have a server.properties file in the same directory, and set the connection url to use the mem protocol - or if you are using hsqldb in another application that allows you to specify the connection url such as jdbc, specify jdbc:hsqldb:mem:aname.
I believe the file is used to load up the db into memory, and then persist when Server stops. I don't think the file is accessed while you're running.
It's been awhile since I've used HSQLDB (or H2), but I'm pretty sure that's how it works.

Categories

Resources