JSP连接mysql,实现登录界面
上一篇 /
下一篇 2012-05-24 12:08:48
/ 个人分类:web
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%!
public static final String DBDRIVER = "com.mysql.jdbc.Driver" ;
public static final String userName = "root"; //登录的用户名
public static final String userPasswd = "jwzhangjie"; //登录mysql密码
public static final String dbName = "test"; //数据库名
public static final String tableName="tuser"; //表名
public static final String DBURL = "jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userPasswd;
%>
<%
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
boolean flag = false ; // 表示登陆成功或失败的标记
%>
<%
String userid = request.getParameter("userid") ; // 接收表单参数
String password = request.getParameter("password") ; // 接收表单参数
try{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL) ;
String sql = "SELECT userid,password FROM tuser WHERE userid=? AND password=?" ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,userid) ;
pstmt.setString(2,password) ;
rs = pstmt.executeQuery() ;
while(rs.next()){
// 如果有内容,则此处执行,表示查询出来,合法用户
flag = true ;
}
}catch(Exception e){
}finally{
try{
conn.close() ; // 连接一关闭,所有的操作都将关闭
}catch(Exception e){}
}
%>
<%
if(flag){ // 登陆成功,应该跳转到success.jsp
%>
<jsp:forward page="success.jsp"/>
<%
}else{ // 登陆失败,跳转到failure.jsp
%>
<%
}
%>
相关阅读:
- MySQL数据库中varchar的限制规则 (xin_晴, 2012-3-22)
- Mysql 安装配置 (系统消息, 2012-3-27)
- 11步教你选择最稳定的MySQL版本 (xin_晴, 2012-4-06)
- Linux mysql 安装配置 (系统消息, 2012-4-11)
- 如何给Python的MySQL模块加功能 (xin_晴, 2012-4-17)
- MySQL数据库性能优化之SQL优化 (xin_晴, 2012-4-19)
- MySQL数据库性能优化之表结构优化 (xin_晴, 2012-5-03)
- MySQL数据库性能优化之索引优化 (xin_晴, 2012-5-04)
- MySQL数据库性能优化之存储引擎选择 (xin_晴, 2012-5-08)
- 优化MySQL语句的十个建议 (xin_晴, 2012-5-14)
收藏
举报
TAG:
JSP
jsp
MySQL
mysql