31 December 2015

Upload File pada JSP

Untuk melakukan upload file pada JSP maka yang harus diperhatikan yaitu harus melalukan import IO (Input Output Steam) terelbih dahulu. Terdapat 2 file yang akan digunakan untuk melakukan pengujian upload file, yaitu upload.jsp yang berperan sebagai eksekutor saat program upload dijalankan, dan v_upload.jsp yang berperan sebagai form upload dimana upload file akan dilakukan.

v_upload.jsp
<FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<br>
<br>
<br>
<center>
<table border="0" bgcolor=#ccFDDEE>
      <tr>
            <center>
            <td colspan="2" align="center"><b>UPLOAD THE FILE</b>
            <center>
            </td>
      </tr>
      <tr>
            <td colspan="2" align="center"></td>
      </tr>
      <tr>
            <td><b>File yang akan di upload :</b></td>
            <td><INPUT NAME="file" TYPE="file"></td>
      </tr>
      <tr>
            <td colspan="2" align="center"></td>
      </tr>
      <tr>
            <td colspan="2" align="center"><input type="submit"
                  value="Send File"></td>
      </tr>
<table>
</center>

</form>


upload.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
    <%
      String saveFile = "";
      String nmaFile = "";
      int TotnamaFile = 0;
      String contentType = request.getContentType();
      if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
            DataInputStream in = new DataInputStream(request.getInputStream());
            int formDataLength = request.getContentLength();
            byte dataBytes[] = new byte[formDataLength];
            int byteRead = 0;
            int totalBytesRead = 0;
            while (totalBytesRead < formDataLength) {
            byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
                  totalBytesRead += byteRead;
            }
            String file = new String(dataBytes);
            saveFile = file.substring(file.indexOf("filename=\"") + 10);
            saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
            saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, 
            saveFile.indexOf("\""));
            int lastIndex = contentType.lastIndexOf("=");
            String boundary = contentType.substring(lastIndex + 1,
            contentType.length());
            int pos;
            pos = file.indexOf("filename=\"");
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            int boundaryLocation = file.indexOf(boundary, pos) - 4;
            int startPos = ((file.substring(0, pos)).getBytes()).length;
            int endPos=((file.substring(0,boundaryLocation)).getBytes()).length;
            String data = "D:/IMPORTANTS FILES/NetBeans/Web2015/web/images/";
            saveFile = data + saveFile;
            TotnamaFile = saveFile.length();
            nmaFile=saveFile.substring(data.length(),TotnamaFile);
            File ff = new File(saveFile);
            FileOutputStream fileOut = new FileOutputStream(ff);
            fileOut.write(dataBytes, startPos, (endPos - startPos));
            fileOut.flush();
            fileOut.close();
        %>
<br>
<table border="2">
      <tr>
            <td><b>Berhasil di upload :</b>
            <%   out.println(saveFile); } %>
            <hr><img src="images/<% out.print(nmaFile); %>"><hr><%   out.print(TotnamaFile); %>
            <hr><%   out.print(nmaFile); %>
            </td>
      </tr></table>

    </body>
</html>



Menentukan Nilai Huruf dan Keterangan Kelulusan dengan Excel Macro

Pembahasan kali ini mengenai Excel Macro yaitu untuk mencari Nilai Huruf dan Kelulusan, terdapat daftar nilai mahasiswa seperti berikut ini :



Langkah berikutnya yaitu membuat pemrograman Macro nya dengan Visual Basic :


Object Form
Propertie
Keterangan
Command1

Name
Value
CmdNilaiHuruf
NILAI HURUF
Command2

Name
Value
CmdKeterangan
KETERANGAN

Coding :

Private Sub CmdKeterangan_Click()
    For i = 2 To 9
        If Cells(i, 5) = "A" Or Cells(i, 5) = "B" Or Cells(i, 5) = "C" Then
            Cells(i, 6).Font.ColorIndex = 5
            Cells(i, 6) = "LULUS"
        ElseIf Cells(i, 5) = "D" Then
            Cells(i, 6).Font.ColorIndex = 4
            Cells(i, 6) = "MENGULANG"
        Else
            Cells(i, 6).Font.ColorIndex = 3
            Cells(i, 6) = "GAGAL"
        End If
    Next i
End Sub

Private Sub CmdNilaiHuruf_Click()
    For i = 2 To 9
        If Cells(i, 4) > 90 Then
            Cells(i, 5) = "A"
        ElseIf Cells(i, 4) >= 80 Then
            Cells(i, 5) = "B"
        ElseIf Cells(i, 4) >= 70 Then
            Cells(i, 5) = "C"
        ElseIf Cells(i, 4) >= 60 Then
            Cells(i, 5) = "D"
        Else
            Cells(i, 5) = "E"
        End If
    Next i
End Sub


Keterangan :
Font.ColorIndex = 3 => Warna Merah
Font.ColorIndex = 4 => Warna Hijau
Font.ColorIndex = 5 => Warna Biru

Import file txt ke Database MySQL

