Browse File

18 11 2007

BrowseFile.java

import    java.awt.*;
import    java.awt.event.*;
import    javax.swing.*;
import    javax.swing.border.*;
import    java.io.*;
import    java.sql.*;
import    java.applet.*;
import    com.stefankrause.xplookandfeel.*;

public class BrowseFile extends JFrame
{
Container    container    =    new    Container();
GridBagLayout    layout    =    new    GridBagLayout();
GridBagConstraints    gbc    =    new    GridBagConstraints();
JLabel    nipLbl    =    new    JLabel(“NIP”),
namaLbl    =    new    JLabel(“Nama”),
photoLbl    =    new    JLabel(“”);
JTextField    nipTF    =    new    JTextField(10),
namaTF    =    new    JTextField(10);
JButton    insertBtn    =    new    JButton(“Insert”),
selectBtn    =    new    JButton(“Search”),
updateBtn    =    new    JButton(“Update”),
deleteBtn    =    new    JButton(“Delete”),
clearBtn    =    new    JButton(“Clear Screen”),
exitBtn    =    new    JButton(“Exit”),
browseBtn    =    new    JButton(“Browse”);
AudioClip    startUpAC,insertAC,searchAC,editAC,deleteAC,clearAC,exitAC;
static    File    fileDirectory    =    null;
File    photoFile    =    new    File(“”);
Image    photoImage;
Icon    photoIcon    =    new    ImageIcon(“14.gif”);
JFrame    frameFrame    =    new    JFrame();
JPanel    p1    =    new    JPanel(),
p2    =    new    JPanel();

String    filePathStr    =    “”,nipStr    =    “”;

public BrowseFile()
{
super(“Browse File”);
container    =    getContentPane();
gbc.anchor    =    GridBagConstraints.WEST;
gbc.insets    =    new    Insets(5,5,5,5);

p1.setLayout(layout);
photoLbl.setPreferredSize(new Dimension(1024/4,768/4));
photoLbl.setBorder(new LineBorder(Color.black));
photoLbl.setHorizontalAlignment(SwingConstants.CENTER);
photoLbl.setIcon(photoIcon);
posisi(1,1,1,1);    p1.add(nipLbl,gbc);
posisi(1,2,1,1);    p1.add(namaLbl,gbc);
posisi(2,1,1,1);    p1.add(nipTF,gbc);
posisi(2,2,1,1);    p1.add(namaTF,gbc);
posisi(2,3,2,1);    p1.add(photoLbl,gbc);
posisi(2,4,1,1);    p1.add(browseBtn,gbc);

p2.setLayout(layout);
posisi(1,1,1,1);    p2.add(insertBtn,gbc);
posisi(2,1,1,1);    p2.add(selectBtn,gbc);
posisi(3,1,1,1);    p2.add(updateBtn,gbc);
posisi(4,1,1,1);    p2.add(deleteBtn,gbc);
posisi(5,1,1,1);    p2.add(clearBtn,gbc);
posisi(6,1,1,1);    p2.add(exitBtn,gbc);

container.setLayout(layout);
posisi(1,1,1,1);    container.add(p1,gbc);
posisi(1,2,1,1);    container.add(p2,gbc);

browseBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
browse();
}
});

insertBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(!filePathStr.equals(“”))
insertWithPicture();
else
insertNoPicture();
}
});
insertSound();

selectBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
select();
}
});
searchSound();

updateBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(!filePathStr.equals(“”))
updateWithPicture();
else
updateNoPicture();
}
});
editSound();

deleteBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
delete();
}
});
deleteSound();

clearBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
nipTF.setText(“”);
namaTF.setText(“”);
photoLbl.setIcon(photoIcon);
nipTF.requestFocus();
clearAC.play();
}
});
clearSound();

exitBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int    flag    =    1;
flag    =    JOptionPane.showConfirmDialog(null,”Apakah anda in”+
“gin keluar?”,”Exit Question”,JOptionPane.YES_NO_OPTION);
if(flag == 0)
System.exit(0);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
});

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
int    flag    =    1;
flag    =    JOptionPane.showConfirmDialog(null,”Apakah anda in”+
“gin keluar?”,”Exit Question”,JOptionPane.YES_NO_OPTION);
if(flag == 0)
System.exit(0);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
});
}

public void posisi(int x,int y,int  w,int h)
{
gbc.gridx        =    x;    gbc.gridy    =    y;
gbc.gridwidth    =    w;    gbc.gridheight    =    h;
}

public void browse()
{
JFileChooser    photoFileChooser    =    new    JFileChooser();
String    fileNameStr    =    “”;
int    flag    =    photoFileChooser.showDialog(frameFrame,”Open”);

if(flag == JFileChooser.APPROVE_OPTION)
{
photoFile    =    photoFileChooser.getSelectedFile();
fileDirectory    =    photoFileChooser.getCurrentDirectory();
photoFileChooser.setCurrentDirectory(fileDirectory);
fileNameStr    =    photoFile.getName();

if(!photoFile.exists())
JOptionPane.showMessageDialog(null,fileNameStr+” tidak ada”,
“Warning”,JOptionPane.WARNING_MESSAGE);
else
{
filePathStr    =    photoFile.getPath();
if(“jpg”.equalsIgnoreCase(fileNameStr.substring(
fileNameStr.length()-3)))
{
photoImage    =    Toolkit.getDefaultToolkit().getImage(
filePathStr);
ImageIcon    a    =    new    ImageIcon(photoImage);
ImageIcon    b    =    null;
if(a.getIconWidth() <= 1024/4 && a.getIconHeight() <= 768/4)
photoLbl.setIcon(a);
else
{
b    =    new    ImageIcon(a.getImage().getScaledInstance(
1024/4,768/4,Image.SCALE_SMOOTH));
photoLbl.setIcon(b);
}
}
else if(“gif”.equalsIgnoreCase(fileNameStr.substring(
fileNameStr.length()-3)))
{
photoImage    =    Toolkit.getDefaultToolkit().getImage(
filePathStr);
ImageIcon    a    =    new    ImageIcon(photoImage);
ImageIcon    b    =    null;
if(a.getIconWidth() <= 1024/4 && a.getIconHeight() <= 768/4)
photoLbl.setIcon(a);
else
{
b    =    new    ImageIcon(a.getImage().getScaledInstance(
1024/4,768/4,Image.SCALE_SMOOTH));
photoLbl.setIcon(b);
}
}
else if(“png”.equalsIgnoreCase(fileNameStr.substring(
fileNameStr.length()-3)))
{
photoImage    =    Toolkit.getDefaultToolkit().getImage(
filePathStr);
ImageIcon    a    =    new    ImageIcon(photoImage);
ImageIcon    b    =    null;
if(a.getIconWidth() <= 1024/4 && a.getIconHeight() <= 768/4)
photoLbl.setIcon(a);
else
{
b    =    new    ImageIcon(a.getImage().getScaledInstance(
1024/4,768/4,Image.SCALE_SMOOTH));
photoLbl.setIcon(b);
}
}
else
{
JOptionPane.showMessageDialog(null,”Tidak ada foto yang “+
“dipilih, silahkan pilih kembali”,”Warning”,
JOptionPane.WARNING_MESSAGE);
browse();
}
}
}
}

