数据库表代码:

/*
Navicat MySQL Data Transfer

Source Server : localhost_3306
Source Server Version : 50528
Source Host : localhost:3306
Source Database : bbs

Target Server Type : MYSQL
Target Server Version : 50528
File Encoding : 65001

Date: 2016-06-04 09:25:01
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`linkman` varchar(255) DEFAULT NULL,
`company` varchar(255) DEFAULT NULL,
`companytel` varchar(255) DEFAULT NULL,
`qq` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('1', 'admin', 'admin', null, null, null, null);
INSERT INTO `tb_user` VALUES ('2', 'user', 'user', null, null, null, null);

注册页面:

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.sql.*"%>
<%!
public static final String DBDRIVER = "com.mysql.jdbc.Driver" ;
public static final String userName = "root"; //登录的用户名
public static final String userPasswd = "taizhen"; //登录mysql密码
public static final String dbName = "test"; //数据库名
public static final String tableName="user"; //表名
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 action = request.getParameter("action");
if(null==action){

}else{
%>

<%out.println(action); %>
<%
String name = request.getParameter("username") ; // 接收表单参数
String password = request.getParameter("password") ; // 接收表单参数
try{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL) ;
String sql = "SELECT name,password FROM user WHERE name=? AND password=?" ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,name) ;
pstmt.setString(2,password) ;
rs = pstmt.executeQuery() ;
while(rs.next()){
// 如果有内容,则此处执行,表示查询出来,合法用户
flag = true ;
}
}catch(Exception e){
}finally{
try{
conn.close() ; // 连接一关闭,所有的操作都将关闭
}catch(Exception e){}
}
%>
<%out.println(flag); %>
<%out.println(action); %>
<%

if(flag){
request.getSession().setAttribute("success", "登录");
%>
<jsp:forward page="success.jsp"/>
<%
}else{ // 登陆失败,跳转到failure.jsp
request.getSession().setAttribute("error", "登录");

%>
<jsp:forward page="error.jsp"/>
<%
}
}

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login.jsp' starting page</title>

</head>

<body>
<form action="login.jsp" method="post">
<input type="hidden" name="action" />
用户名:
<input type="text" name="username" value="username"/>
<br>
密码:
<input type="password" name="password" value="password"/>
<br>
<input type="submit" value="login" />
</form>

</body>
</html>

jsp链接数据库代码:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.sql.*"%>
<%!
public static final String DBDRIVER = "com.mysql.jdbc.Driver" ;
public static final String userName = "root"; //登录的用户名
public static final String userPasswd = "taizhen"; //登录mysql密码
public static final String dbName = "test"; //数据库名
public static final String tableName="user"; //表名
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 action = request.getParameter("username");
if(action!=null){

%>

<%out.println(request.getParameter("password")); %>
<%
String name = request.getParameter("username") ; // 接收表单参数
String password = request.getParameter("password") ; // 接收表单参数
String linkman=request.getParameter("linkman");
String qq=request.getParameter("qq");

try{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL) ;
String sql = "INSERT INTO user (name, password) VALUES (?,?)";

pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,name) ;
pstmt.setString(2,password) ;

int sql_update = pstmt.executeUpdate();
if(sql_update>0){
flag=true;
}
}catch(Exception e){
}finally{
try{
conn.close() ; // 连接一关闭,所有的操作都将关闭
}catch(Exception e){}
}
%>

<%

if(flag){
request.getSession().setAttribute("success", "注册");
%>
<jsp:forward page="success.jsp"/>
<%
}else{ // 登陆失败,跳转到failure.jsp
request.getSession().setAttribute("error", "注册");

%>
<jsp:forward page="error.jsp"/>
<%
}

}else{}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'register.jsp' starting page</title>

</head>

<body>

<div class="loginbox registbox">

<div class="login-content reg-content">
<div class="loginbox-title">
<h3>注册</h3>
</div>
<form id="signupForm" action="register.jsp" method="post">

<div class="row">
<label class="field" for="email">用户名</label>
<input type="text" class="input-text-user noPic input-click" name="username" id="email">
</div>
<div class="row">
<label class="field" for="password">密码</label>
<input type="password" value="" class="input-text-password noPic input-click" name="password" id="password">
</div>

<div class="row">
<label class="field" for="contact">联系人</label>
<input type="text" value="" class="input-text-user noPic input-click" name="linkman" id="contact">
</div>
<div class="row">
<label class="field" for="company">公司名</label>
<input type="text" value="" class="input-text-user noPic input-click" name="company" id="company">
</div>
<div class="row">
<label class="field" for="tel">公司电话</label>
<input type="text" value="" class="input-text-user noPic input-click" name="companytel" id="tel">
</div>
<div class="row">
<label class="field" for="qq">QQ</label>
<input type="text" value="" class="input-text-user noPic input-click" name="qq" id="qq">
</div>

<div class="row btnArea">
<button class="login-btn" id="submit">注册</button>
</div>
</form>
</div>
<div class="go-regist">
已有帐号,请<a href="/Demo/login.jsp" class="link">登录</a>
</div>
</div>
</body>
</html>

登录成功:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'success.jsp' starting page</title>

</head>

<body>
${sessionScope.success }成功! <br>
</body>
</html>

登录失败:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'error.jsp' starting page</title>

</head>

<body>
${sessionScope.error }失败 <br>
</body>
</html>

jsp链接数据库的更多相关文章

  1. JSP-Runoob:JSP 链接数据库

    ylbtech-JSP-Runoob:JSP 链接数据库 1.返回顶部 1. JSP 连接数据库 本教程假定您已经了解了 JDBC 应用程序的工作方式.在您开始学习 JSP 数据库访问之前,请访问 J ...

  2. jsp链接sql数据库

    Connection 参数//这个参数用来执行链接数据库的操作 String 参数2="com.microsoft.sqlserver.jdbc.SQLServerDriver"; ...

  3. Spring自带配置方式链接数据库(没有src新建文件,没有c3p0)

    1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...

  4. JDBC链接数据库步骤

    java中定义链接数据库的标准:JDBC 1.导包:不同数据库有不同的jdbc驱动包,而且jdbc驱动包和数据库版本必须对应 2.测试 3.写代码 try { 1.//加载JDBC驱动    Clas ...

  5. 链接数据库模板 DataBaseLinkTool

    一. 1.Dao层 对数据库的底层操作  增删改查 package Dao; import java.sql.Connection; import java.sql.ResultSet; import ...

  6. 课堂测试_WEB界面链接数据库

    课堂测试_WEB界面链接数据库 一,题目: 一. 考试要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求 ...

  7. PHP学习-链接数据库

    链接数据库文件:conn.php <?php $conn = mysql_connect("localhost:3306","root","us ...

  8. PHP 链接数据库1(连接数据库&简单的登录注册)

    对 解析变量的理解 数据库的名称和表的名称不能重复 从结果中取出的数据   都是以数组的形式取出的 1.PHP查询数据库中的某条信息 //PHP链接数据库 /*1.造链接对象 IP地址 用户名 密码 ...

  9. JDBC的使用(一):引用外部jar;代码链接数据库

    一:引用外部jar 1.首先不jar文件放到项目下: 2.在Eclipse中,右键相应的项目--构建路径--配置构建路径--库--添加外部jar:选中-打开-应用-确定. 二:代码链接数据库 1.加载 ...

随机推荐

  1. Apache Solr采用Java开发、基于Lucene的全文搜索服务器

    http://docs.spring.io/spring-data/solr/ 首先介绍一下solr: Apache Solr (读音: SOLer) 是一个开源.高性能.采用Java开发.基于Luc ...

  2. Sales_item例子

    Sales_item.h #ifndef SALES_ITEM_H #define SALES_ITEM_H #include<iostream> #include<string&g ...

  3. 动态缓存技术之CSI,SSI,ESI

    平常我们谈到网络缓存技术,大多是以页面为单位的,比如,新闻网站中将执行后的结果,缓存为一个静态html文件,下次访问时就直接访问这个静态页面了! 减轻了服务器压力!但是,如果一个页面大部分是可静态的, ...

  4. Helpers\PHPMailer

    Helpers\PHPMailer PHPMailer is a third party class for sending emails, Full docs are available athtt ...

  5. IOS应用程序多语言本地化解决方案

    最近要对一款游戏进行多语言本地化,在网上找了一些方案,加上自己的一点点想法整理出一套方案和大家分享! 多语言在应用程序中一般有两种做法:一.程序中提供给用户自己选择的机会:二.根据当前用户当前移动设备 ...

  6. html笔记02:html,body { ……}

    html,body { margin:0px; height:100%; } html元素可告知浏览器其自身是一个 HTML 文档.body 元素定义文档的主体.它包含文档的所有内容(比如文本.图像. ...

  7. Java操作字符串的工具类

    操作字符串的工具类 import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStre ...

  8. 【暴力,STL,水】UVa 1523 - Helicopter

    Since ancient time, people have been dreaming of flying in the sky. Eventually, the dream was realiz ...

  9. hdu 4670 树的点分治

    思路:首先当然是要用树的点分治了.根节点为root,那么经过root的合法路径数求出来这题就解决了.因为我们可以用分治枚举根,最后将所有根的路径数加起来就是结果.当然这里的根不是整棵树的根,是子树根. ...

  10. 判断checkbox选中

    源码如下,仅限参考,直接复制即可: <meta http-equiv="Content-Type" content="text/html; charset=utf- ...