Docstoc

PROGRAM KEPEGAWAIAN java

Document Sample
PROGRAM KEPEGAWAIAN java Powered By Docstoc
					PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA



                         PROGRAM DATABASE KEPEGAWAIAN
                   MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA
                                    (By : Dedy Wibowo)




                   MATA KULIAH PEMROGRAMAN BERORIENTASI OBJEK
                      FAKULTAS MIPA JURUSAN ILMU KOMPUTER
                                     UNPAK
                                     BOGOR
                                      2009


By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                                       Rancangan Form input Kepegawaian


           Table utama saat database.jar di eksekusi atau dijalankan




                                                (gambar menu awal)




                      (Sebelum dibuat table)                           (Sesudah table dibuat)

                 Saat user mengklik buat maka program akan membuat table baru,setelah itu table baru
           akan tercipta untuk dilakukan penginputan atau pengolahan data.




By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA



                                             Form Pengisian Data

           Sesudah table dibuat tahap selanjutnya adalah mensgisi form Kepegawaian.




                                              (gambar pengisian form)


                                                      Output
           Hasil output yang dihasilkan adalah dalam bentuk notepad sebagai berikut.




                                      (output yang dihasilkan dlm bentuk .txt)



By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA



                         Action script Database kepegawaian menggunakan JAVA
           1. Database
              import javax.swing.JFrame;
              public class database extends JFrame
              {public static void main (String[] args)
                 {JFrame.setDefaultLookAndFeelDecorated(true);
                   JFrame frame = new JFrame("DataBase dheday");
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   databaseGUI databaseWindow = new databaseGUI();
                   frame.setContentPane(databaseWindow.getGUI());
                   frame.setJMenuBar(databaseWindow.getMenu());
                   frame.setSize(databaseWindow.WIDTH, databaseWindow.HEIGHT);
                   frame.setResizable(false);
                   frame.setVisible(true);
                   frame.pack();
                    }}

           2. DataBaseMenu
               import javax.swing.JMenu;
               import javax.swing.JMenuBar;
               import javax.swing.JMenuItem;
               import java.awt.event.ActionListener;
               public class databaseMenu extends JMenuBar
               {private JMenu mnuFile, mnuRecords, mnuOutput;
                private JMenuItem mItemFileNew, mItemFileOpen, mItemFileDelete, mItemFileClose,
               mItemFileCloseAll, mItemFileExit;
                   private JMenuItem mItemOutputXML, mItemOutputTXT;
                   private void setupFileMenu (ActionListener mainApp)
                   {       mnuFile = new JMenu("File");
                            mItemFileNew = new JMenuItem("data baru");
                            mItemFileOpen = new JMenuItem("Buka Tabel");
                            mItemFileClose = new JMenuItem("Tutup Tabel");
                            mItemFileCloseAll = new JMenuItem("Tutup Semua Tabel");
                            mItemFileDelete = new JMenuItem("Delete Table");
                            mItemFileExit = new JMenuItem("keluar");
                            mItemFileNew.addActionListener(mainApp);
                            mItemFileOpen.addActionListener(mainApp);
                            mItemFileClose.addActionListener(mainApp);
                            mItemFileCloseAll.addActionListener(mainApp);
                            mItemFileDelete.addActionListener(mainApp);
                            mItemFileExit.addActionListener(mainApp);
                            mItemFileNew.setActionCommand("data baru");
                            mItemFileOpen.setActionCommand("selectTable");
                            mItemFileClose.setActionCommand("closeTable");
                            mItemFileCloseAll.setActionCommand("closeAll");
                            mItemFileDelete.setActionCommand("deleteTable");
                            mItemFileExit.setActionCommand("Keluar");
                            mnuFile.add(mItemFileNew);
                            mnuFile.add(mItemFileOpen);
                            mnuFile.addSeparator();
                            mnuFile.add(mItemFileClose);
                            mnuFile.add(mItemFileCloseAll);
                            mnuFile.addSeparator();
                            mnuFile.add(mItemFileDelete);

By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                            mnuFile.add(mItemFileExit);
                            }
                   private void setupRecordsMenu (ActionListener mainApp)
                   {        mnuRecords = new JMenu("Menyimpan");
                            mnuOutput = new JMenu("Simpan Ke");
                            mItemOutputXML = new JMenuItem("Dokumen XML");
                            mItemOutputTXT = new JMenuItem("Dokumen TXT");
                            mItemOutputXML.addActionListener(mainApp);
                            mItemOutputTXT.addActionListener(mainApp);
                            mItemOutputTXT.setActionCommand("outputTXT");
                            mItemOutputXML.setActionCommand("outputXML");
                            mnuRecords.add(mnuOutput);
                            mnuOutput.add(mItemOutputXML);
                            mnuOutput.add(mItemOutputTXT);
                            }
                   public databaseMenu (ActionListener mainApp)
                   {        setupFileMenu(mainApp);
                            setupRecordsMenu(mainApp);
                            this.add(mnuFile);
                            this.add(mnuRecords);
                            }
                            public void tableOpen (boolean state)
                   {        mnuOutput.setEnabled(state);
                            mItemFileClose.setEnabled(state);
                            mItemFileCloseAll.setEnabled(state);
                            }}

           3. deleteTabel
                   import javax.swing.JFrame;
                   import java.awt.event.ActionEvent;
                   public class deleteTable extends abstractInputTableDialogue
                   {public deleteTable (JFrame mainApp, String[] strList)
                            {         super(mainApp, strList);
                                      this.setTitle("Delete tabel yang ada");
                                      cmdClick.setText("Delete");
                            }
                            public void actionPerformed (ActionEvent e)
                            {         if (e.getSource() == cmdCancel)
                                      {this.setVisible(false);
                                      }
                                      else
                                      {String strTableName = txtTable.getText();
                                      if (!strTableName.equalsIgnoreCase(""))
                                      {
                                      if (exists(strTableName))
                                      {strChosenTable = strTableName;
                                      this.setVisible(false);
                                      }
                                      else
                                      {lblStatus.setText("Table tidak berfungsi");
                                      }}}}}

           4. ObjCreateAppletImage
                   import java.awt.Toolkit;
                   import java.io.BufferedInputStream;
                   import java.io.FileInputStream;

