0

i created a java applet class which reads data from a db and display then in an applet. the program runs correctly and i fetches the data when i run it from Eclipse IDE, but when i run it from the browser using an html code it runs but does not get data from the db. this is the code i used.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;


public class Driver extends JApplet {
  private Connection connect = null;
  private Statement statement = null;
  private PreparedStatement preparedStatement = null;
  private ResultSet resultSet = null;
  private JLabel lbl = null;
  private String label;

  public void init() {
      try {

          try {
              // 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://localhost/jdbc?"
                      + "user=root&password=");

              // statements allow to issue SQL queries to the database
              statement = connect.createStatement();
              // resultSet gets the result of the SQL query
              resultSet = statement
                  .executeQuery("select * from jdbc.info");
             // writeResultSet(resultSet);
              while (resultSet.next()) {
                  String user = resultSet.getString("id");
                  String name = resultSet.getString("name");
                  label = user +" "+ name;
                }     
            } catch (Exception e) {
              throw e;
            } finally {
             // close();
            }

          SwingUtilities.invokeAndWait(new Runnable() {
              public void run() {
                  lbl = new JLabel(label);
                  add(lbl);
              }
          });


      } catch (Exception e) {
          System.err.println("createGUI didn't complete successfully");
      }
  }

  public static void main(String[] args) throws Exception {
        Driver dao = new Driver();
        dao.init();
  }


} 

And this is the HTML code

<html>
  <head>
    <title>My first app reding from db</title>
  </head>
  <body>
    My first app reding from db<br />
    <applet code="jdbcdemo/Driver.class" width="700" height="700" />
  </body>
</html>

can anyone help me to find why the applet is not reading from the database which is on a local wamp server. is their a specific directory where i have to add the .Class in order for it to run?

6
  • what errors do you get? localhost in the connection string is not correct, it should be IP address or domain name of the database server, localhost will point to the client-machine, which for sure not the db-server. it will be 1 special case if someone is browsing the applet on the server (physically sitting) Commented Jan 28, 2015 at 16:09
  • It is not giving any errors, it just doesn't display data. i just tried to change it and added my server IP but still it does not work. Commented Jan 28, 2015 at 17:08
  • use e.printStacktrace(); just under throw e; and open Java console (from control panel i think) before you start the applet Commented Jan 29, 2015 at 7:14
  • As mentioned by @Yazan.. Be sure the Java Console is configured to show. If there is no output at the default level, raise the level and try it again. Commented Jan 29, 2015 at 7:45
  • @Yazan not sure if giving external access to database is a good idea, I would prefer some very simple service which could be configured to provide some basic security. Commented Jan 29, 2015 at 8:06

1 Answer 1

2

Imagine that someone deployed an applet on example.org. From your browser, you access example.org and the code is getting executed(and localhost now is your machine actaully, because code gets executed in the JVM on your computer). Since you are not very cautious, you have not set the password to your local database. That someone, who deployed malicious applet at example.org, can now read data from your database and use it in the way he wants to.

To be able to access database from an applet, you should have some server code running on server machine, which would serve you the data via some kind of network protocol (RMI, Sockets, etc).

Sign up to request clarification or add additional context in comments.

1 Comment

can you please point out some references or example i can use

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.