实例,JSP 连接MySQL实现增删改查,注册登录

简单实例JSP 连接MySQL实现增删改查,注册登录。
本文实例下载地址。
https://download.csdn.net/download/by2233/10747249
**1,运行环境MySQL5.5,Tomacat7,JDBC驱动,Eclipse Luna-SR2 **

2,环境配置

下载Java 专用的连接MySQL的驱动包mysql-connector-java-5.1.47-bin.jar。
MySQL官网可以下载,下载地址https://dev.mysql.com/downloads/connector/j/
把mysql-connector-java-x.x.x-bin.jar拷贝到Tomcat的安装bin目录。X:\XXX\Tomcat\lib下,或者直接拷进Eclipse工程WEB-INF\lib下。

3,JSP连接MySQL
建立数据库xxx,建立表xxx。
有三个列,id,username,password,类型自定义即可。
我这里用了两个表。
现在使用jsp连接mysql了。
建立一个页面userlist.jsp 。

代码如下,查询user表下所有数据

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>




用户列表


<% 
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/kongdechang","root","root");
Statement statement=con.createStatement();

ResultSet rs=statement.executeQuery("SELECT * FROM user");
//sql.execute("SELECT*FROM user");
//rs.next();
%>

<%
while(rs.next()){
	

    
<%
}

rs.close();
statement.close();
con.close();
%>
用户id用户名密码
<%out.println(rs.getInt("id"));%> <%out.println(rs.getString("username"));%> <%out.println(rs.getString("password"));%>
  • 添加页面,jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>




添加用户


用户名
密码  
        

添加操作

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<% 
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");  
String pwd = request.getParameter("pwd");
Class.forName("com.mysql.jdbc.Driver");
//桥接
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/kongdechang","root","root");
//连接数据库
String sql = "insert into admin values(null,?, ?)";

PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,pwd);
ps.executeUpdate();
con.close();
response.sendRedirect("adduser.jsp");
%>

删除页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




删除用户


ID

删除操作

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<% 
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");  
String pwd = request.getParameter("pwd");
String id= request.getParameter("id");
Class.forName("com.mysql.jdbc.Driver");
//桥接
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/kongdechang","root","root");
//连接数据库

String sql = "DELETE FROM `kongdechang`.`user` WHERE (`id` = ?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,id);

ps.executeUpdate();

con.close();
response.sendRedirect("deleteuser.jsp");
%>

修改页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>




修改用户


ID
用户名
密码  
        

修改操作

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<% 
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");  
String password = request.getParameter("password");
String id= request.getParameter("id");
Class.forName("com.mysql.jdbc.Driver");
//桥接
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/kongdechang","root","root");
//连接数据库

String sql = "UPDATE user SET"+" username='"+username+"',"+"password='"+password+"'"+ " WHERE (`id` = ?)";
Statement statement=con.createStatement();
//语句对象
//String sql = "select * from admin where name='"+ name +"'and pwd='"+ pwd + "'";
//ResultSet rs=statement.executeQuery(sql);
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,id);
//ps.setString(2,username);
//ps.setString(3,password);
ps.executeUpdate();

con.close();
response.sendRedirect("updateuser.jsp");
%>

登录页面与注册

用户名
 密码  
                    注册
用户名
密码  
                  登录

登录处理

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.sql.*"%> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


  




<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");  
String pwd =  request.getParameter("pwd");
%>
<%
Class.forName("com.mysql.jdbc.Driver");
//桥接
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/kongdechang","root","root");
//连接数据库
Statement statement=con.createStatement();
//语句对象
String sql = "select * from admin where name='"+ name +"'and pwd='"+ pwd + "'";
ResultSet rs=statement.executeQuery(sql);	
//语句对象			
if(rs.next()){
                    response.sendRedirect("loginsuccess.jsp");  
                }else{
                    response.sendRedirect("login.jsp"); 
                }
     rs.close();
     statement.close();
     con.close();

%>




注册处理

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
    
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>







<% 
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");  
String pwd = request.getParameter("pwd");
Class.forName("com.mysql.jdbc.Driver");
//桥接
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/kongdechang","root","root");
//连接数据库
String sql = "insert into admin values(null,?, ?)";

PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,pwd);
ps.executeUpdate();
con.close();
response.sendRedirect("registersuccess.jsp");
%>


你可能感兴趣的:(jsp)