bukuTamu2 - WordPress.com

advertisement
Koneksi.jsp
<%-Document : koneksi
Created on : Nov 21, 2013, 7:05:21 PM
Author
: umay
--%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
Connection conn=null;
try{
String connectionURL="jdbc:mysql://localhost/buku_tamu";
String username="root";
String password="root";
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(connectionURL,username,password);
}catch(Exception ex){
System.err.println("tidak berhasil koneksi "+ex.getMessage());
out.println("gagal "+ex.getMessage());
}
%>
Form.jsp
<%-Document : form
Created on : Nov 21, 2013, 7:07:30 PM
Author
: umay
--%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="koneksi.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Form Buku Tamu</title>
</head>
<%
String kode1 = "";
String nama1 = "";
String gender1 = "";
String alamat1 = "";
String id = request.getParameter("no");
Statement statement = conn.createStatement();
ResultSet hasil1 = statement.executeQuery("select *from dataTamu where no=" + id + "");
if (hasil1.next()) {
kode1 = hasil1.getString(1);
nama1 = hasil1.getString(2);
gender1 = hasil1.getString(3);
alamat1 = hasil1.getString(4);
}
%>
<body>
<form name="form1" method="post" action="aksi.jsp">
<table width="449" border="0" align="center">
<tr align="center">
<td colspan="10"><strong>Masukkan Data Anda</strong></td>
</tr>
<tr>
<td width="384"><input name="no" type="hidden" size="30" value="<%=kode1%>"></td>
</tr>
<tr>
<td width="55">Nama</td>
<td width="384"><input name="nama" type="text" size="30" value="<%=nama1%>" ></td>
</tr>
<tr>
<td width="55">Gender</td>
<td width="395"><input name="gender" type="radio" value="L">Laki-laki
<input name="gender" type="radio" value="P">Perempuan</td>
</tr>
<tr>
<td width="55">Alamat</td>
<td><textarea name="alamat" ><%=alamat1%></textarea></td>
</tr>
<tr>
<td><input name="Submit" type="submit" value="masuk"></td>
</tr>
</table>
<hr>
<table align="center" width="764" border="0">
<tr align="center">
<td colspan="10"><strong>Data Tamu</strong></td>
</tr>
<tr bgcolor="green">
<td height="27"><b>Id Tamu</b></td>
<td><b>Nama</b></td>
<td><b>Gender</b></td>
<td><b>Alamat</b></td>
</tr>
<%
Statement statement1 = conn.createStatement();
ResultSet hasil = statement1.executeQuery("select *from dataTamu");
while (hasil.next()) {
String kode = hasil.getString(1);
String nama = hasil.getString(2);
String gender = hasil.getString(3);
String alamat = hasil.getString(4);
%>
<tr bgcolor="F0F0F9">
<td><%=kode%></td>
<td><%=nama%></td>
<td><%=gender%></td>
<td><%=alamat%></td>
</tr>
<%
}
%>
</table>
</form>
</body>
</html>
Aksi.jsp
<%-Document : aksi
Created on : Nov 21, 2013, 7:20:15 PM
Author
: umay
--%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@include file="koneksi.jsp"%>
<%
String id = request.getParameter("no");
String nama = request.getParameter("nama");
String gender = request.getParameter("gender");
String alamat = request.getParameter("alamat");
if (request.getParameter("Submit").equals("masuk")) {
if (nama != "" && gender != null) {
String sql = "insert into dataTamu(nama,gender,alamat)"
+ "value('" + nama + "','" + gender + "','" + alamat + "');";
Statement statement = conn.createStatement();
statement.executeUpdate(sql);
if (statement != null) {
response.sendRedirect("awal.jsp?page=form");
out.print("sukses");
}
}
}
%>
Download