<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
创新互联公司主营丰宁网站建设的网络公司,主营网站建设方案,App定制开发,丰宁h5小程序定制开发搭建,丰宁网站营销推广欢迎丰宁等地区企业咨询
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
This is my JSP page.
注册页面代码:
<%@ 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", "登录");
%>
<%
}else{ // 登陆失败,跳转到failure.jsp
request.getSession().setAttribute("error", "登录");
%>
<%
}
}
%>
链接数据库代码:
<%@ 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", "注册");
%>
<%
}else{ // 登陆失败,跳转到failure.jsp
request.getSession().setAttribute("error", "注册");
%>
<%
}
}else{}
%>
登录失败代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
${sessionScope.error }失败
登陆成功代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
${sessionScope.success }成功!
数据库表:
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);
文章名称:jsp链接数据库源代码
链接分享:http://lswzjz.com/article/gicodh.html