3

I have a desktop application and it makes use of JDBC. I have no problem with JDBC whenever I use localhost. Now, I am to connect to a server that does not allow remote connection. I was advised to provide a web service to serve as a gateway between my application and the database.

An alternative solution I can think of is, to look for a mysql server that allows remote connection. I find it difficult to look for tutorials where I can clearly understand web services in java. I've done some research and I was told I could use PHP to write a web service and generate JSON file, then I could parse it in java. But If I do that, all my JDBC codes have to be recoded/removed.

Is it possible to connect to the database remotely without having my JDBC codes removed? Or can I incorporate Tomcat with JDBC? Thank you!

Here's what I get..

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Caused by: java.net.ConnectException: Connection timed out: connect

user974227
  • 199
  • 1
  • 4
  • 16

6 Answers6

1

Your questions seems to be leading to an architecture similar to this: http://yuml.me/2cc6bd7f

But unlike what your question suggests - the Server Side Module doesn't relay DB queries from the desk-top application to the database - it acts as a server to your desktop application which becomes a client application.

This means re-architecturing (not a real word I think) of your application - but a common best practice. The server side module is responsible for authenticating and authorizing your users to ensure that no one can perform malicious activities on the database.

There is no short answer here - you need to consider if this is the direction you want to go with.

An alternative as others suggested is to allow direct access from the desktop application to the database via a firewall. I assume you are posting here since the people responsible for the database's integrity told you you shouldn't do that.

RonK
  • 9,472
  • 8
  • 51
  • 87
  • I was told that the server doesn't really allow remote connections, yes, for security reasons.. – user974227 Mar 25 '13 at 12:09
  • Some machine will eventually need to talk to the database server, the question is which interface will be available. A database interface is not secured. However an application interface, whether by web service, JMS, EJB or any other interface you choose - will have less privileges, less susceptibility to attacks and will be easier to protect. The server-side module doesn't even have to be deployed on the same machine as the database server, your system engineers can ensure that. – RonK Mar 25 '13 at 12:17
  • It's really impossible to allow the server. My only option (if I really want to keep my codes) is to look for a mysql server.. Am I seeing things correctly? – user974227 Mar 25 '13 at 12:19
  • I don't see how mysql will solve the problem, you have rethink your architecture if your desktop application is not allowed to directly access the database – RonK Mar 25 '13 at 16:46
0

To connect Database with web services is higly not recomandable.

Think this way web services is having input/output pattern. so you want fetch data from table1, with method1. table2 with method2 etc...

so provide remote access for that database server this could be fine.

NPKR
  • 5,368
  • 4
  • 31
  • 48
  • Opening a remote DB for JDBC connections is the worst security nightmare until you take care of explicitly hiding it.Related discussion at http://stackoverflow.com/questions/442862/how-can-i-protect-mysql-username-and-password-from-decompiling/442872#442872 – Akhilesh Singh Mar 25 '13 at 11:57
  • Why do you say this: "To connect Database with web services is higly not recomandable."? – NoChance Oct 21 '15 at 00:23
0

You can allow remote connections in MySQL. You're going to have to search for a guide for the specifics depending on the server it's running on (here's one for debian). Although if you don't trust the people running the application or the DB contains sensitive data I would strongly advise you not to do that and use the PHP instead as it introduces a lot of security issues. If it's only IP address that needs to connect at any one time you should only allow that IP address to connect, that will make the security vulnerabilities smaller.

ddmps
  • 4,350
  • 1
  • 19
  • 34
  • The server I'm to use does not allow remote connections. :( Is it possible to directly communicate with a server without web services? I'll look into that. Thanks! – user974227 Mar 25 '13 at 11:53
  • If the server doesn't allow, no. What you can do is build something in java with your existing code (the current JDBC code can be at the server - and you can talk to it through an ObjectOutput/InputStream with ease), if java is allowed that is. Otherwise you're gonna have to start out fresh. – ddmps Mar 25 '13 at 11:56
  • If I find a server that allows remote connection, then, can I directly communicate with the database? I've been looking for free mysql servers in the net, but I find it difficult to find a good one. I've seen cPanel, freemsyql.net, etc. – user974227 Mar 25 '13 at 11:58
  • Yeah, that would work "fine". There are complications that you will have to take care of in the code though (like timeouts). I don't know of any free providers that allow remote connection to the DB, sorry (don't know many at all so don't give up looking!). – ddmps Mar 25 '13 at 12:01
0

There is nothing stopping you from accessing a remote database using JDBC apart from firewall rules. It is generally considered a bad practice to expose your db credentials over to client side - even if hardcoded in code. This is architecturally flawed approach and should not consider for more than school homework.

However, if you need the solution, you will have following options to look at: 1) Check for the ip address on which Mysql runs. It must not be localhost but a IP like 192.168.1.2, etc.. 2) Check if the JDBC error is Authentication related, then you will need to add right permission to the user account. MySQL security model ties the username and the IP from where the user can login. You may need to correct those.

If both are correct, please post the exact exception which you are getting while using JDBC.

Akhilesh Singh
  • 2,548
  • 1
  • 13
  • 10
0

You don't have to use a web service. You can implement any form of client/server communication e.g. web services, REST, RMI, native sockets etc. It would be worthwhile to investigate these and determine which is most appropriate. However....

This strikes me as an architectural issue rather than an issue surrounding specific technologies. It sounds to me like you're being guided down the path of implementing some service that allows you not only to access the database, but provide a richer API. e.g. you don't want your client to insert into a table. You should provide an API to add to a shopping basket. i.e. you're working at a different level of abstraction (in the future you may implement your database in a completely different fashion and you don't want to change your clients).

The above is a standard pattern in the Java EE world and wider.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • I'm open to the idea of re-working on it. But it would be quite a relief if I can keep my codes and avoid recoding >. – user974227 Mar 25 '13 at 12:15
0

I think you don't need web service here. To address your issue, you can enable remote access in your MySQL server. Please follow the instruction which is available in this blog. If your MySQL server hosted in Windows environment please refer this document also.

After that update your JDBC URL with remote MySQL server domain or IP address.

Chandana
  • 2,578
  • 8
  • 38
  • 55