您的位置:首页 > 运维架构 > Tomcat

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

2015-07-24 12:53 633 查看
我的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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: