8 Replies - 776 Views - Last Post: 09 April 2012 - 07:33 PM Rate Topic: -----

#1 tawilovejessicalawrent  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 122
  • Joined: 08-April 12

I want to make html file with Java applet

Posted 08 April 2012 - 05:19 AM

I want to make an html file with Java applets but the problem is it works with netbeans and at the time I make an html file to use the applet, it is not working, what is wrong with it?

The code is:



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication3;

/**
 *
 * @author Tawi
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.sql.*;
import java.util.*;
import java.applet.*;

public class JavaApplication3 extends JApplet {

    /**
     * @param args the command line arguments
     */
    // Connection to the database
  private Connection connection;

  // Statement to execute SQL commands
  private Statement statement;

  // Text area to enter SQL Command
  private JTextArea jtasqlCommand = new JTextArea();

  // Text area to display results from SQL Commands
  private JTextArea jtaSQLResult = new JTextArea();

  // JDBC info for a database connection
  JTextField jtfUsername = new JTextField();
  JPasswordField jpfPassword = new JPasswordField();
  JComboBox jcboURL = new JComboBox (new String[] {"jdbc:odbc:DatabaseKlinik"});
  JComboBox jcboDriver = new JComboBox (new String[] {"sun.jdbc.odbc.JdbcOdbcDriver"});

  JButton jbtExecuteSQL = new JButton("Buat Satu Perintah SQL");
  JButton jbtClearSQLCommand = new JButton ("Hapus");
  JButton jbtKeluar = new JButton ("Keluar");
  JButton jbtConnectDB1 = new JButton("Hubungkan ke Paduk Data");
  JButton jbtClearSQLResult = new JButton("Hapus Hasil");

  // Create titled borders
  Border titledBorder1 = new TitledBorder("Masukkan Perintah SQL");
  Border titledBorder2 = new TitledBorder("Hasil Pembuatan SQL");
  Border titledBorder3 = new TitledBorder("Masukkan Informasi Paduk Data");

  JLabel jlblConnectionStatus = new JLabel("Saat Ini Tidak Ada Koneksi");

  /** Initialize the applet */
  public void init() {
    JScrollPane jScrollPane1 = new JScrollPane(jtasqlCommand);
    jScrollPane1.setBorder(titledBorder1);
    JScrollPane jScrollPane2 = new JScrollPane(jtaSQLResult);
    jScrollPane2.setBorder(titledBorder2);

    JPanel jPanel1 = new JPanel (new FlowLayout(FlowLayout.RIGHT));
    jPanel1.add(jbtClearSQLCommand);
    jPanel1.add(jbtExecuteSQL);
    jPanel1.add(jbtKeluar);

    JPanel jPanel2 = new JPanel ();
    jPanel2.setLayout (new BorderLayout());
    jPanel2.add(jScrollPane1, BorderLayout.CENTER);
    jPanel2.add(jPanel1, BorderLayout.SOUTH);
    jPanel2.setPreferredSize(new Dimension(500,100));

    JPanel jPanel3 = new JPanel();
    jPanel3.setLayout(new BorderLayout());
    jPanel3.add(jlblConnectionStatus, BorderLayout.CENTER);
    jPanel3.add(jbtConnectDB1, BorderLayout.EAST);

    JPanel jPanel4 = new JPanel();
    jPanel4.setLayout(new GridLayout(4, 1, 10, 5));
    jPanel4.add(jcboDriver);
    jPanel4.add(jcboURL);
    jPanel4.add(jtfUsername);
    jPanel4.add(jpfPassword);

    JPanel jPanel5 = new JPanel();
    jPanel5.setLayout(new GridLayout(4, 1));
    jPanel5.add(new JLabel ("Driver JDBC"));
    jPanel5.add(new JLabel ("URL Paduk Data"));
    jPanel5.add(new JLabel ("Nama Pengguna"));
    jPanel5.add(new JLabel ("Kata Lewat"));

    JPanel jPanel6 = new JPanel();
    jPanel6.setLayout(new BorderLayout());
    jPanel6.add(jPanel4, BorderLayout.CENTER);
    jPanel6.add(jPanel5, BorderLayout.WEST);

    JPanel jPanel7 = new JPanel();
    jPanel7.setLayout(new BorderLayout());
    jPanel7.add(jPanel3, BorderLayout.SOUTH);
    jPanel7.add(jPanel6, BorderLayout.CENTER);

    JPanel jPanel8 = new JPanel();
    jPanel8.setLayout(new BorderLayout());
    jPanel8.add(jPanel2, BorderLayout.CENTER);
    jPanel8.add(jPanel7, BorderLayout.WEST);

    JPanel jPanel9 = new JPanel (new FlowLayout(FlowLayout.LEFT));
    jPanel9.add(jbtClearSQLResult);

    jcboURL.setEditable(true);
    jcboDriver.setEditable(true);

    add(jPanel8, BorderLayout.NORTH);
    add(jScrollPane2, BorderLayout.CENTER);
    add(jPanel9, BorderLayout.SOUTH);

    jbtExecuteSQL.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        executeSQL();
      }
    });
    jbtConnectDB1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        connectToDB();
      }
    });
    jbtClearSQLCommand.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jtasqlCommand.setText(null);
      }
    });
    jbtClearSQLResult.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jtaSQLResult.setText(null);
      }
    });
    jbtKeluar.addActionListener (new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          System.exit(0);
      }
    });
  }

    /** Connect to DB */
    private void connectToDB() {
      //Get database information from the user input
      String driver = (String)jcboDriver.getSelectedItem();
      String url = (String)jcboURL.getSelectedItem();
      String username = jtfUsername.getText().trim();
      String password = new String(jpfPassword.getPassword());

      // Connection to the database
      try {
        Class.forName(driver);
        connection = DriverManager.getConnection(
          url, username, password);
        jlblConnectionStatus.setText("Terhubung ke " + url);
      }
      catch (ClassNotFoundException ex) {
          System.err.println("Driver Bermasalah");
          ex.printStackTrace();
          System.exit(1);
      }
      catch (SQLException ex) {
          System.err.println("Gagal Dihubungkan dengan Paduk Data");
          System.exit(1);
      }
    }

  /** Execute SQL Commands */
  private void executeSQL() {
    if (connection == null) {
      jtaSQLResult.setText("Hubungkan ke Paduk Data Dahulu");
      return;
    }
    else {
      String sqlCommands = jtasqlCommand.getText().trim();
      String[] commands = sqlCommands.replace('\n', ' ').split(";");

      for (String aCommand: commands) {
        if (aCommand.trim().toUpperCase().startsWith("SELECT")) {
           processSQLSelect(aCommand);
        }
        else {
          processSQLNonselect(aCommand);
        }
      }
    }
  }

  /** Execute SQL Select Commands */ ;
  private void processSQLSelect(String sqlCommand) {
    try {
      // Get a new statement for the current connection
      statement = connection.createStatement();

      //Execute a SELECT SQL command
      ResultSet resultSet = statement.executeQuery(sqlCommand);

      //find the number of columns in the result set
      int columnCount = resultSet.getMetaData().getColumnCount();
      String row = " ";

      // Display column names
      for (int i = 1; i <= columnCount; i++) {
       row += resultSet.getMetaData().getColumnName(i) + "\t";
      }

      jtaSQLResult.append(row + '\n');

      while (resultSet.next()) {
        //REset row to empty;
        row = " ";

        for (int i = 1; i <= columnCount; i++) {

          // A non-String column is Coverted to a string;
          row += resultSet.getString(i) + "\t";
        }
      
        jtaSQLResult.append(row + '\n');
      }
    }
    catch (SQLException ex) {
      jtaSQLResult.setText(ex.toString());
    }
  }

  /** Execute SQL DDL, and modification Commands */
  private void processSQLNonselect(String sqlCommand) {
    try {
      // Get new statement for the current Connection;
      statement = connection.createStatement();

      // Execute a non-Select SQL Command;
      statement.executeUpdate(sqlCommand);

      jtaSQLResult.setText("Perintah SQL Sudah Dibuat");
    }
    catch (SQLException ex) {
      jtaSQLResult.setText(ex.toString());
    }
  }
}
   



