JSP使用者登入(JavaBean操作sql server資料庫) (tomcat伺服器)

來源:互聯網
上載者:User
我的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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.