Suppose, I have a website for a school to collect students information and data.And my site has a MySQL databse.So how can I or my desktop application get those data from my site's database?Basically, when I use MySQL for my desktop application I write some line of codes to connect with my local MySQL database which is given below.
public settingdatabase(){
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/rgacd","root", "");
stat = con.createStatement();
}catch(Exception ex){
System.out.println("Error: "+ex);
}
}
But when I try to connect with any HTTP site's database throough my desktop application what should I do.
Thanks In Advance. :D
Typically there are firewalls involved. Many networks running web servers will only accept incoming http/https connections (ports 80 and 443). Other ports (such as 3306 to connect to a mysql database) are closed to the outside world. The web server for the site often connects to an application server (e.g. Tomcat) that has network privileges to access the internal database.
So the simple answer is usually you can't directly connect to the database backing any arbitrary website because you don't have access to it.
Here is a somewhat typical architecture courtesy of Tivoli and an IBM slide deck:
By design, in most cases, direct access to the RDBMS is simply inaccessible to the users outside the firewall.
Now, in the unusual case that the firewall allowed an open port to connect to the database from any external machine, then you could connect to the machine via a jdbc network connection (the same as you are doing by accessing the database over localhost:3306). To establish such a connection you would need to use the publicly available address of the database machine (which may or may not be the same as the address used for the http connection, depending upon the network configuration of the system).
Related
I made an inventory system in netbeans using the language java and i connect it to derby as my database it is already working as a whole system but I want to have a client-server functionality what I mean is I want my program to have a server which holds the database(different computer) and different users with different computers which they can save/view data at the same time to the database means they are connected in one network.
Working with multiple connections to a single database.
What code can I use or method or do I have to import something?
I did some research and the only thing that I found is socket which can be used to create a chat between server and a client.
But I only tried the IP 127.0.0.1 for the client since I am making running the server and the client in the same computer.
And also can I open the connection of the server in the client form and send data like SQLQuery so I can save it in the database of the server?
Where can I see examples for these? Please help thanks
Yes, Derby supports a client-server configuration of your application.
Deploying your application in the client-server configuration is straightforward:
Deploy the Derby Network Server, for example by running the startNetworkServer script that is included with the Derby distribution.
Ensure that derbyclient.jar is in your application's CLASSPATH, and that you register "org.apache.derby.jdbc.ClientDriver" with the JDBC DriverManager.
Change your JDBC Connection URL from jdbc:derby:<db details> to jdbc:derby://<host:port>/<db details>.
If this is your first time using Derby, I strongly recommend working your way through the Derby tutorial at https://db.apache.org/derby/docs/10.12/getstart/index.html
For more information about running the Derby Network Server to service database requests for your applications, read the Derby Admin Guide: https://db.apache.org/derby/docs/10.12/adminguide/index.html
Use the IP 0.0.0.0 or for all connections in the server. The the connection url should include the name of the server or the ip address of the server in the network. When you use ip 127.0.0.1 or localhost derby can only accept connections to the database in the same machine, in this case localhost. All of this can be done by your network application server
I am working on java desktop application (swing) which needs to be install/run in three different computers on same Local Network ( LAN ). But this application need to share one database and each application should be able to insert, update, delete records in the same database.
How do i achieved that in java, any suggestion/help would be appreciated ?
Thanks in advance
I came across this while reading some questions. I also created a desktop application which is being accessed by other computers on the same network which includes laptops and desktop PCs in the past.
Step 1:
Choose which of the PCs will be the server. In other words, which among the PCs will be your server. Your chosen PC-server will have a server application(e.g. XAMPP) installed. This PC is where you will import your existing database (.sql file). Other PCs don't have to have XAMPP(or other server applications) installed on them. Other PCs only need to have a copy of the desktop application that you created. No need to set other PCs with server application and sql.
This PC-server that you have chosen will have a connection string with localhost since I assume that all of them are not accessing the internet to use the database.
Example: jdbc:mysql://localhost:3306/yourdatabasename
Step 2:
Grant privileges to the database that was setup on your chosen PC-server
The simplest example is the one below.
grant all privileges on db_name.* to 'username'#'localhost' identified by 'password';
Don't forget to replace with your connections' username and password
But you may also select certain privileges based on your preference.
Step 3:
Set other PCs' connection string that has copy of the desktop application you created.
If PC-server has an IP Address of let's say 192.168.2.3 then, the connection string of other PCs will be
jdbc:mysql://192.168.2.3:3306/yourdatabasename
Other PCs will connect using the PC-Server's IP address since our database and server application was setup on PC-Server.
I hope this helps and others who might see this question.
Peace.
This is a basic approach to this problem:
-Have a client side application and server side
Request access to the database, have the server side application either accept or reject the request.
Once authenticated to the database, have the server side application send the information over to the client application containing the database info
When the client application wants to perform one of the actions you described : update,delete,etc...have it send requests to the server application to complete these.
It works exactly as if the database is local.
THe only main difference is that the url to the database is not in the form 127.0.0.1 (or localhost) but has the ip of the machine where the database is present.
If changing that you have connection problems probably a firewall is blocking your requests. Check if a firewall exists and open the port of the database.
If more applications try to access to data at the same time you need to put autocommit to false and manually commit the data only when the update is finished. Other threads reading or trying to update the same data will wait until the first thread has committed (or rollbacked) the transaction.
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 am attempting to create a MySQL and Java client app for my home network.On the server machine I successfully connected to the MySQL as root
Now I want to connect to MySQL from my client PC using the Java client program,
How to do this??
Do i need to install Tomcat Server to run on server for this.
I am using Windows 7 on all my clients and server machines.
Tomcat server is used for web applications.You just need to create a JDBC code and in the URL string just give the ip address of the system where mysql is installed.For example
connection = DriverManager
.getConnection("jdbc:mysql://192.168.1.205:3306/database_name","root", "password");
Also if you have not granted the permission in mysql then grant it .See this for granting permission for access to mysql on remote system
Depending on what you want to achieve, you need to establish a JDBC connection to the server. Take a look at the JDBC trail for more details.
This will allow you to connect directly from you client machine to your database server. This would be done using the required JDBC connection URL for the MySQL connection, for example jdbc:mysql://[host][,failoverhost...][:port]/[database] ยป
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]..., check out Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J for more details.
You may also need to configure your MySQL server to allow remote access before you can connect
You just need to do two things:
Make sure the server hosting MySQL allows incoming connections
Write JDBC code and use your MySQL server hostname/ipaddress in the jdbc string to connect and use it.
I am developing a java applet which connects to mysql database, the applet works fine on local host but when I uploaded it and tried to run, i get an error as "communications link failure the last packet sent successfully to the server was 0 milliseconds ago.
the driver has not recieved any packets from the server"
i tried it on x10hosting and hostable, please help me if anyone knows the solution...
Are you sure your MySQL server port is really open to the public? E.g., on a publicly-routable IP address and not blocked by your routers/network? Most likely your MySQL instance is not accessible from the outside world...
...which is how it should be. It's very, very, very rarely appropriate to have an applet talk directly to your database server. The usual thing is for the applet to talk to a middle tier on your web server, which in turn talks to the database server. There's a security aspect to that (your middle-tier can defend against various attacks on your database), and there's a practicality aspect: You'll probably want to keep the chatter across the public network to a minimum, and your server can roll up the results of multiple operations into one bundle it then sends to the client.
If your MySQL server really is available to the outside world, is its host the same as the host the applet is loaded from? That's one of the security requirements for (unsigned) applets, that they can only open network connections to the same host they were loaded from. But if that were the issue, I would have expected you to get a security exception (unless Connector/J is hiding it from you).
The applet is client-side. If you want to directly connect to a mysql server this would mean each user that opens the applet should either have mysql installed or have access and credentials to the remote database. Even if your firewall allows the access it is a very bad idea to do so. If you do everyone will have access to your database.
So if you want to communicate with a remote database, use some protocol (http would be easiest, via a Servlet) to send requests to the server and receive responses generated based on the database results. For example invoking http://yoursite.com/addRecord?name=foo&email=bar#baz.com could tell the server-side application to open a (local) connection to mysql and insert a record.