By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                   import java.io.IOException;
                   import javax.swing.ImageIcon;
                   public class objCreateAppletImage
                   {public static ImageIcon getImageIcon (String path, String description, int fileSize) throws Exception
                            {        int count = 0;
                                     BufferedInputStream imgStream = new BufferedInputStream(new
                   FileInputStream(path));
                                     byte buff[] = new byte[fileSize];
                                     if (imgStream == null) //If doesn't exist
                                     {throw new Exception("File tidak ditemukan");
                                     }
                                     try
                                     {        count = imgStream.read(buff);
                                              imgStream.close();
                                     }
                                     catch (IOException ex)
                                     {        throw new Exception("file korup");
                                     }
                   return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buff), description);
                   }}

           5. SelectTabel
                  import javax.swing.JFrame;
                  import java.awt.event.ActionEvent;
                  public class selectTable extends abstractInputTableDialogue
                  { public selectTable (JFrame mainApp, String[] strList)
                  {super(mainApp, strList);
                           this.setTitle("pilih tabel untuk menyimpan record");
                           cmdClick.setText("Pilih");
                           }
                           public void actionPerformed (ActionEvent e)
                           {if (e.getSource() == cmdCancel)
                           {this.setVisible(false);
                           }
                           else
                           {String strTableName = txtTable.getText();
                           if (!strTableName.equalsIgnoreCase(""))
                           {if (exists(strTableName))
                           {strChosenTable = strTableName;
                           this.setVisible(false);
                           }
                           else
                           {lblStatus.setText("Tabel tidak berfungsi");
                           }}}}}
           6. Connectdatabase
                  import java.sql.Connection;
                  import java.sql.DriverManager;
                  import java.sql.SQLException;
                  public class connectDatabase //Static class for connecting to a database
                  {private static String strDriver = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; //Only
                  can do MS Access at the momment
                  public static Connection getConnection (String strFileName, String strUserName, String strPassWord)
                  throws Exception
                           {Connection databaseConnection;
                                      try
                           {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Asumming it tests if the driver is supported

By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                             strDriver += strFileName.trim() + ";DriverID=22;READONLY=true}"; //Adds to the driver the
                   database address and extra stuff
                           databaseConnection = DriverManager.getConnection(strDriver, strUserName, strPassWord);
                   //Create the connection
                        }
                        catch (ClassNotFoundException ex) //Thrown if driver is not reconised
                        { throw new Exception("\nError loading database access driver.\nPossible reason is that you do not
                   have MS Access");
                        }
                        catch (SQLException ex) //Thrown if database can not be found
                        {      throw new Exception("\nError connecting to database\n" + System.getProperty("user.dir") + "\\"
                   + strFileName + "\nPlease make sure the database exists");
                        }
                        return databaseConnection;
                          }}

           7. DatabaseGui
                   import javax.swing.JPanel;
                   import javax.swing.JButton;
                   import javax.swing.JMenuBar;
                   import javax.swing.JOptionPane;
                   import javax.swing.JTabbedPane;
                   import java.awt.Dimension;
                   import java.awt.Color;
                   import java.awt.Container;
                   import java.awt.BorderLayout;
                   import java.awt.event.ActionListener;
                   import java.awt.event.ActionEvent;
                   import java.sql.Connection;
                   import java.sql.SQLException;
                   public class databaseGUI extends JPanel implements ActionListener
                   {        private final String strFileName = "bardaxs.mdb";
                            private final int width = 800, height = 300;
                            private JTabbedPane tabbedSystem;
                            private databaseToolbar panToolbar;
                            private databaseMenu menuSystem;
                            private tableList     listTables;
                            private Connection       databaseConnection;
                            private void setupConnection ()
                            {
                            try
                            {databaseConnection = connectDatabase.getConnection(strFileName, "","");
                                      }
                                      catch (Exception ex)
                                      {
                            JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error",
                   JOptionPane.ERROR_MESSAGE);
                                               System.exit(0);
                                      }}
                            private void setupTableList ()
                            { try
                            { listTables = new tableList(databaseConnection);
                            }
                                      catch (SQLException ex)



By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                             { JOptionPane.showMessageDialog(null, "Error reading from\n" + System.getProperty("user.dir")
                   + "\\" + strFileName + ". Please check permissions", "Database Access Error",
                   JOptionPane.ERROR_MESSAGE);
                             System.exit(0);
                             }}
                             public databaseGUI ()
                             {this.setPreferredSize(new Dimension(width, height));
                             this.setLayout(new BorderLayout());
                             tabbedSystem = new JTabbedPane();
                             tabbedSystem.setPreferredSize(new Dimension(width, (height - 40)));
                             setupConnection();
                             setupTableList();
                             menuSystem = new databaseMenu(this);
                             menuSystem.tableOpen(false);
                             panToolbar = new databaseToolbar(this);
                             panToolbar.tableOpen(false);
                             this.add(panToolbar, BorderLayout.NORTH);
                             this.add(tabbedSystem, BorderLayout.SOUTH);
                             }
                             public Container getGUI ()
                             {return this;
                             }
                             public JMenuBar getMenu ()
                             { return menuSystem;
                             }
                             private boolean isTableOpen (String strTableName)
                             {for (int i = 0; i < tabbedSystem.getTabCount(); i++)
                             {if (tabbedSystem.getTitleAt(i).equalsIgnoreCase(strTableName))
                             { return true;
                             }}
                             return false;
                             }
                             private void tableOpen (boolean state)
                             {menuSystem.tableOpen(state);
                             panToolbar.tableOpen(state);
                             }
                             public void actionPerformed (ActionEvent e)
                             {String strCommand = e.getActionCommand();
                             if (strCommand.equalsIgnoreCase("newTable"))
                             {createTable digCreate = new createTable(null, listTables.getList());
                             digCreate.setVisible(true);
                             String strTableName = digCreate.getChosenTable();
                             if (!strTableName.equals(""))
                             {try
                             {listTables.addTable(strTableName);
                             tabbedSystem.addTab(strTableName, new objTab(strTableName, databaseConnection));
                             tabbedSystem.setSelectedIndex(tabbedSystem.getTabCount() - 1);
                             JOptionPane.showMessageDialog(null, "Table " + strTableName + " was created
                             successfully\nand is now open for inputting records into", "Table Created",
                             JOptionPane.INFORMATION_MESSAGE);
                             tableOpen(true);
                             }
                             catch (Exception ex)
                             {JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error",
                   JOptionPane.ERROR_MESSAGE);
                             }}}

By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                            else if (strCommand.equalsIgnoreCase("selectTable"))
                            {selectTable digSelect = new selectTable(null, listTables.getList());
                            digSelect.setVisible(true);
                            String strTableName = digSelect.getChosenTable();
                            if (!strTableName.equals(""))
                            {if (isTableOpen(strTableName))
                            {JOptionPane.showMessageDialog(null, "Tabel sedang terbuka", "Database Access Error",
                   JOptionPane.ERROR_MESSAGE);
                            }
                            else
                            {try
                            {tabbedSystem.addTab(strTableName, new objTab(strTableName, databaseConnection));
                            tableOpen(true);
                            }
                            catch (Exception ex)
                            {JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error",
                   JOptionPane.ERROR_MESSAGE);
                            }}}}
                            else if (strCommand.equalsIgnoreCase("deleteTable"))
                            {deleteTable digDelete = new deleteTable(null, listTables.getList());
                            digDelete.setVisible(true);
                            String strTableName = digDelete.getChosenTable();
                            if (!strTableName.equals(""))
                            {if (isTableOpen(strTableName))
                                                        {
                                                               JOptionPane.showMessageDialog(null, "Tidak Bisa delete
                   table yang belum terbuka", "Database Access Error", JOptionPane.ERROR_MESSAGE);
                                                        }
                                                        else
                                                        {try
                                                               {

                                                                      listTables.deleteTable(strTableName);
                                                                      JOptionPane.showMessageDialog(null, "Table " +
                   strTableName + " Sukses Mendelete", "Tabel didelete", JOptionPane.INFORMATION_MESSAGE);

                                                              }
                                                              catch (Exception ex)
                                                              {
                                                                       JOptionPane.showMessageDialog(null,
                   ex.getMessage(), "Database Access Error", JOptionPane.ERROR_MESSAGE);
                                                              }}}}
                                    else if (strCommand.equalsIgnoreCase("closeTable"))
                                    {

                                            tabbedSystem.remove(tabbedSystem.getSelectedIndex());

                                               if (tabbedSystem.getTabCount() == 0)
                                               {
                                                        tableOpen(false);
                                               }}
                                    else if (strCommand.equalsIgnoreCase("closeAll"))
                                    {          tabbedSystem.removeAll();
                                               tableOpen(false);
                                    }
                                    else if (strCommand.equalsIgnoreCase("outputTXT"))

By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                                  {try
                                  {String strTable = tabbedSystem.getTitleAt(tabbedSystem.getSelectedIndex());
                                                       outputFile.TXT(listTables.getRecords(strTable), strTable);
                                             }
                                             catch (Exception ex)
                                             {JOptionPane.showMessageDialog(null, ex.getMessage(), "Writing Error",
                   JOptionPane.ERROR_MESSAGE);
                                             }}
                                  else if (strCommand.equalsIgnoreCase("outputXML"))
                                  {try
                                             {
                                             String strTable = tabbedSystem.getTitleAt(tabbedSystem.getSelectedIndex());
                                                       outputFile.XML(listTables.getRecords(strTable), strTable);
                                             }
                                             catch (Exception ex)
                                             {JOptionPane.showMessageDialog(null, ex.getMessage(), "Writing Error",
                   JOptionPane.ERROR_MESSAGE);
                                             }}
                                  else if (strCommand.equalsIgnoreCase("exit"))
                                  {System.exit(0);
                                  }}}

           8. DatabaseToolBar
                   import javax.swing.JPanel;
                   import javax.swing.JLabel;
                   import javax.swing.JButton;
                   import java.awt.event.ActionListener;
                   public class databaseToolbar extends JPanel
                   {private final String[] strToolbarTitles = new String[] {"Buat", "Pilih", "Tutup", "Tutup Semua", "XML",
                   "TXT", "Delete", "Keluar"};
                             private final String[] strToolbarTips = new String[] {"Create a new table", "Select a table for
                   editing", "Close the selected table", "Close all open tables", "Output the current table's records to XML",
                   "Output the current table's records to TXT", "Delete a table", "Exit the application"};
                             private final String[] strToolbarAction = new String[]{"newTable", "selectTable", "closeTable",
                   "closeAll", "outputXML", "outputTXT", "deleteTable", "exit"};
                             private final boolean[] spaceSort = new boolean[] {false, false, true, false, true, false, true, false};
                             private final boolean[] allowDisable = new boolean[] {false, false, true, true, true, true, false,
                   false};
                             private JButton[] cmdToolbarButton;
                             private JButton cmdCreate, cmdSelect, cmdClose, cmdCloseAll, cmdDelete, cmdExit;
                             private JButton cmdXML, cmdTXT;
                             public databaseToolbar (ActionListener mainApp)
                             {cmdToolbarButton = new JButton[strToolbarTitles.length];
                             for (int i = 0; i < strToolbarTitles.length; i++)
                             {cmdToolbarButton[i] = new JButton(strToolbarTitles[i]);
                             cmdToolbarButton[i].setToolTipText(strToolbarTips[i]);
                             cmdToolbarButton[i].addActionListener(mainApp);
                             cmdToolbarButton[i].setActionCommand(strToolbarAction[i]);
                             if (spaceSort[i])
                             {this.add(new JLabel(" "));
                             }
                             this.add(cmdToolbarButton[i]);
                             }
                             try
                             {for (int i = 0; i < strToolbarTitles.length; i++)


By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                             {cmdToolbarButton[i].setIcon(objCreateAppletImage.getImageIcon("images/toolbarIcon" + i +
                   ".gif", "", 3000));
                             }}
                             catch (Exception ex)
                             {}}
                             public void tableOpen (boolean state)
                             {for (int i = 0; i < strToolbarTitles.length; i++)
                             {if (allowDisable[i])
                             {cmdToolbarButton[i].setEnabled(state);
                             }}}}

           9. ObjTab
                   import javax.swing.JPanel;
                   import javax.swing.JButton;
                   import javax.swing.JLabel;
                   import javax.swing.JTextField;
                   import java.awt.GridLayout;
                   import java.awt.event.ActionListener;
                   import java.awt.event.ActionEvent;
                   import java.util.Calendar;
                   import java.sql.Connection;
                   import java.sql.Statement;
                   import java.sql.SQLException;
                   public class objTab extends JPanel implements ActionListener
                   {private String strTable = "";
                   private JButton cmdSave;
                   private JLabel lblTitle, lblYear, lblStatus,lblNpm,lblNanak,lblGol,lblPensiun,lblTgl,lblJabatan,lblCuti;
                   private JPanel panTitle,panNpm,panYear,
                   panStatus,panNanak,panGol,panPensiun,panTgl,panJabatan,panCuti;
                   private JTextField txtTitle,txtNpm, txtYear,txtNanak,txtGol,txtPensiun,txtTgl,txtJabatan,txtCuti;
                   private Connection databaseConnection;
                   public objTab (String strTable, Connection databaseConnection)
                             {this.strTable = strTable;
                             this.databaseConnection = databaseConnection;
                             cmdSave = new JButton("Simpan");
                             cmdSave.addActionListener(this);
                             lblTitle = new JLabel ("Nama pegawai :",JLabel.RIGHT);
                             lblNpm = new JLabel ("IDNIKNIP :",JLabel.RIGHT);
                              lblYear = new JLabel ("Tahun Umur :",JLabel.RIGHT);
                              lblNanak = new JLabel ("Nama Anak :",JLabel.RIGHT);
                              lblGol = new JLabel ("Golongan :",JLabel.RIGHT);
                              lblPensiun = new JLabel ("Pensiun     :",JLabel.RIGHT);
                              lblTgl = new JLabel ("Tgl. Masuk :",JLabel.RIGHT);
                              lblJabatan = new JLabel ("Jabatan :",JLabel.RIGHT);
                              lblCuti = new JLabel ("Cuti panjang :",JLabel.RIGHT);
                           lblStatus = new JLabel("Masukan data dan kemudian simpan", JLabel.CENTER);
                             txtTitle = new JTextField("",10);
                             txtNpm = new JTextField("",10);
                             txtYear = new JTextField("",10);
                              txtNanak = new JTextField("",10);
                              txtGol = new JTextField("",10);
                              txtPensiun=new JTextField("",10);
                              txtTgl = new JTextField("",10);
                              txtJabatan = new JTextField ("",10);
                              txtCuti = new JTextField ("",10);
                             panTitle = new JPanel();

By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                    panNpm = new JPanel();
                     panYear = new JPanel();
                       panNanak = new JPanel();
                       panGol = new JPanel();
                       panPensiun=new JPanel();
                       panTgl = new JPanel();
                       panJabatan = new JPanel();
                       panCuti = new JPanel();
                        panStatus = new JPanel();
                     this.setLayout(new GridLayout(7, 1));
                     this.add(panTitle);
                     panTitle.add(lblTitle);
                     panTitle.add(txtTitle);
                    this.add(panNpm);
                     panNpm.add(lblNpm);
                      panNpm.add(txtNpm);
                     this.add(panYear);
                     panYear.add(lblYear);
                     panYear.add(txtYear);
                     this.add(panNanak);
                     panNanak.add(lblNanak);
                     panNanak.add(txtNanak);
                   this.add(panGol);
                   panGol.add(lblGol);
                   panGol.add(txtGol);
                   this.add(panPensiun);
                   panPensiun.add(lblPensiun);
                   panPensiun.add(txtPensiun);
                   this.add(panTgl);
                   panTgl.add(lblTgl);
                   panTgl.add(txtTgl);
                   this.add(panJabatan);
                   panJabatan.add(lblJabatan);
                   panJabatan.add(txtJabatan);
                   this.add(panCuti);
                   panCuti.add(lblCuti);
                   panCuti.add(txtCuti);
                   this.add(panStatus);
                     panStatus.add(cmdSave);
                     panStatus.add(lblStatus);
                     }
                     private int getYear () throws NumberFormatException
                     {int year = 0;
                     try
                     {year = Integer.parseInt(txtYear.getText());
                     }
                     catch (NumberFormatException ex)
                     {throw new NumberFormatException("Menulis tahun umur menggunakan angka contoh : 1987");
                     }
                     Calendar calNow = Calendar.getInstance();
                     int currentYear = calNow.get(Calendar.YEAR);
                     if (year < 1900 || year > currentYear)
                     {throw new NumberFormatException("Harus diantara 1900 and " + currentYear);
                     }
                     return year;
                     }

By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                            private void saveRecord (String strTitle, String strNpm, String strNanak,String strGol,String
                   strPensiun,String strTgl,String strJabatan,String strCuti,int year) throws SQLException
                            {Statement s = databaseConnection.createStatement();
                                      s.execute("insert into " + strTable +" values('" + strTitle +
                   ","+strNpm+","+strNanak+","+strGol+","+strPensiun+","+strTgl+","+strJabatan+","+strCuti+"', " + year +
                   ")");
                                      s.close();
                            }
                            public void actionPerformed (ActionEvent e)
                            { lblStatus.setText("");
                               String strTitle = txtTitle.getText() ;
                             String strNpm = txtNpm.getText();
                             String strNanak = txtNanak.getText();
                             String strGol = txtGol.getText();
                             String strPensiun = txtPensiun.getText();
                             String strTgl = txtTgl.getText();
                             String strJabatan = txtJabatan.getText();
                             String strCuti = txtCuti.getText();
                                      int year = 0;
                                      try
                                      {if      (strTitle.equals(""))
                                      {lblStatus.setText("Data namajangan kosong");
                                   }
                                   else if (strNpm.equals(""))
                                   { lblStatus.setText("npmnya jangan kosong");
                                   }
                                   else if (strNanak.equals(""))
                                   { lblStatus.setText("Nama anak Kosong");
                                   }
                                                  else
                                                  {year = getYear();
                            saveRecord(strTitle,strNpm,strNanak,strGol,strPensiun,strTgl,strJabatan,strCuti,year);
                                            txtTitle.setText("");
                                         txtNpm.setText("");
                                             txtYear.setText("");
                                         txtNanak.setText("");
                                         txtGol.setText("");
                                         txtPensiun.setText("");
                                         txtTgl.setText("");
                                         txtJabatan.setText("");
                                         txtCuti.setText("");
                                         txtTitle.requestFocus();
                                         txtNpm.requestFocus();
                                         txtNanak.requestFocus();
                                         txtGol.requestFocus();
                                         txtPensiun.requestFocus();
                                         txtTgl.requestFocus();
                                         txtJabatan.requestFocus();
                                         txtCuti.requestFocus();
                                                  lblStatus.setText("Data sukses tersimpan");
                                                  }}
                                      catch (NumberFormatException ex)
                                      {lblStatus.setText(ex.getMessage());
                                      }
                                      catch (SQLException ex)
                                      {lblStatus.setText("Error writing Record to table " + strTable);

By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                                      }}}

           10. table list
                            import java.sql.Connection;
                            import java.sql.ResultSet;
                            import java.sql.DatabaseMetaData;
                            import java.sql.SQLException;
                            import java.sql.Statement;
                            import java.util.Vector;
                            public class tableList
                            { private Vector vecTables = new Vector();
                                private Connection databaseConnection;
                                public tableList (Connection databaseConnection) throws SQLException
                                {         this.databaseConnection = databaseConnection;
                                          setupList();
                                }
                                public void setupList () throws SQLException
                                {         ResultSet rsList;
                                          DatabaseMetaData dmd = databaseConnection.getMetaData();
                                   rsList = dmd.getTables(null, null, "%", null);
                                   while (rsList.next())
                                   { if (rsList.getString(4).equalsIgnoreCase("TABLE"))
                                              {
                                                        vecTables.addElement(rsList.getString(3));
                                              }} }
                                 public void addTable (String strTableName) throws Exception
                              {
                                   try
                                   {          Statement s = databaseConnection.createStatement();
                                              s.execute("create table " + strTableName + " (Title varchar(40), Year integer)");
                                              s.close();
                                   vecTables.addElement(strTableName);
                                   }
                                   catch (SQLException ex)
                                   { throw new Exception("Error creating " + strTableName);
                                   } }
                                 public void deleteTable (String strTableName) throws Exception
                              { try
                                   {          Statement s = databaseConnection.createStatement();
                                              s.execute("drop table " + strTableName);
                                              s.close();
                                             vecTables.removeElement(strTableName);
                                   }
                                catch (SQLException ex)
                                {throw new Exception("Error deleting " + strTableName);
                                }}
                                public String[] getList ()
                                {String [] strTemp = new String[1];
                                          strTemp[0] = "---";
                                          if (vecTables.size() == 0)
                                   { return strTemp;
                                   }
                                          String[] strList = new String[vecTables.size()];
                                   for (int i = 0; i < vecTables.size(); i++)
                                   { strList[i] = (String)vecTables.elementAt(i);
                                   }

By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA



                          return strList;
                          }
                             public ResultSet getRecords (String strTableName) throws Exception
                          { try
                              { Statement s = databaseConnection.createStatement();
                                        s.execute("select * from " + strTableName);
                                        return s.getResultSet();
                                        }
                              catch (SQLException ex)
                              { throw new Exception("Error reading from " + strTableName);
                              }}}

           11. AbstractInputTabelDialog
                   import javax.swing.JOptionPane;
                   import javax.swing.JDialog;
                   import javax.swing.JFrame;
                   import javax.swing.JList;
                   import javax.swing.JTextField;
                   import javax.swing.JButton;
                   import javax.swing.JLabel;
                   import javax.swing.JScrollPane;
                   import javax.swing.JPanel;
                   import javax.swing.BorderFactory;
                   import javax.swing.event.ListSelectionListener;
                   import javax.swing.event.ListSelectionEvent;
                   import java.awt.Dimension;
                   import java.awt.BorderLayout;
                   import java.awt.event.ActionListener;
                   import java.awt.event.ActionEvent;
                   public class abstractInputTableDialogue extends JDialog implements ListSelectionListener, ActionListener
                   {        private JList lstTables;
                            private JScrollPane scrollPane;
                            protected JTextField txtTable;
                            protected JButton cmdClick, cmdCancel;
                            protected JLabel lblStatus;
                            private JPanel panRoot, panSouthArea, panSelect;
                            protected String strChosenTable = "";
                            private String[] strList;
                            protected String getChosenTable ()
                            {return strChosenTable;
                            }

                           public abstractInputTableDialogue (JFrame mainApp, String[] strList)
                           {super(mainApp, true);
                                    this.strList = strList;
                                    this.setSize(new Dimension(350, 500));
                                    this.setResizable(false);
                                    this.setLayout(new BorderLayout());
                                    lstTables = new JList();
                                    if (!strList[0].equalsIgnoreCase("---"))
                                    {          lstTables.setListData(strList);
                                               lstTables.addListSelectionListener(this);
                                    }



By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                                      scrollPane = new JScrollPane(lstTables,
                   JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                   JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                                      scrollPane.setPreferredSize(new Dimension(350, 400));
                                      txtTable = new JTextField("", 15);
                                      lblStatus = new JLabel("Pilih Tabel ");
                                      cmdClick = new JButton("");
                                      cmdCancel = new JButton("Cancel");
                                      cmdClick.addActionListener(this);
                                      cmdCancel.addActionListener(this);
                                      panRoot = new JPanel(new BorderLayout());
                                      panSouthArea = new JPanel(new BorderLayout());
                                      panSelect = new JPanel();
                                      this.add(panRoot, BorderLayout.CENTER);
                                                 panRoot.add(scrollPane, BorderLayout.NORTH);
                                                 panRoot.add(panSouthArea, BorderLayout.SOUTH);
                                                           panSouthArea.add(panSelect, BorderLayout.NORTH);
                                                                    panSelect.add(txtTable);
                                                                    panSelect.add(cmdClick);
                                                                    panSelect.add(cmdCancel);
                                                           panSouthArea.add(lblStatus, BorderLayout.SOUTH);
                            panRoot.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
                            txtTable.requestFocusInWindow();
                            }
                            public void valueChanged (ListSelectionEvent e)
                            {txtTable.setText((String)lstTables.getSelectedValue());
                            }
                            public void actionPerformed (ActionEvent e)
                            {}
                            protected boolean exists (String strTableName)
                            {for (int i = 0; i < strList.length; i++)
                            {if (strTableName.equalsIgnoreCase(strList[i]))
                            {return true;
                                                 }}
                                      return false;
                                      }}

           12. CreateTabel
                   import javax.swing.JFrame;
                   import java.awt.event.ActionEvent;
                   public class createTable extends abstractInputTableDialogue
                   {public createTable (JFrame mainApp, String[] strList)
                            {         super(mainApp, strList);
                                      this.setTitle("buat tabel baru");
                                      cmdClick.setText("buat");
                            }
                            public void actionPerformed (ActionEvent e)
                            {if (e.getSource() == cmdCancel)
                                      {this.setVisible(false);
                                      }
                                      else
                                      {         String strTableName = txtTable.getText();
                                                if (!strTableName.equalsIgnoreCase(""))
                                                {if (!exists(strTableName))
                                                                   {strChosenTable = strTableName;
                                                                   this.setVisible(false);

By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                                                                }
                                                       else
                                                       {lblStatus.setText("Table Sudah Ada");
                                                       }}}}}
           13. OutputFile
                   import java.sql.ResultSet;
                   import java.io.BufferedWriter;
                   import java.io.FileWriter;
                   import java.io.IOException;
                   import javax.swing.JOptionPane;
                   import java.util.Calendar;
                   import java.text.DecimalFormat;
                   public class outputFile
                   {public static void TXT (ResultSet rsList, String strFile) throws Exception
                   {strFile = removeExtension(strFile, ".txt");
                             try
                             {BufferedWriter createDocument = new BufferedWriter(new FileWriter(strFile + ".txt"));
                                       String strLine = "";
                                       int counter = 1;
                                       strLine = "Records from table: " + strFile;
                                       writeLine(strLine, createDocument);
                                       strLine = getTimeStamp();
                                       writeLine(strLine, createDocument);
                                       createDocument.newLine();
                                       while (rsList.next())
                             {strLine = "(" + counter + ") " + rsList.getString(1) + " [" + rsList.getInt(2) + "]";
                                                          writeLine(strLine, createDocument);
                                                          counter++;
                             }
                                       createDocument.close();
                                       JOptionPane.showMessageDialog(null, "Records written successfully to\n" +
                   System.getProperty("user.dir") + "\\" + strFile + ".txt", "Save",
                   JOptionPane.INFORMATION_MESSAGE);
                                       }
                                       catch (IOException ex)
                                       {
                                                 throw new Exception("Error writing file " + System.getProperty("user.dir") +
                   "\\" + strFile + ".txt\nPlease check file permissions");
                                       }}
                             public static void XML (ResultSet rsList, String strFile) throws Exception
                             {strFile = removeExtension(strFile, ".xml");
                             try
                             {BufferedWriter createDocument = new BufferedWriter(new FileWriter(strFile + ".xml"));
                             String strLine = "";
                             int counter = 1;
                             strLine = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
                             writeLine(strLine, createDocument);
                             createDocument.newLine();
                             strLine = "<!DOCTYPE conversions[";
                             writeLine(strLine, createDocument);
                             createDocument.newLine();
                             strLine = "\t<!ELEMENT " + strFile + " (title, year)>";
                             writeLine(strLine, createDocument);
                             strLine = "\t\t<!ELEMENT title (#PCDATA)>";
                             writeLine(strLine, createDocument);
                             strLine = "\t\t<!ELEMENT year (#PCDATA)>";

By : Dedy Wibowo
PROGRAM DATABASE KEPEGAWAIAN MENGGUNAKAN APLIKASI PROGRAM NET.BEANS JAVA


                             writeLine(strLine, createDocument);
                             createDocument.newLine();
                             strLine = "]>";
                             writeLine(strLine, createDocument);
                             createDocument.newLine();
                             strLine = "<!-- " + getTimeStamp() + " -->";
                             writeLine(strLine, createDocument);
                             createDocument.newLine();
                             strLine = "<" + strFile + ">";
                             writeLine(strLine, createDocument);
                             createDocument.newLine();
                             while (rsList.next())
                             {strLine = "\t<index>" + counter + "</index>";
                             writeLine(strLine, createDocument);
                             strLine = "\t<title>" + rsList.getString(1) + "</title>";
                             writeLine(strLine, createDocument);
                             strLine = "\t<year>" + rsList.getInt(2) + "</year>";
                             writeLine(strLine, createDocument);
                             createDocument.newLine();
                             counter++;
                             }
                             strLine = "</" + strFile + ">";
                             writeLine(strLine, createDocument);
                             createDocument.close();
                             JOptionPane.showMessageDialog(null, "Records written successfully to\n" +
                   System.getProperty("user.dir") + "\\" + strFile + ".xml", "Save",
                   JOptionPane.INFORMATION_MESSAGE);
                                                 }
                                       catch (IOException ex)
                                       {throw new Exception("Error writing file " + System.getProperty("user.dir") + "\\" +
                   strFile + ".xml\nPlease check file permissions");
                                       }}
                                       private static void writeLine (String strLine, BufferedWriter createDocument) throws
                   IOException
                             {createDocument.write(strLine, 0, strLine.length());
                                       createDocument.newLine();
                             }         private static String removeExtension (String strFile, String strExt)
                             {if (strFile.toLowerCase().endsWith(strExt.toLowerCase()))
                                       {strFile = strFile.substring(0, strFile.length() - 4);
                                       }
                                                 return strFile;
                                       }
                                       private static String getTimeStamp ()
                             {Calendar calNow = Calendar.getInstance();
                               DecimalFormat decFormat = new DecimalFormat("00");
                                       return "Last Updated " + decFormat.format(calNow.get(Calendar.DAY_OF_MONTH))
                   + "\\" + decFormat.format((calNow.get(Calendar.MONTH) + 1)) + "\\" + calNow.get(Calendar.YEAR) + "
                   at " + decFormat.format(calNow.get(Calendar.HOUR_OF_DAY)) + ":" +
                   decFormat.format(calNow.get(Calendar.MINUTE));
                                       }}




By : Dedy Wibowo

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:141
posted:7/27/2012
language:
pages:18
Description: application Java Netbeans