Showing posts with label j2se. Show all posts
Showing posts with label j2se. Show all posts

Wednesday, January 14, 2009

How to Connect Java to DB2

It's not difficult to connect java application to DB2. It's almost the same like connect java to other DBMS such as access, SQL server and so on.

This is the snippet code.

String DB_URL = “jdbc:db2:SAMPLE”;
String userid = “db2admin”;
String password = “mypasswd”;

Connection connection = null;

Properties connectProperties = new Properties();
connectProperties.put(”user”, userid);
connectProperties.put(”password”, password);

Class.forName(”com.ibm.db2.jcc.DB2Driver”).newInstance();
connection = DriverManager.getConnection(DB_URL, connectProperties);
PreparedStatement pstmt = connection.prepareStatement(”SELECT * FROM employee”);
ResultSet rs = pstmt.executeQuery();

Best regards,

Deny Sutani

Tuesday, January 13, 2009

Change Font in Java

This is an example code how to change a font in Java application.

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import java.awt.Font;

public class Teks extends JFrame {
JLabel lbl1 = new JLabel(”Helvetica biasa”);
JLabel lbl2 = new JLabel(”SansSerif bold”);
JLabel lbl3 = new JLabel(”Monospaced italic”);
JLabel lbl4 = new JLabel(”Serif biasa”);

public Teks()
{
setTitle(”Font”);
setApplicationLayout();
setSize(175,200);
setLocationRelativeTo(null);
setVisible(true);
}

public static void main(String args[])
{
Teks teks = new Teks();
teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void setApplicationLayout()
{
setLayout(new FlowLayout());
lbl1.setFont(new Font (”Helvetica”, Font.PLAIN, 16));
lbl2.setFont(new Font (”SansSerif”, Font.BOLD, 16));
lbl3.setFont(new Font (”Monospaced”, Font.ITALIC, 16));
lbl4.setFont(new Font (”Serif”, Font.PLAIN, 24));
add(lbl1);
add(lbl2);
add(lbl3);
add(lbl4);
}
}

Best regards,

Deny Sutani

Digital Clock in Java

Oneday, my friend ask about how to make digital clock in java. It's like a timer, when the second has reach 60, the minute will be added by 1, when the minute reach 60, the hour will be added by 1.

This is the code. Hope this code will be useful not only for my friend, but also other people who need it.

import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.FlowLayout;

public class Jam extends JFrame {

private Timer timer;

int detik,menit,jam = 0;

JLabel lblDetik = new JLabel(”");
JLabel lblMenit = new JLabel(”");
JLabel lblJam = new JLabel(”");

public Jam()
{
setTitle(”Jam”);
setApplicationLayout();
setSize(150,75);
setLocationRelativeTo(null);
setVisible(true);
}

public static void main(String args[])
{
Jam jam = new Jam();
jam.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

class PuterJam extends TimerTask
{
public void run()
{
detik++;
if(detik==60)
{
detik=0;
menit++;
}
if(menit==60)
{
menit=0;
jam++;
}
if(detik<10) lblDetik.setText(”0″+detik);
else lblDetik.setText(”"+detik);

if(menit<10) lblMenit.setText(”0″+menit+” : “);
else lblMenit.setText(”"+menit+” : “);

if(jam<10) lblJam.setText(”0″+jam+” : “);
else lblJam.setText(”"+jam+” : “);
}
}

public void setApplicationLayout()
{
timer = new Timer();
timer.schedule(new PuterJam(), 0,1*1000);
setLayout(new FlowLayout());
add(lblJam);
add(lblMenit);
add(lblDetik);

}

}

Best regards,

Deny Sutani

Insert an Icon to JInternalFrame

Often, we insert an icon into a JFrame in Java application, but how to insert an icon into JInternalFrame? It's almost the same, it's only need to use different method. We can user setFrameIcon() method.

This is the snippet code
URL url = getClass().getResource(”image/login.gif”);
Icon icon = new ImageIcon(url);
setFrameIcon(icon);

Best regards,

Deny Sutani