File text yang akan di import menggunakan delimiter penghubung ; (titik koma), tampilannya seperti berikut ini :

10032;Sukarno, Mohamad;PHP & Jason Developer's Guide;Computer;57500;2015-03-18;An in-depth look at creating applications with PHP & Jason.

10033;Brain, Mathew;ASP .NET Developer's Guide;Computer;60500;2015-08-15;An in-depth look at creating applications with ASP .NET.

10034;Bro, Man;JSP Developer's Guide;Computer;60500;2015-04-24;An in-depth look at creating applications with JSP


dbkoneksi.php

<?php
$host = "localhost";

$user = "root";
$pass = "";
$dtbase = "blog";
$ceka = mysqli_connect($host,$user,$pass,$dtbase) or Die("Tidak terkoneksi ke server");

?>


importtxt.php


<form method='post' enctype='multipart/form-data'>
Silakan Pilih File hidden: <input name='userfile' type='file' class='form-control'><br>
<input name='upload' type='submit' value='Import' class='btn btn-danger'>
</form>
<form method='post'>
<?php
if(!empty($_POST['upload'])) {
$nm_file = $_FILES['userfile']['name'];
$tp_file = $_FILES['userfile']['tmp_name'];
$fp = fopen($nm_file, "r");
$i=1;
while ($line = fgets($fp)) { // Loop through each line
     list ($id, $author, $title, $genre, $price, $publish_date, $description) = split(";", $line, 7); // Split the line by ; delimiter and store it in our list
                 echo "$i. $id, $author, $title, $genre, $price, $publish_date, $description
                 <input type=hidden name=id$i value='$id'>
                 <input type=hidden name=author$i value='$author'>
                 <input type=hidden name=title$i value='$title'>
                 <input type=hidden name=genre$i value='$genre'>
                 <input type=hidden name=price$i value='$price'>
                 <input type=text name=publish_date$i value='$publish_date'>
                 <input type=hidden name=description$i value='$description'><br>";
                 $i++;
}
$n=100;
if($i<$n) { $maks = $i-1; }
echo "<input type=hidden name=total value=$maks><br><input type=submit name=proses value='PROSES AKHIR'>";

}
?>
</form>
<?php
include("dbkoneksi.php");
if(!empty($_POST['proses'])) {
for($i=1;$i<=$_POST['total'];$i++) {
$id[$i] = $_POST["id$i"];
$author[$i] = $_POST["author$i"];
$title[$i] = $_POST["title$i"];
$genre[$i] = $_POST["genre$i"];
$price[$i] = $_POST["price$i"];
$publish_date[$i] = $_POST["publish_date$i"];
$description[$i] = addslashes($_POST["description$i"]);
$sql = "insert into book (id,author,title,genre,price,publish_date,description)
values ('$id[$i]', '$author[$i]', '$title[$i]', '$genre[$i]', '$price[$i]', '$publish_date[$i]', '$description[$i]')"; // Generate our sql string
mysqli_query($ceka,$sql); // Execute the sql               
}
echo "<script>alert('Data berhasil di import !');</script>";
}
?>





30 December 2015

Menset PHP sebagai Shell Programming

Banyak programmer yang mengartikan Shell Script Programming sebagai sebuah pemrograman yang hasilnya akhirnya dijalankan di DOS Prompt. Pada lingkungan DOS sendiri juga sudah terdapat shell script yang dikenal dengan Batch. Adapun shell script yang terkenal adalah Perl.

Selain sebagai monster penghasil aplikasi-aplikasi berbasis web, ternyata PHP juga sangat handal jika disulap menjadi shell script programming. Kemampuan PHP Shell Script Programming dapat digunakan untuk membuat aplikasi pengirim email, socket programming, pengolahan data di database server.

Menset PHP sebagai Script programming :

1. Klik Start > Control Panel > System > Advanced system setting. Dan pada kotak dialog System Properties pilih tab Advanced dan klik tombol Environment Variable. Maka akan tampil kotak dialog Environment Variable seperti berikut ini :


2. Kemudian pilih Path pada System Variable dan klik tombol Edit maka akan tampil kotak dialog Edit System Variable. C:\xampp\php\;


Setelah menset PHP sebagai shell scripting, maka kita akan mencoba penerapannya. Buka Command Prompt, setelah itu kita masuk ke direktori yang sama saat kita setting Path, yaitu C:\xampp\php\. Caranya dengan memasukan teks >cd C:\xampp\php dan enter.


Berikut ini beberapa parameter perintah PHP di shell programming :
  • php –v                      :  berfungsi untuk menampilkan versi dari PHP yang di gunakan.
  • php namafile        :  berfungsi untuk menjalankan file php agar bias menjadi shell script.
  • php –w namafile  :  berfungsi untuk menampilkan source code dari file yang bersangkutan.
  • php –l namafile         :  berfungsi untuk mendeteksi kesalahan syntax pada sebuah file.
  • php –i namafile      :     berfungsi untuk menampilkan informasi seputar konfigurasi PHP di computer kita.
Contoh menampilkan versi PHP dan mengeksekusi file :

Contoo