public void insertWithPicture()
{
try
{
photoFile    =    new    File(filePathStr);
FileInputStream    fis    =    new    FileInputStream(photoFile);
Class.forName(“com.mysql.jdbc.Driver”);
Connection    connection    =    DriverManager.getConnection(“jdbc:mysq”+
“l://localhost/picture”,”root”,”malik”);
String    sql    =    “INSERT INTO photo (nip,nama,image) values (‘”+
nipTF.getText()+”‘,'”+namaTF.getText()+”‘,?);”;
PreparedStatement    statement    =    connection.prepareStatement(
sql);
statement.setBinaryStream(1,fis,(int)photoFile.length());
statement.executeUpdate();
statement.close();
fis.close();
connection.close();
JOptionPane.showMessageDialog(null,”Data telah disimpan”,
“Insert Data”,JOptionPane.INFORMATION_MESSAGE);
insertAC.play();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,”Error pada fungsi insertWithPi”+
“cture”,”Error”,JOptionPane.ERROR_MESSAGE);
}
}

public void insertNoPicture()
{
try
{
Class.forName(“com.mysql.jdbc.Driver”);
Connection    connection    =    DriverManager.getConnection(“jdbc:mysq”+
“l://localhost/picture”,”root”,”malik”);
Statement    statement    =    connection.createStatement();
String    sql    =    “INSERT INTO photo (nip,nama) values (‘”+
nipTF.getText()+”‘,'”+namaTF.getText()+”‘);”;
statement.executeUpdate(sql);
connection.close();
statement.close();
connection.close();
JOptionPane.showMessageDialog(null,”Data telah disimpan”,
“Insert Data”,JOptionPane.INFORMATION_MESSAGE);
insertAC.play();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,”Error pada fungsi insertNoPict”+
“ure”,”Error”,JOptionPane.ERROR_MESSAGE);
}
}

public void select()
{
try
{
Class.forName(“com.mysql.jdbc.Driver”);
Connection    connection    =    DriverManager.getConnection(“jdbc:mysq”+
“l://localhost/picture”,”root”,”malik”);
Statement    statement    =    connection.createStatement();
String    sql    =    “SELECT * FROM photo WHERE nip='”+nipTF.getText()+
“‘;”;
ResultSet    rs    =    statement.executeQuery(sql);
if(rs.next())
{
nipStr    =    nipTF.getText();
nipTF.setText(rs.getString(1));
namaTF.setText(rs.getString(2));
if(rs.getBinaryStream(3) != null)
{
Blob    foto    =    rs.getBlob(3);
byte    besarPhoto[]    =    foto.getBytes(1,(int)foto.length());
ImageIcon    a    =    new    ImageIcon(besarPhoto);
ImageIcon    b    =    null;
if(a.getIconWidth() <= 1024/4 && a.getIconHeight() <= 768/4)
photoLbl.setIcon(a);
else
{
b    =    new    ImageIcon(a.getImage(
).getScaledInstance(1024/4,768/4,Image.SCALE_SMOOTH));
photoLbl.setIcon(b);
}
}
}
else
JOptionPane.showMessageDialog(null,”Data tidak ditemukan”,
“Search Data”,JOptionPane.INFORMATION_MESSAGE);
statement.close();
connection.close();
searchAC.play();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,”Error pada fungsi select”,
“Error”,JOptionPane.ERROR_MESSAGE);
}
}

public void updateWithPicture()
{
try
{
photoFile    =    new    File(filePathStr);
FileInputStream    fis    =    new    FileInputStream(photoFile);
Class.forName(“com.mysql.jdbc.Driver”);
Connection    connection    =    DriverManager.getConnection(“jdbc:mysq”+
“l://localhost/picture”,”root”,”malik”);
String    sql    =    “UPDATE photo SET nip='”+nipTF.getText()+”‘,nama='”+
namaTF.getText()+”‘,image=? WHERE nip='”+nipStr+
“‘;”;
PreparedStatement    statement    =    connection.prepareStatement(
sql);
statement.setBinaryStream(1,fis,(int)photoFile.length());
statement.executeUpdate();
statement.close();
fis.close();
connection.close();
JOptionPane.showMessageDialog(null,”Data telah diubah”,”Ubah Data”,
JOptionPane.INFORMATION_MESSAGE);
editAC.play();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,”Error pada fungsi updateWithPi”+
“cture”,”Error”,JOptionPane.ERROR_MESSAGE);
}
}

public void updateNoPicture()
{
try
{
Class.forName(“com.mysql.jdbc.Driver”);
Connection    connection    =    DriverManager.getConnection(“jdbc:mysq”+
“l://localhost/picture”,”root”,”malik”);
Statement    statement    =    connection.createStatement();
String    sql    =    “UPDATE photo SET nip='”+nipTF.getText()+”‘,nama='”+
namaTF.getText()+”‘ WHERE nip='”+nipStr+”‘;”;
statement.executeUpdate(sql);
statement.close();
connection.close();
JOptionPane.showMessageDialog(null,”Data telah diubah”,”Ubah Data”,
JOptionPane.INFORMATION_MESSAGE);
editAC.play();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,”Error pada fungsi updateNoPict”+
“ure”,”Error”,JOptionPane.ERROR_MESSAGE);
}
}

public void delete()
{
try
{
Class.forName(“com.mysql.jdbc.Driver”);
Connection    connection    =    DriverManager.getConnection(“jdbc:mysq”+
“l://localhost/picture”,”root”,”malik”);
Statement    statement    =    connection.createStatement();
String    sql    =    “DELETE FROM photo WHERE nip='”+nipStr+”‘;”;
statement.executeUpdate(sql);
statement.close();
connection.close();
JOptionPane.showMessageDialog(null,”Data telah dihapus”,”Hapus Dat”+
“a”,JOptionPane.INFORMATION_MESSAGE);
nipTF.setText(“”);
namaTF.setText(“”);
photoLbl.setIcon(photoIcon);
nipTF.requestFocus();
deleteAC.play();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,”Error pada fungsi delete”,
“Error”,JOptionPane.ERROR_MESSAGE);
}
}

private void startUpSound()
{
Sound    objSound    =    new    Sound();
startUpAC    =    objSound.getAudioClip(“startUp.wav”);
}

private void insertSound()
{
Sound    objSound    =    new    Sound();
insertAC    =    objSound.getAudioClip(“Insert.wav”);
}

private void searchSound()
{
Sound    objSound    =    new    Sound();
searchAC    =    objSound.getAudioClip(“Search.wav”);
}

private void editSound()
{
Sound    objSound    =    new    Sound();
editAC    =    objSound.getAudioClip(“Edit.wav”);
}

private void deleteSound()
{
Sound    objSound    =    new    Sound();
deleteAC    =    objSound.getAudioClip(“Delete.wav”);
}

private void clearSound()
{
Sound    objSound    =    new    Sound();
clearAC    =    objSound.getAudioClip(“Clear.wav”);
}

private void exitSound()
{
Sound    objSound    =    new    Sound();
exitAC    =    objSound.getAudioClip(“Exit.wav”);
}

public static void main(String [] args)
{
try
{
UIManager.setLookAndFeel(
new com.stefankrause.xplookandfeel.XPLookAndFeel());
new    BrowseFile();
}
catch(Exception e)
{
System.out.println(“Error pada fungsi main”);
}
}
}

sound.java

import java.applet.*;

public class Sound
{
private String prefix    =    “”;

public Sound()
{

}

public AudioClip getAudioClip(String soundFile)
{
try
{
return Applet.newAudioClip(getClass().getResource(
prefix+soundFile));
}
catch ( NullPointerException nullPointerException )
{
return null;
}
}

public void setPathPrefix(String string)
{
prefix    =    string;
}
}

sql source

CREATE DATABASE picture;
FLUSH PRIVILEGES;
USE picture;

CREATE TABLE photo(nip VARCHAR(10) PRIMARY KEY, nama VARCHAR(100) NOT NULL, image MEDIUMBLOB);


Actions

Information

One response

19 11 2007
rgseven

il ysng lengkap lagi beritanay

Leave a comment