I get some yellow exclamation mark, can any folks help me with these code? I want to add some clear screen code either, how to do that?

Is This A Good Question/Topic? 0
  • +

Replies To: I want to make html file with Java applet

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: I want to make html file with Java applet

Posted 08 April 2012 - 07:25 PM

What error(s) do you get ?
Post your stack trace
Was This Post Helpful? 0
  • +
  • -

#3 tawilovejessicalawrent  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 122
  • Joined: 08-April 12

Re: I want to make html file with Java applet

Posted 08 April 2012 - 08:30 PM

The netbeans told me that I must do some override in some where, any folks can help?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: I want to make html file with Java applet

Posted 08 April 2012 - 08:37 PM

For which obscure (masochist) reason do you name a Applet JavaApplication3 ?

Your warnings are:

import java.util.*;
import java.applet.*;

neither of these are used

public class JavaApplication3 extends JApplet {

you do not have a SerialVersionUID for that class, you can ignore that warning
Was This Post Helpful? 0
  • +
  • -

#5 tawilovejessicalawrent  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 122
  • Joined: 08-April 12

Re: I want to make html file with Java applet

Posted 08 April 2012 - 09:22 PM

But it won't work after I use it in an html file, it only work in netbeans, how to fix it? how to make any override?

What is that serial?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: I want to make html file with Java applet

Posted 09 April 2012 - 08:16 AM

Where does it fail ?

And put

public static long serialVersionUID = 123456L;

in your instance variables if it bothers you that much.
Was This Post Helpful? 0
  • +
  • -

#7 tawilovejessicalawrent  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 122
  • Joined: 08-April 12

Re: I want to make html file with Java applet

Posted 09 April 2012 - 05:24 PM

Well, it is working well in netbeans but when I put it in an html file, it shows nothing, am I putting wrong things? Where do I have to put that serialUID and what is that?
Was This Post Helpful? 0
  • +
  • -

#8 tawilovejessicalawrent  Icon User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 122
  • Joined: 08-April 12

Re: I want to make html file with Java applet

Posted 09 April 2012 - 05:30 PM

Do I have to put main either in those file?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: I want to make html file with Java applet

Posted 09 April 2012 - 07:33 PM

An Applet does not have a main() method.

The serialVersionUID is used, when you serialize an instance of a class an de-serialize it later to make sure that new newly read/create instance has the same version then the serialized one.
As you do not serialize you instance you don't have to worry about it.

Add println() in your code to see where you pass by and where you do not pass.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1