Kemarin ada temanku yang menanyakan soal JSP dengan koneksi oracle, disini saya akan mencoba membantu dengan memberikan contoh membuat halaman login dengan menggunakan JSP dengan koneksi database ORACLE. Moga-moga bisa sedikit membantu ya...!! ^_^
index.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%if(session.getValue("usid")==null) {%>
<%@include file="halaman_utama.jsp"%>
<%}else{%>
<%response.sendRedirect("admin_area.jsp");%>
<%}%>
halaman_utama.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%if(session.getValue("usid")==null) {%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Halaman Utama</title>
</head>
<body>
<h1>HALAMAN UTAMA</h1>
<a href="halaman_masuk.jsp">Masuk</a>
</body>
</html>
<%}else{%>
<%response.sendRedirect("admin_area.jsp");%>
<%}%>
koneksi.jsp
<%
String xDRIVER = "oracle.jdbc.driver.OracleDriver";
String xUSERNAME = "agustianra";
String xPASSWORD = "bulongx1";
String xSTRING = "jdbc:oracle:thin:@localhost:1521:XE";
Class.forName(xDRIVER);
%>
halaman_masuk.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%if(session.getValue("usid")==null) {%>
<%@include file="koneksi.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Halaman Masuk</title>
</head>
<body>
<h1>Halaman Masuk</h1>
<form action="<%=LA%>">
<input type="text" name="username" id="username">
<input type="password" name="password" id="password">
<input type="submit" value="Masuk" id="masuk">
<input type="reset" value="Batal">
</form>
<%!String LA="";%>
<%
String LoginAction = request.getRequestURI();
LA = LoginAction;
if (request.getQueryString() != null && request.getQueryString().length() > 0) {
LoginAction += "?" + request.getQueryString();
String valUsername=request.getParameter("username");
if (valUsername != null) {
String fldUserAuthorization="";
String redirectLoginSuccess="admin_area.jsp";
String redirectLoginFailed="halaman_masuk.jsp";
String redirectLogin=redirectLoginFailed;
Connection connUser = DriverManager.getConnection(xSTRING, xUSERNAME, xPASSWORD);
String pSQL = "SELECT username, password FROM jsp2cms_pengguna where username='"+valUsername+"'";
PreparedStatement statementUser = connUser.prepareStatement(pSQL);
ResultSet rsUser = statementUser.executeQuery();
boolean rsUser_isNotEmpty = rsUser.next();
if (rsUser_isNotEmpty) {
session.putValue("usid", valUsername);
if(request.getParameter("password").toString().equals(rsUser.getString("Password").toString())) {
redirectLogin=redirectLoginSuccess;
} else {
redirectLogin=redirectLoginFailed;
}
} else {
redirectLogin=redirectLoginFailed;
}
response.sendRedirect(response.encodeRedirectURL(redirectLogin));
return;
}
}
%>
</body>
</html>
<%}else{%>
<%response.sendRedirect("admin_area.jsp");%>
<%}%>
admin_area.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
if(session.getValue("usid")==null) {
response.sendRedirect("halaman_masuk.jsp");
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Halaman Admin</title>
</head>
<body>
<h1>HALAMAN ADMIN</h1>
<a href="keluar.jsp">Keluar</a>
</body>
</html>
keluar.jsp
<%
session.removeValue("usid");
%>
<%response.sendRedirect("halaman_utama.jsp");%>
Oracle SQL
create table jsp2cms_pengguna(id_pengguna int primary key not null, pengguna varchar(32) unique, sandi varchar(32));
insert into jsp2cms_pengguna values(0, 'admin', 'admin');
DOWNLOAD JDBC DRIVER ORACLE
1 komentar:
kalau konek dengan sql server apa saja yang harus diganti ?
Posting Komentar