JSP用户登录(JavaBean操作sql server数据库) (tomcat服务器)

我的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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值