Results 1 to 5 of 5
  1. #1
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Red face Applet runs on Web page, but no database connection

    My applet made in NetBeans is connected to MySQL database using text field for input in applet and button to confirm input. I enabled Web start in order to show applet on Web page and test it, but now the database doesn't accept input from applet when I input from Web page. In Applet Viewer everything works just fine... Do you have any idea?

    I have isolated this code which is related to the problem:

    Java Code:
    package org.me.now;
    import java.awt.*;
    import java.applet.Applet;
    import java.awt.Event;
    import java.sql.*;
    import java.net.SocketPermission;
    
    public class New extends Applet implements Runnable {
        FlowLayout lm = new FlowLayout(FlowLayout.RIGHT);
        Button open;
        Connection con;
        private String upit, url, baza;
        private Statement stmt;
        private ResultSet rezultat;
        TextField plaintext = new TextField(20);
        Label plaintextvalue = new Label();
    
        @Override
        public void init() {
            setBackground(new Color(60,179,113 ));
    
            try
          {
               Class.forName("com.mysql.jdbc.Driver");
                    String url1 = "jdbc:mysql://localhost:3306/MyNewDatabase";
               con = DriverManager.getConnection(url1, "root", "");
               Statement stmt1 = con.createStatement();
               ResultSet rezultat1 = stmt1.executeQuery("select * from evidencija");
    
          }
                  catch(Exception e)
        {
          e.printStackTrace();
        }
    
       add(new Label("Unesite ime i prezime radi evidencije posete"));
       add(plaintext);
       add(new Button("Potvrdite unos"));
       add(plaintextvalue);
        }
    
    public void Close(java.sql.Statement stmt) {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
        @Override
     public boolean action (Event evt, Object arg)
      { if (evt.target instanceof Button) {
            String labl = (String) arg;
            if (labl.equals("Potvrdite unos"))
        	 {
    
    	  plaintextvalue.setText
                  (plaintext.getText());
            try
        {
    
           Statement stmt2 = con.createStatement();
    
     String updateString = "INSERT INTO evidencija (ime)" + "VALUES ('"+plaintext.getText().toString()+"')";
     int count = stmt2.executeUpdate(updateString);
        }
     catch (Exception e){
                         e.printStackTrace();
            }
            finally {
    	 }
       }
     {if (evt.target == plaintext)
         {
          plaintextvalue.setText
             (plaintext.getText());
          return true;
         }
     }
    }
         return true;
     }
    
        @Override
        public void paint(Graphics g) {
        }
    
    void checkWebPermission() {
    		final SecurityManager sm = System.getSecurityManager();
    		if (null == sm)
    			throw new RuntimeException("Security manager is null");
    		sm.checkPermission(new SocketPermission("www.google.com", "connect"));
    		System.out.println("Got permission to connect.");
    	}
    
        @Override
        public void start() {
        }
    
        @Override
        public synchronized void stop() {
        }
    
        @SuppressWarnings("static-access")
        public void run() {
            
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Do you get any error messages?

    What server and browser are you using?

    Is this being executed inside of Netbeans or outside?

  3. #3
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I don't get error messages. I'm using XAMPP as local server. My default browser is IE. I run it inside the Netbeans.

    One friend told me that problem might be that client application can't work nothing on web server, nor to write in database.

    I'm reading about servlets now. Can it be solution for this problem?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    The error messages would be in the browser's java console Probably something about not having permission.

  5. #5
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    the problem is actually may cause the applet location... A non native host applet cannot access Database unless it's signed.

    The code is really hard one as for any browser

    Java Code:
    try
          {
               Class.forName("com.mysql.jdbc.Driver");
                    String url1 = "jdbc:mysql://localhost:3306/MyNewDatabase";
               con = DriverManager.getConnection(url1, "root", "");
               Statement stmt1 = con.createStatement();
               ResultSet rezultat1 = stmt1.executeQuery("select * from evidencija");
    
          }
    To make it work I recommend use applet<->servlet bound. Place the hard code to servlet and it should work fine. And do the applet as an invoker...

    The second thing is to sign your applet but it is no good because as a rule signing means not its native server correct connection but a client local file system access wanted... It doesn't worth the effort as for db connection...
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

Similar Threads

  1. connection to database
    By buston01 in forum JDBC
    Replies: 4
    Last Post: 02-21-2010, 02:32 AM
  2. getting database connection
    By ravidasineni in forum AWT / Swing
    Replies: 1
    Last Post: 11-22-2009, 03:01 AM
  3. Database connection for applet
    By danielsl89 in forum Java Applets
    Replies: 2
    Last Post: 10-21-2009, 01:24 PM
  4. Replies: 10
    Last Post: 10-11-2009, 08:07 PM
  5. Database connection
    By oaklander in forum New To Java
    Replies: 2
    Last Post: 11-13-2007, 12:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •