ORACLE+Myeclipse+jsp實現簡單登錄功能
Oracle數據庫表的建立:
create table Log(
un char(20),
pd char(25)
)
tablespace users;
Oracle在Myeclipse中的配置:
1.導jar包:
右鍵項目名,build path→configure build path→Libraries→Add External JARs..→D:\app\asus\product\11.2.0\dbhome_1\jdbc\lib(也就是你的Oracle安裝路徑\product......)→選擇ojdbc*.jar(*為自己文件裏的jar包版本號)→OK
就可以看到項目裏jar包導入,顯示在Referenced Libraries目錄下。
2.打開Myeclipse Database Explorer進行Oracle數據庫連接配置。
右鍵反手一個new→Driver template選擇Oracle(Thin driver)→Driver name:OracleDriver→Connection URL:jdbc:oracle:thin:@localhost:1521:<database_name>(<database_name>用要連接的數據庫名代替)→User name: 用來登錄數據庫的用戶名→Password:對應密碼→Add JARs(路徑和導包的時候一樣,選一樣版本的)→Driver classname:這裏是Add JARs後自動生成的,註意Class.forName()必須對應到這裏→Test DriverSave→顯示Database connection successfully established就表示連接成功。(可以Save password方便下次test)
3.編寫連接代碼,祝好運。
Logincheck.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String driverName="oracle.jdbc.OracleDriver";
String dbURL="jdbc:oracle:thin:@localhost:1521:test";
String userName="system";
String userPwd="1234";
Connection conn = null;
ResultSet rs = null;
%>
<%
boolean flag=false;
String user = request.getParameter("usern");
System.out.println(user);
String pass = request.getParameter("passw");
System.out.println(pass);
try{
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test", "system", "1234");
String sql="select * from Log where UN=‘ "+user+" ‘ and PD=‘ "+pass+" ‘ ";
Statement stmt = conn.createStatement();
System.out.println("stmt");
rs = stmt.executeQuery(sql);
System.out.println("rs suc");
if(rs.next()){
System.out.println("yes");
flag=true;
}else{
flag=false;
System.out.println("no");
}
rs.close();
stmt.close();
conn.close();
}catch(Exception err){
System.out.println("error");
}
if(flag){
%>
<jsp:forward page="suc.jsp"/>
<%
}else{
%>
<jsp:forward page="error.jsp"/>
<%
}
%>
</body>
</html>
Login.jsp(只截取主要部分)
<body>
<form action="Logincheck.jsp" method="post">
<table align="center">
<tr> <td>用戶名: <input type="text" name="usern" /></td> </tr>
<tr> <td>密碼: <input type="password" name="passw" /><a href="find.jsp">忘記密碼?</a> </td></tr>
</table>
</form>
</body>
ORACLE+Myeclipse+jsp實現簡單登錄功能