Connection to a remote database through an Applet - java

I made a applet that includes JDBC sql connector.
On my pc I can connect to the database on my server, so that works.
But when I put the applet in my browser it doesn't work.
When I load JDBC with it, it gives me an error.
And when I don't load the archive it does loads, but it doesn't connect.
Now my qeustion is.
How to connect ?
and is it possible some how to hide my database account info.
Because if I use mywebsite/java/UCP.class I can open it and I can read the password of my database account out of it...
So maybe somebody can help me with this?

For security reasons it would be better to create a web endpoint that mediates between your applet and your database.
More information here:
Execute jdbc applet in browser
and here:
http://www.stanford.edu/dept/itss/docs/oracle/10g/java.101/b10979/applet.htm

Related

How to test database connection through JDBC

I got a problem about testing db connection. It was a handed over Java application in Eclipse. (which I am not familiar with it).
The app used JDBC to connect with Oracle and put the db information inside the context.xml. I tried to add different System.out.println() in the DBconn.java, but didn't show any log in console window. Any suggestion to me for testing the connection?

How to connect Java desktop application to an online mysql database?

As titled, I want to make my desktop java application to connect to an online mysql db ?! How can I reach it such connection to be able to add and retrieve data?!
Download the connector from here. Add it to your classpath and in the code:
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://remoteUri/database-name?"
+ "user=user&password=userpw");
By all means, a JDBC connection is the way to go. But, you must make sure that the MySQL users have access to the database from another IP Address (i.e. your desktop application).
MySQL users are set up so that they can access the database on the local machine. Therefore, you have to allow access to the MySQL database from any host. Please see the following article which shows how to do that.
You could directly connect and let the queries run over the network (via JDBC)example: vogella.com
You build an RESTful Service, there are lots of tutorials as Example;
RESTful Web Services API using Java and MySQL

Connecting to a sql database from an applet on the server, do you use localhost or the actual ip

I am trying to connect to a database on my website from a java applet.
When I try from eclipse, it says that there was a communications link failure and that the driver has not received any packets from the server. Is that because only applets on the actual server can connect to a database?
So I exported it as a jar and I personally signed it and uploaded it to my website. Then using as embed tag, I put it in my webpage.
<div id="blah">
<embed id="Math"
type="application/x-java-applet"
width="810" height="600"
archive="mathG.jar,mysql-connector-java-5.1.23-bin.jar"
code="com.mathg.math.MathG"
codebase="test"
pluginspage="http://java.com/download/"/>
Is that the proper way to put the applet on the site? The applet shows up and it works on the webpage, but when I press a button to connect to the database, it says that it couldn't connect.
I tried using both localhost and the actual ip in my java code to try to connect, but neither works.
Putting the IP address of the database server is preferred; localhost should only be used for quick & dirty local tests. It's unlikely that you'll deploy an app or database on localhost.
This is a very bad idea. No applet should have direct access to a database from the internet. All that data is exposed.
A better idea is to put a servlet in-between the applet and database. Deploy the servlet by exposing it to the internet; put the database behind a firewall and only open its port to the servlet. Let the servlet handle authentication, validation, binding, and interacting with the database. Users will be much better off with this design.

Applet unable to connect to mysql server

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.

Update The database using applets

I have written a code in JAVA to search files from the system.User specifies the filename and extension on text input on applet window and on button click code establishes a connection to oracle database and searches the directory name from the database table.The code for searching works fine without applet and on using applet it detects an error which is
"access denied(java.util.PropertyPermissionfile.encoding read)"
How do i grant this applet the permission to read from database?
An applet is only allowed to connect to server from where it was downloaded. Otherwise you need to sign the applet. Or use a proxy to connect to the database from the machine the applet came from.
See also How can I connect from an applet to a database on the server?

Categories

Resources