Commit 962434e3 authored by Dwinowo Muhammad's avatar Dwinowo Muhammad

Penambahan fitur CRUD

parent d6717055
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0">
<attributes>
<attribute name="owner.project.facets" value="jst.web"/>
......@@ -9,5 +13,10 @@
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="lib" path="C:/Users/admin/Documents/mysql-connector-java-8.0.28.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="build/classes"/>
</classpath>
package com.kampus;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class AksesLogin
*/
@WebServlet("/AksesLogin")
public class AksesLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AksesLogin() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/Login/Login.jsp").include(request, response);
HttpSession session = request.getSession(false);
if(session!=null) {
String name=(String)session.getAttribute("nama");
//kasih alert Anda login sebagai
}else {
//kasih alert login first
request.getRequestDispatcher("/Login/Login.jsp").include(request, response);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.kampus;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class ArrayMahasiswa
*/
@WebServlet("/ArrayMahasiswa")
public class ArrayMahasiswa extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ArrayMahasiswa() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/Login/Login.jsp").include(request, response);
HttpSession session = request.getSession(false);
if (session != null) {
try {
Connection con = DatabaseConnection.initializeDatabase();
Statement stmt = null;
stmt = (Statement) con.createStatement();
String query = "SELECT * FROM tbl_mahasiswa";
ResultSet rs = stmt.executeQuery(query);
List<Mahasiswa> listMahasiswa = new ArrayList<Mahasiswa>();
while (rs.next()) {
Mahasiswa mahasiswa = new Mahasiswa();
mahasiswa.setId(rs.getInt("id"));
mahasiswa.setNama(rs.getString("nama"));
mahasiswa.setNamaPanggilan(rs.getString("nama_panggilan"));
mahasiswa.setTempatLahir(rs.getString("tempat_lahir"));
mahasiswa.setTanggalLahir(rs.getDate("tanggal_lahir"));
mahasiswa.setJenisKelamin(rs.getString("jenis_kelamin"));
mahasiswa.setHobi(rs.getString("hobi"));
mahasiswa.setEmail(rs.getString("email"));
mahasiswa.setAnakKe(rs.getInt("anak_ke"));
mahasiswa.setPendidikanTerakhir(rs.getString("pendidikan_terakhir"));
mahasiswa.setAlamat(rs.getString("alamat"));
listMahasiswa.add(mahasiswa);
}
request.setAttribute("listMahasiswa", listMahasiswa);
request.getRequestDispatcher("Dashboard.jsp").forward(request, response);
// request.getRequestDispatcher("edit.jsp").forward(request, response);
// Create a sql query to insert data into demo table
// demo table consists of two columns, so two '?' is used
response.getWriter().append("Served at: ").append(request.getContextPath());
} catch (SQLException | ClassNotFoundException e) {
// TODO: handle exception
}
// kasih alert Anda login sebagai
} else {
// kasih alert login first
request.getRequestDispatcher("/Login/Login.jsp").include(request, response);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.kampus;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
protected static Connection initializeDatabase() throws ClassNotFoundException, SQLException {
// Initialize all the information regarding
// database connection
String dbDriver = "com.mysql.jdbc.Driver";
String dbURL = "jdbc:mysql://localhost/";
// Databse name to access
String dbName = "kampus_moscow";
String dbUsername = "root";
String dbPassword = "admin";
Class.forName(dbDriver);
Connection con = DriverManager.getConnection(dbURL + dbName + "?autoReconnect=true&useSSL=false", dbUsername,
dbPassword);
return con;
}
}
package com.kampus;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Delete
*/
@WebServlet("/Delete")
public class Delete extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Delete() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Connection con = DatabaseConnection.initializeDatabase();
// EDIT dibawah ==================================================
String sqlDelete = "DELETE FROM tbl_mahasiswa WHERE id = ?";
int idBaru = Integer.parseInt(request.getParameter("idBaru"));
PreparedStatement statementDelete = con.prepareStatement(sqlDelete);
statementDelete.setInt(1, idBaru);
statementDelete.executeUpdate();
statementDelete.close();
con.close();
response.sendRedirect("/tugasAkhir_DwinowoMuhammad/ArrayMahasiswa");
} catch (Exception e) {
// TODO: handle exception
}
doGet(request, response);
}
}
package com.kampus;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Edit
*/
@WebServlet("/Edit")
public class Edit extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Edit() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tanggalLahir = request.getParameter("tanggalLahir");
Date tanggalLahirNew = java.sql.Date.valueOf(tanggalLahir);
int anakKe = Integer.parseInt(request.getParameter("anakKe"));
String futsal = request.getParameter("futsal");
String badminton = request.getParameter("badminton");
String membaca = request.getParameter("membacaBuku");
String renang = request.getParameter("berenang");
String lainnya = request.getParameter("lainnya");
String hobi = "";
try {
if (futsal.equals("Futsal")) {
hobi = hobi + futsal + ", ";
}
} catch (Exception e) {
}
try {
if (badminton.equals("Badminton")) {
hobi = hobi + badminton + ", ";
}
} catch (Exception e) {
}
try {
if (membaca.equals("Membaca Buku")) {
hobi = hobi + membaca + ", ";
}
} catch (Exception e) {
}
try {
if (renang.equals("Berenang")) {
hobi = hobi + renang + ", ";
}
} catch (Exception e) {
}
try {
if (lainnya.equals("Lainnya")) {
hobi = hobi + lainnya + ", ";
}
} catch (Exception e) {
}
String hobiNew = hobi.substring(0, hobi.length()-2);
//======================= update data di bawah
try {
Connection con = DatabaseConnection.initializeDatabase();
// EDIT dibawah ==================================================
String sqlEdit = "UPDATE tbl_mahasiswa SET nama = ?, nama_panggilan = ?, tempat_lahir = ?, tanggal_lahir = ?, jenis_kelamin = ?, hobi = ?, email = ?, anak_ke = ?, pendidikan_terakhir = ?, alamat = ? WHERE id = ?";
int idBaru = Integer.parseInt(request.getParameter("idBaru"));
PreparedStatement statementEdit = con.prepareStatement(sqlEdit);
statementEdit.setString(1, request.getParameter("nama"));
statementEdit.setString(2, request.getParameter("namaPanggilan"));
statementEdit.setString(3, request.getParameter("tempatLahir"));
statementEdit.setDate(4, (java.sql.Date) tanggalLahirNew);
statementEdit.setString(5, request.getParameter("jenisKelamin"));
statementEdit.setString(6, hobiNew);
statementEdit.setString(7, request.getParameter("email"));
statementEdit.setInt(8, anakKe);
statementEdit.setString(9, request.getParameter("pendidikanTerakhir"));
statementEdit.setString(10, request.getParameter("alamat"));
statementEdit.setInt(11, idBaru);
statementEdit.executeUpdate();
statementEdit.close();
con.close();
response.sendRedirect("/tugasAkhir_DwinowoMuhammad/ArrayMahasiswa");
} catch (Exception e) {
// TODO: handle exception
}
doGet(request, response);
}
}
package com.kampus;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class Logout
*/
@WebServlet("/Logout")
public class Logout extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Logout() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/Login/Login.jsp").include(request, response);
HttpSession session = request.getSession();
session.invalidate();
//kasih alert sukses keluar
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.kampus;
import java.sql.Date;
public class Mahasiswa {
protected int id;
protected String nama;
protected String namaPanggilan;
protected String tempatLahir;
protected Date tanggalLahir;
protected String jenisKelamin;
protected String hobi;
protected String email;
protected int anakKe;
protected String pendidikanTerakhir;
protected String alamat;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public String getNamaPanggilan() {
return namaPanggilan;
}
public void setNamaPanggilan(String namaPanggilan) {
this.namaPanggilan = namaPanggilan;
}
public String getTempatLahir() {
return tempatLahir;
}
public void setTempatLahir(String tempatLahir) {
this.tempatLahir = tempatLahir;
}
public Date getTanggalLahir() {
return tanggalLahir;
}
public void setTanggalLahir(Date date) {
this.tanggalLahir = date;
}
public String getJenisKelamin() {
return jenisKelamin;
}
public void setJenisKelamin(String jenisKelamin) {
this.jenisKelamin = jenisKelamin;
}
public String getHobi() {
return hobi;
}
public void setHobi(String hobi) {
this.hobi = hobi;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAnakKe() {
return anakKe;
}
public void setAnakKe(int anakKe) {
this.anakKe = anakKe;
}
public String getPendidikanTerakhir() {
return pendidikanTerakhir;
}
public void setPendidikanTerakhir(String pendidikanTerakhir) {
this.pendidikanTerakhir = pendidikanTerakhir;
}
public String getAlamat() {
return alamat;
}
public void setAlamat(String alamat) {
this.alamat = alamat;
}
}
package com.kampus;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class ProsesTambah
*/
@WebServlet("/ProsesTambah")
public class ProsesTambah extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ProsesTambah() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tanggalLahir = request.getParameter("tanggalLahir");
Date tanggalLahirNew = java.sql.Date.valueOf(tanggalLahir);
String futsal = request.getParameter("futsal");
String badminton = request.getParameter("badminton");
String membaca = request.getParameter("membacaBuku");
String renang = request.getParameter("berenang");
String lainnya = request.getParameter("lainnya");
int anakKe = Integer.parseInt(request.getParameter("anakKe"));
String hobi = "";
try {
if (futsal.equals("Futsal")) {
hobi = hobi + futsal + ", ";
}
} catch (Exception e) {
}
try {
if (badminton.equals("Badminton")) {
hobi = hobi + badminton + ", ";
}
} catch (Exception e) {
}
try {
if (membaca.equals("Membaca Buku")) {
hobi = hobi + membaca + ", ";
}
} catch (Exception e) {
}
try {
if (renang.equals("Berenang")) {
hobi = hobi + renang + ", ";
}
} catch (Exception e) {
}
try {
if (lainnya.equals("Lainnya")) {
hobi = hobi + lainnya + ", ";
}
} catch (Exception e) {
}
String hobiNew = hobi.substring(0, hobi.length()-2);
// proses tambah DATA =============================
try {
//Initialize the database
Connection con = DatabaseConnection.initializeDatabase();
//create sql query insert data to table
PreparedStatement st = con.prepareStatement("INSERT INTO tbl_mahasiswa (nama, nama_panggilan, tempat_lahir, tanggal_lahir, jenis_kelamin, hobi, email, anak_ke, pendidikan_terakhir, alamat) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
// for the first parameter, amgka 1 itu urutan tanda tanya diatas
st.setString(1, request.getParameter("nama"));
st.setString(2, request.getParameter("namaPanggilan"));
st.setString(3, request.getParameter("tempatLahir"));
st.setDate(4, (java.sql.Date) tanggalLahirNew);
st.setString(5, request.getParameter("jenisKelamin"));
st.setString(6, hobiNew);
st.setString(7, request.getParameter("email"));
st.setInt(8, anakKe);
st.setString(9, request.getParameter("pendidikanTerakhir"));
st.setString(10, request.getParameter("alamat"));
// Execute the insert command using executeUpdate()
st.executeUpdate();
//close all connection
st.close();
con.close();
response.sendRedirect("/tugasAkhir_DwinowoMuhammad/ArrayMahasiswa");
} catch (Exception e) {
// TODO: handle exception
}
doGet(request, response);
}
}
package com.kampus;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.kampus.Mahasiswa;
import com.kampus.DatabaseConnection;
/**
* Servlet implementation class SelectedDelete
*/
@WebServlet("/SelectedDelete")
public class SelectedDelete extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SelectedDelete() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Connection con = DatabaseConnection.initializeDatabase();
Mahasiswa mahasiswa = new Mahasiswa();
String sql = "SELECT * FROM tbl_mahasiswa WHERE id = ?";
PreparedStatement statement = con.prepareStatement(sql);
statement.setInt(1, Integer.valueOf(request.getParameter("mahasiswaObjek")));
ResultSet rs = statement.executeQuery();
if (rs.next()) {
int id = rs.getInt("id");
String nama = rs.getString("nama");
String namaPanggilan = rs.getString("nama_panggilan");
String tempatLahir = rs.getString("tempat_lahir");
Date tanggalLahir = rs.getDate("tanggal_lahir");
String jenisKelamin = rs.getString("jenis_kelamin");
String hobi = rs.getString("hobi");
String email = rs.getString("email");
int anakKe = rs.getInt("anak_ke");
String pendidikanTerakhir = rs.getString("pendidikan_terakhir");
String alamat = rs.getString("alamat");
mahasiswa.setId(id);
mahasiswa.setNama(nama);
mahasiswa.setNamaPanggilan(namaPanggilan);
mahasiswa.setTempatLahir(tempatLahir);
mahasiswa.setTanggalLahir(tanggalLahir);
mahasiswa.setJenisKelamin(jenisKelamin);
mahasiswa.setHobi(hobi);
mahasiswa.setEmail(email);
mahasiswa.setAnakKe(anakKe);
mahasiswa.setPendidikanTerakhir(pendidikanTerakhir);
mahasiswa.setAlamat(alamat);
}
request.setAttribute("mahasiswaSelected", mahasiswa);
rs.close();
statement.close();
request.getRequestDispatcher("Delete.jsp").forward(request, response);
response.sendRedirect("/tugasAkhir_DwinowoMuhammad/Delete.jsp");
} catch (Exception e) {
// TODO: handle exception
}
doGet(request, response);
}
}
package com.kampus;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SelectedEdit
*/
@WebServlet("/SelectedEdit")
public class SelectedEdit extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SelectedEdit() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Connection con = DatabaseConnection.initializeDatabase();
Mahasiswa mahasiswa = new Mahasiswa();
String sql = "SELECT * FROM tbl_mahasiswa WHERE id = ?";
PreparedStatement statement = con.prepareStatement(sql);
statement.setInt(1, Integer.valueOf(request.getParameter("mahasiswaObjek")));
ResultSet rs = statement.executeQuery();
if (rs.next()) {
int id = rs.getInt("id");
String nama = rs.getString("nama");
String namaPanggilan = rs.getString("nama_panggilan");
String tempatLahir = rs.getString("tempat_lahir");
Date tanggalLahir = rs.getDate("tanggal_lahir");
String jenisKelamin = rs.getString("jenis_kelamin");
String hobi = rs.getString("hobi");
String email = rs.getString("email");
int anakKe = rs.getInt("anak_ke");
String pendidikanTerakhir = rs.getString("pendidikan_terakhir");
String alamat = rs.getString("alamat");
mahasiswa.setId(id);
mahasiswa.setNama(nama);
mahasiswa.setNamaPanggilan(namaPanggilan);
mahasiswa.setTempatLahir(tempatLahir);
mahasiswa.setTanggalLahir(tanggalLahir);
mahasiswa.setJenisKelamin(jenisKelamin);
mahasiswa.setHobi(hobi);
mahasiswa.setEmail(email);
mahasiswa.setAnakKe(anakKe);
mahasiswa.setPendidikanTerakhir(pendidikanTerakhir);
mahasiswa.setAlamat(alamat);
}
request.setAttribute("mahasiswaSelected", mahasiswa);
rs.close();
statement.close();
request.getRequestDispatcher("Edit.jsp").forward(request, response);
response.sendRedirect("/tugasAkhir_DwinowoMuhammad/Edit.jsp");
} catch (Exception e) {
// TODO: handle exception
}
doGet(request, response);
}
}
package com.kampus;
public class Users {
protected int id;
protected String nama;
protected String email;
protected String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.kampus;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class login
*/
@WebServlet("/login")
public class login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public login() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String emailSignIn = request.getParameter("email").toString();
String passwordSignIn = request.getParameter("password").toString();
Users pegawaiKampus = new Users();
Connection con = DatabaseConnection.initializeDatabase();
Statement stmt = null;
stmt = (Statement) con.createStatement();
String sql = "SELECT * FROM tbl_user WHERE email='" + emailSignIn + "' and password='" + passwordSignIn
+ "'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
pegawaiKampus.setId(rs.getInt("id"));
pegawaiKampus.setNama(rs.getString("nama"));
pegawaiKampus.setEmail(rs.getString("email"));
pegawaiKampus.setPassword(rs.getString("password"));
}
if (pegawaiKampus.getEmail().equals(emailSignIn)) {
System.out.println("Login Success");
HttpSession session = request.getSession();
session.setAttribute("nama", pegawaiKampus.getNama());
session.setAttribute("email", pegawaiKampus.getEmail());
// request.getRequestDispatcher("Dashboard.jsp").forward(request, response);
//
// response.sendRedirect("/tugasAkhir_DwinowoMuhammad/Dashboard.jsp");
response.sendRedirect("/tugasAkhir_DwinowoMuhammad/ArrayMahasiswa");
} else {
request.getRequestDispatcher("/Login/Login.jsp").include(request, response);
// kasih alert pesan
}
} catch (Exception e) {
// TODO: handle exception
}
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.List"%>
<%@ page import="com.kampus.Mahasiswa"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>INI UDAH MASUK ${nama} dan ${email}</h1>
<form action="TambahData.jsp" method="post">
<input type="submit" value="Tambah Data" class="tombolTambah">
</form>
<form action="/tugasAkhir_DwinowoMuhammad/Logout" method="post">
<input type="submit" value="Logout" class="tombolTambah">
</form>
<table border="2">
<tr>
<th>No</th>
<th>Nama</th>
<th>Nama Panggilan</th>
<th>Tempat Lahir</th>
<th>Tanggal Lahir</th>
<th>Jenis Kelamin</th>
<th>Hobi</th>
<th>Email</th>
<th>Anak Ke</th>
<th>Pendidikan Terakhir</th>
<th>Alamat</th>
<th>Action</th>
</tr>
<%
List<Mahasiswa> listMahasiswa = (ArrayList<Mahasiswa>) request.getAttribute("listMahasiswa");
for (int i = 0, j = 1; i < listMahasiswa.size(); i++, j++) {
%>
<tr>
<td><%=j%></td>
<td><%=listMahasiswa.get(i).getNama()%></td>
<td><%=listMahasiswa.get(i).getNamaPanggilan()%></td>
<td><%=listMahasiswa.get(i).getTempatLahir()%></td>
<td><%=listMahasiswa.get(i).getTanggalLahir()%></td>
<td><%=listMahasiswa.get(i).getJenisKelamin()%></td>
<td><%=listMahasiswa.get(i).getHobi()%></td>
<td><%=listMahasiswa.get(i).getEmail()%></td>
<td><%=listMahasiswa.get(i).getAnakKe()%></td>
<td><%=listMahasiswa.get(i).getPendidikanTerakhir()%></td>
<td><%=listMahasiswa.get(i).getAlamat()%></td>
<td>
<form action="/tugasAkhir_DwinowoMuhammad/SelectedEdit" method="post">
<input type="hidden" name="mahasiswaObjek"
value="<%=listMahasiswa.get(i).getId()%>"> <input type="submit"
value="Edit">
</form>
<form action="/tugasAkhir_DwinowoMuhammad/SelectedDelete" method="post">
<input type="hidden" name="mahasiswaObjek"
value="<%=listMahasiswa.get(i).getId()%>"> <input type="submit"
value="Hapus" class="tombolDelete">
</form>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.kampus.Mahasiswa"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Mahasiswa mahasiswa = (Mahasiswa) request.getAttribute("mahasiswaSelected");
%>
Apakah anda yakin untuk menghapus data mahasiswa dengan nama "<%=mahasiswa.getNama()%>"? <br/>
<form action="Delete" method="post">
<input type="hidden"
name="idBaru" value="<%=mahasiswa.getId()%>">
<input
type="submit" value="Ya">
</form>
<form action="ArrayMahasiswa">
<input type="submit" value="Tidak"></form>
</body>
</html>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.kampus.Mahasiswa"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Mahasiswa mahasiswa = (Mahasiswa) request.getAttribute("mahasiswaSelected");
%>
<form action="/tugasAkhir_DwinowoMuhammad/Edit" method="post">
<label>Nama : </label>
<input type="text" name="nama" value="<%=mahasiswa.getNama()%>"><br/>
<label>Nama Panggilan : </label>
<input type="text" name="namaPanggilan" value="<%=mahasiswa.getNamaPanggilan()%>"><br/>
<label>Tempat Lahir : </label>
<input type="text" name="tempatLahir" value="<%=mahasiswa.getTempatLahir()%>"><br/>
<label>Tanggal Lahir : </label>
<input type="date" name="tanggalLahir" value="<%=mahasiswa.getTanggalLahir()%>"><br/>
<%! String boy; %>
<%! String girl; %>
<%
if(mahasiswa.getJenisKelamin().equals("Laki-laki")){
boy = "selected";
}else if(mahasiswa.getJenisKelamin().equals("Perempuan")){
girl = "selected";
}
%>
<label>Jenis Kelamin : </label>
<select id="jenisKelamin" name="jenisKelamin"">
<option value="Laki-laki" <%=boy%>>Laki - laki</option>
<option value="Perempuan" <%=girl%>>Perempuan</option>
</select><br/>
<%! String renang; %>
<%! String membaca; %>
<%! String futsal; %>
<%! String badminton; %>
<%! String lainnya; %>
<%
String [] arrHobi = mahasiswa.getHobi().split(",");
for (int i = 0; i < arrHobi.length; i++) {
if(arrHobi[i].trim().equals("Berenang")){
renang = "checked";
}else if(arrHobi[i].trim().equals("Membaca Buku")){
membaca = "checked";
}else if(arrHobi[i].trim().equals("Futsal")){
futsal = "checked";
}else if(arrHobi[i].trim().equals("Badminton")){
badminton = "checked";
}else if(arrHobi[i].trim().equals("Lainnya")){
lainnya = "checked";
}
}
%>
<label>Hobi : </label>
<input type="checkbox" name="berenang" id="berenang" value="Berenang" <%=renang%>>
<label for="berenang">Berenang</label>
<input type="checkbox" name="membacaBuku" id="membacaBuku" value="Membaca Buku" <%=membaca%>>
<label for="membacaBuku">Membaca Buku</label>
<input type="checkbox" name="futsal" id="futsal" value="Futsal" <%=futsal%>>
<label for="futsal">Futsal</label>
<input type="checkbox" name="badminton" id="badminton" value="Badminton" <%=badminton%>>
<label for="badminton">Badminton</label>
<input type="checkbox" name="lainnya" id="lainnya" value="Lainnya" <%=lainnya%>>
<label for="lainnya">Lainnya</label>
<br/>
<label>Email : </label>
<input type="email" name="email" value="<%=mahasiswa.getEmail()%>"><br/>
<label>Anak-Ke : </label>
<input type="text" name="anakKe" value="<%=mahasiswa.getAnakKe()%>"><br/>
<%! String smp; %>
<%! String smaDkk; %>
<%! String s1; %>
<%! String s2; %>
<%! String s3; %>
<% if(mahasiswa.getPendidikanTerakhir().equals("SMP")) {
smp = "checked";
}else if(mahasiswa.getPendidikanTerakhir().equals("SMA/SMK/Sederajat")){
smaDkk = "checked";
}else if(mahasiswa.getPendidikanTerakhir().equals("Strata 1")){
s1 = "checked";
}else if(mahasiswa.getPendidikanTerakhir().equals("Strata 2")){
s2 = "checked";
}else if(mahasiswa.getPendidikanTerakhir().equals("Strata 3")){
s3 = "checked";
}
%>
<label>Pendidikan Terakhir : </label>
<input type="radio" id="smp" name="pendidikanTerakhir" value="SMP" <%=smp%>>
<label for="smp">SMP</label>
<input type="radio" id="smaSederajat" name="pendidikanTerakhir" value="SMA/SMK/Sederajat" <%=smaDkk%>>
<label for="smaSederajat">SMA/SMK/Sederajat</label>
<input type="radio" id="strata1" name="pendidikanTerakhir" value="Strata 1" <%=s1%>>
<label for="strata1">Strata 1</label>
<input type="radio" id="strata2" name="pendidikanTerakhir" value="Strata 2" <%=s2%>>
<label for="strata2">Strata 2</label>
<input type="radio" id="strata3" name="pendidikanTerakhir" value="Strata 3" <%=s3%>>
<label for="strata3">Strata 3</label>
<br/>
<label>Alamat : </label>
<input type="text" name="alamat" value="<%=mahasiswa.getAlamat()%>">
<br/>
<input type="hidden" name="idBaru" value="<%=mahasiswa.getId()%>">
<input type="submit" value="Submit">
</form>
</body>
</html>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Halaman Login</title>
</head>
<body>
<form action="/tugasAkhir_DwinowoMuhammad/login" method="post">
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br>
<br> <input type="submit" value="Sign in">
</form>
<form action="sxxxxxxxxxxxxxxxx" method="post">
<input type="submit" value="Sign up">
</form>
</body>
</html>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Register Admin</title>
</head>
<body>
<form action="" method="post">
<label>ID</label>
<input type="text" name="idBaru" id="idBaru" required="required"><br/>
<label>Nama</label>
<input type="text" name="namaBaru" id="namaBaru" required="required"><br/>
<label>Email</label>
<input type="email" name="emailBaru" id="emailBaru" required="required"><br/>
<label>Password</label>
<input type="password" name="passwordBaru" id="passwordBaru" required="required"><br/>
<label>Password Konfirmasi</label>
<input type="password" name="passwordBaruKonfrm" id="passwordBaruKonfrm" required="required">
<input type="submit" value="Register">
</form>
<script type="text/javascript">
var password = document.getElementById("passwordBaru")
, password_konfirmasi = document.getElementById("passwordBaruKonfrm");
function validatePassword(){
if(password.value != password_konfirmasi.value) {
password_konfirmasi.setCustomValidity("Passwords Tidak Sama");
} else {
password_konfirmasi.setCustomValidity('');
}
}
password.onchange = validatePassword;
konfirmasi_password.onkeyup = validatePassword;
</script>
</body>
</html>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Tambah Data Mahasiswa</title>
</head>
<body>
<form action="/tugasAkhir_DwinowoMuhammad/ProsesTambah" method="post">
<label>Nama : </label>
<input type="text" name="nama"><br/>
<label>Nama Panggilan : </label>
<input type="text" name="namaPanggilan"><br/>
<label>Tempat Lahir : </label>
<input type="text" name="tempatLahir"><br/>
<label>Tanggal Lahir : </label>
<input type="date" name="tanggalLahir"><br/>
<label>Jenis Kelamin : </label>
<select id="jenisKelamin" name="jenisKelamin">
<option value="Laki-laki">Laki - laki</option>
<option value="Perempuan">Perempuan</option>
</select><br/>
<label>Hobi : </label>
<input type="checkbox" name="berenang" id="berenang" value="Berenang">
<label for="berenang">Berenang</label>
<input type="checkbox" name="membacaBuku" id="membacaBuku" value="Membaca Buku">
<label for="membacaBuku">Membaca Buku</label>
<input type="checkbox" name="futsal" id="futsal" value="Futsal">
<label for="futsal">Futsal</label>
<input type="checkbox" name="badminton" id="badminton" value="Badminton">
<label for="badminton">Badminton</label>
<input type="checkbox" name="lainnya" id="lainnya" value="Lainnya">
<label for="lainnya">Lainnya</label>
<br/>
<label>Email : </label>
<input type="email" name="email"><br/>
<label>Anak-Ke : </label>
<input type="text" name="anakKe"><br/>
<label>Pendidikan Terakhir : </label>
<input type="radio" id="smp" name="pendidikanTerakhir" value="SMP">
<label for="smp">SMP</label>
<input type="radio" id="smaSederajat" name="pendidikanTerakhir" value="SMA/SMK/Sederajat">
<label for="smaSederajat">SMA/SMK/Sederajat</label>
<input type="radio" id="strata1" name="pendidikanTerakhir" value="Strata 1" >
<label for="strata1">Strata 1</label>
<input type="radio" id="strata2" name="pendidikanTerakhir" value="Strata 2">
<label for="strata2">Strata 2</label>
<input type="radio" id="strata3" name="pendidikanTerakhir" value="Strata 3">
<label for="strata3">Strata 3</label>
<br/>
<label>Alamat : </label>
<input type="text" name="alamat">
<br/>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>SendDirect BERHASIL</h1>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment