Java Applet is a Java Program that can be embedded in HTML documents. Usually, we use an applet viewer to show the result of our program. For java applet, we must create an HTML documents that specifies which applet class to execute. We will learn by making the applet class and making the simple HTML code that can embed our program.
This time, I only use the simple JApplet program, just to make sure that in Applet we can also make a connection to MySQL Database. 🙂
In this Tutorial, we will use 2 class that we have created in past tutorial, Connect.java and Sqlstatement.java.
First, make sure that the webserver has been started. I use XAMPP 1.6.6a, and I use the database that we’ve made in the past JDBC tutorial.
Then, Create your java library Project from Netbeans :
After that, add the MySQL JDBC :
Then add a class named Blablabla.java, for this tutorial I use Welcome.java. This class in functioned as an Applet. The content is look like this :
import javax.swing.JApplet; import javax.swing.JOptionPane; import java.awt.Graphics; /** * * @author ferdiansyah.dolot */ public class Welcome extends JApplet{ String connection = ""; String insertion =""; public void init(){ try{ Connect c = new Connect(); connection = "Connection Established"; Sqlstatement s = new Sqlstatement(); s.insert("Dolot",3); s.insert("Gery",4); insertion = "Success"; } catch(Exception e){ insertion="Failed"; connection="Connection Failed"; } } public void paint(Graphics g){ super.paint(g); g.drawString(connection,25,25); g.drawString(insertion,25,45); } }
Samples method that we use in applet program above are init () and paint(Graphics g). The applet container first called init() method when applet is loaded for exection, after that paint(Graphics g) method is executed. More about JavaApplet you can read here : http://java.sun.com/applets/ . The two method init() and paint(Graphics g) are JApplet life-cycle method that are called by an applet container during an applet’s execution. About JApplet life-cycle you can read them here : http://java.sun.com/docs/books/tutorial/deployment/applet/lifeCycle.html
After making the Applet class, then we must make the HTML file to specify which applet class we use. The simple HTML code is look like this :
<html>
<applet code=”Welcome.class” width=”250″ height=”80″></applet>
</html>
In HTML above, we specify the width of applet viewer is 250 and the height is 80, and we use Welcome class. Save this HTML document in the same folder of src in your Netbeans Project folder (example : …\NetBeansProjects\Project1\src), put the HTML code in src folder.
After that, run the Welcome class in Netbeans, then it works.
Then’ It’s possible to use applet with JDBC. 🙂
Thanks for reading.
Past Tutorial to find the Java Past Program that we use in this tutorial :
Tega bener gw gak di link ~ btw nice info keep posting dol!
It doesn’t work out of Netbeans environment.
udah run yg welcome..kan kalo sukses biasanya udah langsung kebentuk class.nya….tp ga ada..liat class di netbeans dimananya…???
udah di run.. success..tp ga muncul yang applet gitu…
Terima kasih untuk tipsnya, saya mau coba semoga juga.
Info menarik dan boleh sekali dicoba, Makasih buat infonya dan sukses selalu.
IH -_-”
GEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEK 😐
maksih ilmunya….