Magazine
 

Tips & Tricks

 

applet in the time format like: hours: minutes: seconds AM/PM (hh:mm:ss AM/PM). Here, the ClockApplet class name extends from the Applet class and implements to the Runnable interface. start() method creates a new instance of the Thread class and starts it. Loop in run() method lets the applet repaint itself till the current thread
is not null and tells the thread to sleep for 1 second. repaint() method calls the applet’s paint() method which updates the applet.

ClockApplet.java

import java.applet.*;
import java.awt.*;
import java.util.*;
public class ClockApplet extends Applet
implements Runnable{
Thread t,t1;
public void start(){
t = new Thread(this);
t.start();
}
public void run(){
t1 = Thread.currentThread();
while(t1 == t){
repaint();
try{
t1.sleep(1000);
}catch(InterruptedException e){}
}
}
public void paint(Graphics g){
Calendar cal = new GregorianCalendar();
String hour =
String.valueOf(cal.get(Calendar.HOUR));
String minute =
String.valueOf(cal.get(Calendar.MINUTE));
String second =
String.valueOf(cal.get(Calendar.SECOND));
int int_am_pm = cal.get(Calendar.AM_PM);
String str_am_pm = “AM”;
if(int_am_pm==1)
str_am_pm = “PM”;
Font f = new Font(“SansSerif”, Font.BOLD,
50);
g.setFont(f);
g.drawString(hour + “:” + minute + “:” +
second + “ “ + str_am_pm, 50, 50);
}
}

 

Output of the program:

4. Get Column Names From ResultSet in MySQL

In JDBC, ResultSet is used to retrieve column values using either the index number or the name of the column. ResultSet is generated by executing a statement that queries the database. If we want to find out name, type,
properties of the columns retrieved through ResultSet object then ResultSetMetaData object is used which is returned by the getMetaData() of ResultSet. In the example below, ResultSet contains values of all the
columns of the “employee” table of “test” database. The following code fragment creates the ResultSet object rs, creates the ResultSetMetaData object rsmd, and uses rsmd to find out number of columns rs has, types of
columns and lengths of columns, table name of the column. List of tables in test database used in the example has been given below:

Tables in test database in MySQL:
employee table:

empId empAdd
emp1 Delhi
emp2 Noida

 

Feb 2008 | Java Jazz Up | 65
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 ,

Download PDF