I have a sourceforge hosted MySQL database and the connection URL is as follows:
https://mysql-t.sourceforge.net:3306/"db_name"
I want to use JDBC to connect to this database.
My connection URL is as follows:
jdbc:mysql://https://mysql-t.sourceforge.net:3306
However, I keep getting the following error
java.sql.SQLException: Must specify port after ':' in connection string
The reason for this, I believe, is that the https:// part keeps expecting a port after the : sign, like https://<port#>
I could probably use getInetAddress() to retrieve the IP and use that, but I need a better solution since that might not be right.
Your URL should not include http or https. Try it like this:
jdbc:mysql://mysql-t.sourceforge.net:3306/db_name
You might be thinking that you have to include http or https because port 3306 is not visible to your client on the Internet. Be glad that it's not. If it's visible to you, it's visible everyone else, too.
You need an intermediary servlet between your client and the database. Clients can contact the servlet via http port 80 or https port 443. The servlet will authenticate and authorize them, validate and bind parameters, make requests to the database on their behalf, and send the appropriate response.
You should not be letting anyone use the root or admin credentials to access that database. You should create credentials just for that app, accessing only one database, allowing only the operations needed to do the job.
Problem fixed.
1) The access URL is different from what was provided. I found that out from the sourceforge forums where other users had similar problems.
2) The port number is different. It works at 4040 and not 3306.
3) Sourceforge apparently doesn't allow permanent access to their database. It works only on request and only for 4 hours at a time. They do not allow using their database as backend.
Related
I am attempting to connect to URLs in Java to see if they are valid and I am wondering if I need to connect to HTTPS(port 443?) or if connecting to just HTTP(port 80) will be enough.
Does connecting to HTTP for an HTTPS website work? Is there anything with firewalls I should watch out for that wouldn't allow me to do this?
Thanks.
If you want to check that URLs are "valid" I think you want to know if they respond with a 200 status code to a GET request.
You'll need to check http and https separately if you want to know if they both work. They're two different protocols, and severs handle them differently. Some servers mirror the same content over both protocols, but many of them redirect the HTTP -> HTTPS etc.
Also not every server supports SSL connections, therefore HTTPS might not be available.
Since you rephrased your question I'll update my answer accoring to that.
To stay with your example:
Checking for URLS on port 80 is totally independent from checking urls on port 443. Maybe port 80 leads to the same content as port 443. Maybe port 80 leads to the end-user content, while port 443 leads to the admin-login.
Maybe apache operates on port 80 while nginx operates on port 443.
So to get the all of the content, you need to scan both ports. Additionally be prepared to find sometimes two different types of content, that don't have anything to do with each other. Admittedly this will happen rarely but it can happen.
Regarding firewalls:
If a web-service is intended to be public, firewalls will happily allow you to connect to the service. If a web-service is intended to be private and you can connect to it nonetheless, the firewall admin made a mistake :)
HTH
I have always used a database located in my pc, so the steps to connect to it are:
connect to my local db using jdbc
perform queries
show results
now, i want to know what are the steps if the database is located in an online server?
some guides,tutorials are very appreciated.
Since you will be connecting to a remote database I would advise you to read about secure connections using JDBC. See this question, for example. You don't want to interact with a remote database without something like SSL to protect the confidentiality of the data.
Once you think you have secured your connection, you can use a tool like Wireshark to make sure that the packets that are going to and coming from your database are in fact opaque.
Furthermore, as others have said, not much changes if you already have a working connection to a local database, it's a matter of changing your URL from jdbc:mysql://localhost:port/database to jdbc:mysql://ipaddress:port/database.
From my experience, I found that some hosting companies block database access from unknown IP addresses, so it's possible that you'll have to go to your CPanel and whitelist your IP address.
Once your database connection is setup, the code you use to query the database should look the same.
Some useful links:
JDBC Best practices
JDBC Tutorials from Oracle
You have to change database url only to be like
jdbc:mysql://IP:3306/databasename
also username and password.
some hosting company like godaddy create database in localhost(in the same host) which doesn't need to change anything in your code.
I'm developing a Java Swing based app which uses JDBC to connect to a MySQL database. As such, the software directly remotely access the database from whichever computer it happens to be running on. Additionally, the app uses prepared statements to query the database and the database is hosted on a shared CPanel hosting account (if that matters).
The snippet of code I use to connect to the database is as follows (fairly standard connect code I think and all strings in all caps contain the correct contents):
String url = "jdbc:mysql://URL:PORT/DB_NAME?connectTimeout=3000";
Connection conn = DriverManager.getConnection(url, USERNAME, PASSWORD);
I have only ever successfully used the app from one IP. Before I use the app from an IP, I have to manually whitelist the IP by adding it as an allowed remote MySQL access host. If I don't add the IP as an allowed access host, the server refuses my connection and I get the resultant error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Then if I whitelist an IP and try to connect from it, I don't get that error and the app connects to the database properly.
This system would be okay if the app were only going to be used from one IP, but it needs to work from any IP since I cannot predict who will download and use it. The only solution I see would be to do a global whitelist of all IPs in the allowed MySQL access hosts area. However, that seems like it has many drawbacks, such as being insecure as anyone who has the correct password could log in (and would thus be susceptible to brute force attacks). This seems to corroborate the hypothesis that that method is insecure. Thus, I would like to have a system of communicating between the app and database that is IP-independent (doesn't require whitelisting all the IPs).
Additionally (I don't know if this makes sense or matters), but I believe some of the areas I expect the app to be used in block certain protocols. Thus, (I think) I would like it if the selected method of communication only used HTTP or some other widely-used protocol.
I did some research into this problem and my efforts led me to 2-tier and n-tier models of database communication. Perhaps I could do something like make a PHP page which accepts a statement and a series of parameters (plus a password to gain entry), executes the statement, and then returns the result back as JSON. However, this seems like another less-than-ideal method as it seems like it would also have security problems.
I'm sure someone more experienced and knowledgeable than I has already come across this problem and developed a solution.
Therefore, my question: What is the preferred method of connecting to a MySQL database from a Java app in an IP-independent way?
I greatly appreciate and thank you for your time.
You're on the right track:
1) If you want any arbitrary client to connect directly to your database, and the clients can have any arbitrary IP address ... then you're probably going to have to effectly disable IP security be whitelisting all possible client IP addresses.
2) On the other hand, if you only allow local access to mySql (by far the most common scenario), then you can create a web app to interface between your clients and mySql.
SUGGESTION:
Consider creating a "REST" web service that your clients can talk to.
Here's a good tutorial that might help you get started:
REST with Java (JAX-RS) using Jersey - Tutorial
Q: Does your Swing app really need to emit "raw SQL"? Or can it make "high level" queries? REST is ideally suited for the latter.
PS:
Here's another, short example that might help suggest some design alternatives with REST, mySQL and Java for you:
http://www.9lessons.info/2012/09/restful-web-services-api-using-java-and.html
You are up against the policies -- primarily the security policies -- of your hosting provider. It's generally considered insecure to allow port 3306 (MySQL) connections from the whole internet. It certainly lays your MySQL server open to trivial denial-of-service attacks. (It just takes some knucklehead controlling a botnet to send in lots of port 3306 connection attempts. They don't even have to be successful connection attempts.) If you're sharing your MySQL server with other customers of your hosting provider, they have every incentive to restrict your remote access to their server.
Most folks who build database applications for deployment on the public internet do it by providing web services to hit the database with the specific operations required by the application. The application deployed at the end-user's machine then uses HTTP (or HTTPS for security) to access those web services. In turn the web services access the database. That's what multitier operations do. You're right that there are security problems, but you can mitigate them with careful development of your web service code.
You could use SSH tunneling to handle your database access. The SSH suite of remote-access applications allows port forwarding. To use this, you would establish (authenticated and encrypted) ssh connections between your end-users' machines and your database machine, that forward port 3306. Then your users could connect to localhost:3306, and that net traffic would be forwarded to your database server. It's pretty flexible and quite secure, if not completely simple to configure.
You might also investigate using SQL Relay. It's generally used for connection pooling and management within a data center network, but it might work for this purpose.
Be careful opening up your MySQL server to the world! If you do that you may want to require the use of TLS encrypted conections.
I need to retrieve server information like Server IP and Port at server startup.
I am using Spring and hibernet in my project and Glassfish (and tomcat) is the application server.
I know that I can get the IP and Port information from request, but there is no request at server startup.
I found on net after some search that IP address can be retrieved as follows:
InetAddress.getLocalHost().getHostName();
but I did not find any method to retrive the current Port of the server. I am hoping to find API from app server which will provide this information. I am using Spring in my application and thus any indications from Spring API will also be helpful.
Well you should configure port in your web.xml as param and read it out in your code using ServletContext or you can use this poor hack
A server may be listening to multiple ports on multiple names, so you cannot be certain that the one chosen automatically is the one you really want.
Question is what you need it for.
If it is for giving URL backs in requests then use the information in requests.
If it is for logging or announcement with ZeroConf, then consider writing application server specific code asking it about its configuration.
If all else fails, explicitly pass in the information through e.g. system properties or JNDI.
i need to send a request to a website when a user submits their url, i tried using curl but it uses server ip which i dont want i have seen a website that is doing a similar job by using applet, users need to open a popup which contains the applet after they leave it open allow it to run it uses a port and then uses the localhost ip like so 127.0.0.1:64653 to send request and basicly curl by the user ip. i decompiled the applet the website was using and they were using java.net.ServerSocket and some other java code but i do not know anything about java. I would to know how this can be done.
I think you are really confused.
a user does not have a URL.
there is a IP address that
represents the browser end of the
connection to the web server he/she
is connected to (actually, there
could be more than one) but this
does not allow something else to
establish a connection to the
browser. So it is not a URL.
the IP address + port number
that the browser has are transient.
In 2 minutes time, the very same
IP/port could denote a different
user, possibly even on a different
machine.
127.0.0.1 is a "special" IP
address that says "this machine".
It cannot be used for communication
with another machine.
notwithstanding all of the
above, web browsers do not accept
incoming HTTP connections from web
servers or anything else. The HTTP
protocol (which is what the web
works on) distinguishes between the
roles of "client" and "server", and
specifies that a client connects to
a server and not the other way
round. A web browser is always an
HTTP client, by convention and also
for security reasons.
So when you say ...
i need to send a request to a website when a user submits their url
... it simply does not make any sense. Please explain what you are tying to achieve ... not how you are trying to achieve it ... and we might be able to help.