我的bean
package db;
import java.sql.*;
import java.io.*;
import java.util.*;
public class dbConn{
String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=disc";
String user="sa";
String password="20263588";
Statement stmt=null;
Connection conn=null;
ResultSet rs=null;
public dbConn(){
try{
Class.forName(sDBDriver).newInstance();
System.out.println("載入資料庫驅動成功");
}
catch(Exception e)
{
e.printStackTrace() ;
}
}
public ResultSet executeQuery(String sql){
try{
conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
System.out.print("串連資料庫成功");
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql){
stmt=null;
rs=null;
try{
conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex){
System.err.println(ex.getMessage());
}
}
public void closeStmt()
{
try{
stmt.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void closeConn()
{
try{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
login_confirm.jsp代碼
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head>
<title>登入驗證</title>
</head>
<body>
<%@ page language="java" import="java.sql.*,java.util.*,java.lang.*" %>
<jsp:useBean id="connDbBean" scope="page" class="db.dbConn"/>
<%
String name1= new String(request.getParameter("user_id").getBytes("ISO-8859-1"));
String pwd1= new String(request.getParameter("password").getBytes("ISO-8859-1"));
String sql="select * from adm where name=' "+name1+" ' and password=' "
+pwd1+"'";
ResultSet rs=connDbBean.executeQuery(sql);
if(rs.next()){
rs.close();
connDbBean.closeStmt();
connDbBean.closeConn();
session.putValue("username",name1);
session.putValue("password",pwd1);
%>
<script language=javascript>
window.location="index_OK.jsp?mark=dfsd"
</script>
<% }
else{
rs.close();
connDbBean.closeStmt();
connDbBean.closeConn();
session.putValue("login_message",msg);
%>
<jsp:foward page="relogin.jsp"/>
<% } %>
</body>
</html>
問題補充:sos提交表單部分
<form action="login_confirm.jsp" method="post" name="form1">
<TD width="62" align="left"><div align="center">使用者名稱</div></td>
<td width="181"><input name="user_id" type="text" ></TD>
<TD width="48" align="left"><div align="center">密 碼</div></td>
<td width="168"><input type="password" name="password" ></TD>
<td colspan=2 align="center">
<input type="button" value="登入" OnClick="submit()" size="4" ; ></td>
<td width="129" colspan="2 " align="right" ><a href="register.jsp">
<input type="reset" value="取消" size="4" />
<span class="STYLE29">新會員註冊 </span></a></td>
</tr>
<%
String getLoginmessage=(String)session.getValue("loginSign");
if(getLoginmessage!="OK")
{
%>
<tr>
<td colspan=2>
<div align="center"></div>
</td>
</tr>
<%
}
else{
%>
<tr>
<td colspan=2 align="center">
<a href="change_infor.jsp">修改
</a>
</td>
</tr>
<%
}
%>
</table>
</form>
</div>
</body>
</html>