简体   繁体   中英

Java Desktop Application to connect to database

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

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.

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.

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.

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.

You don't have to use a web service . You can implement any form of client/server communication eg 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. eg you don't want your client to insert into a table. You should provide an API to add to a shopping basket. ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM