jsp链接数据库

数据库表代码:

/*
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>

时间: 2024-12-27 22:15:45

jsp链接数据库的相关文章

jsp链接数据库源代码

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

python链接数据库

1.首先要下载安装数据库 2.安装mysql模块(pip install mysql:easy_install mysql) 3.链接数据库 # -*- coding:utf-8-*-import MySQLdbimport sysreload(sys)sys.setdefaultencoding('utf8') conn=MySQLdb.connect(host="127.0.0.1", port=3306, user="root", passwd="1

PDO链接数据库

链接数据库 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Co

java:JSP(JSPWeb.xml的配置,动态和静态导入JSP文件,重定项和请求转发,使用JSP实现数据库的增删改查实例)

1.JSP的配置: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":&qu

链接数据库 远程事务的处理方式

环境:windows server 2003 SP2 + mssql 2000 SP4 最近在开发一个报表查询系统,使用到链接数据库技术,由于第一次接触该技术,所以在开发的过程中遇到了重重困难,幸好有百度,幸好有热心的同事,问题也如愿以偿的解决了. 在整个开发过程中使用了两种链接技术: 1.ODBC链接方式 --建立链接服务器 EXEC sp_addlinkedserver 'remote_queueRpt','','MSDASQL',NULL,NULL,'DRIVER={SQL Server}

理解class.forName() ---使用jdbc方式链接数据库时会经常看到这句代码

目录(?)[-] 官方文档 类装载 两种装载方法的区别 不同的类装载器 是否实例化类 在jdbc链接数据库中的应用 资源 原文地址:http://yanwushu.sinaapp.com/class_forname/ 使用jdbc方式链接数据库时会经常看到这句代码:Class.forName(String className).本文将浅显地解读这句代码的含义.看这篇文章之前应该对java的反射机制有所了解,至少是对Class类有一定的了解. 官方文档 初始化参数指定的类,并且返回此类对应的Cla

链接数据库

1 //生成一个连接 2 $db_connect=mysql_connect($dbhost,$username,$userpass) or die("Unable to connect to the MySQL!"); 3 4 //选择一个需要操作的数据库 5 mysql_select_db($dbdatabase,$db_connect); 6 7 //执行MySQL语句 8 $result=mysql_query("SELECT id,name FROM user&qu

Java通过ODBC链接数据库并遍历结果的一个问题

上一篇文章谈到怎么连接Oracle数据库,其实通过ODBC也差不多,只是driver要换成JdbcOdbcDriver.配置文件如下: driver=sun.jdbc.odbc.JdbcOdbcDriver url=jdbc:odbc:[Your DSN] username=[Your user name] password=[Your password] 需要注意的是,在sql语句里,避免出现重复的列别名,例如: select o.TIMESTAMP ReceivedTimestamp, o.

tp框架知识 之(链接数据库和操作数据)

框架有时会用到数据库的内容,在"ThinkPhp框架知识"的那篇随笔中提到过,现在这篇随笔详细的描述下. 一.链接数据库 (1)找到模块文件夹中的Conf文件夹,然后进行编写config.php文件 我这里是这样的文件路径 (2)打开这个config.php文件,然后找到父类配置文件convention.php文件,将关于"数据库"的部分复制粘贴到config.php配置文件中 1 2 3 4 5 6 7 8 9 /* 数据库设置 */    'DB_TYPE'