jsp JDBC连接MySQL数据库操作标准流程参考

1. 此案例以帐号密码后台更新维护为例子,对数据库调取数据更新流程进行演示:


代码示例:


<%@page import="java.io.IOException"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>SQLOper_Demo</title>
</head>
<body>

	<%!
		/*数据库初始化样例代码段*/
		int flag = 0;
		String username = "";
		String password = "";
		String sql_demo = "";
		PreparedStatement pres = null;
		public static Connection getConn(JspWriter out) throws IOException {
		    String url = "jdbc:mysql://localhost:3306/user";
		    String username = "root";
		    String password = "admin";
		    Connection conn = null;
		    try {
		    	Class.forName("com.mysql.jdbc.Driver"); //classLoader,加载对应驱动
		        conn = (Connection) DriverManager.getConnection(url, username, password);
		    }catch(ClassNotFoundException e){
		    	out.println("数据库组件查找异常!!");
		        e.printStackTrace();
		    }catch(SQLException e){
		    	out.println("数据库操作异常!!");
		    	e.printStackTrace();
		    }
		    return conn;
		}
	%>
	
	<%
		/*参数接收以及判断流程代码段*/
		try{
			username = request.getParameter("username");
			password = request.getParameter("password");
		}catch(Exception e){
			out.println("参数接收异常");
			e.printStackTrace();
		}
		try{
			Connection conn = getConn(out);
			if(conn == null){
				out.println("连接异常!");
			}else{
				sql_demo = "update userinfo set username=?,password=?";
				pres = conn.prepareStatement(sql_demo);
				pres.setString(1, username);
				pres.setString(2,password);
				pres.executeUpdate();
				if(flag == 1){
					out.println("帐户更新成功!!"+"<br>");
				}else{
					out.println("帐户更新失败!!"+"<br>");
				}
				pres.close();
				conn.close();
			}
		}catch(SQLException e){
			out.print("帐户更新出现异常!!");
			e.printStackTrace();
		}
		
	%>
</body>
</html>



2. 此案例以帐号密码后端登录验证的例子,对数据库的数据取出流程进行演示:


代码示例:


<%@page import="java.io.IOException"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Requests</title>
</head>
<body>
	<%!
		/*数据库初始化样例代码段*/
		String username = "";
		String password = "";
		String correct_password = "";
		String sql_Request = "";
		PreparedStatement pres = null;
		ResultSet res = null;
		public static Connection getConn(JspWriter out) throws IOException {
		    String url = "jdbc:mysql://localhost:3306/user";
		    String username = "root";
		    String password = "admin";
		    Connection conn = null;
		    try {
		    	Class.forName("com.mysql.jdbc.Driver"); //classLoader,加载对应驱动
		        conn = (Connection) DriverManager.getConnection(url, username, password);
		    }catch(ClassNotFoundException e){
		    	out.println("数据库组件查找异常!!");
		        e.printStackTrace();
		    }catch(SQLException e){
		    	out.println("数据库操作异常!!");
		    	e.printStackTrace();
		    }
		    return conn;
		}
	%>
	
	<%	
		/*帐号密码验证代码段*/
		try{
			username = request.getParameter("username");
			password = request.getParameter("password");
		}catch(Exception e){
			out.println("参数接收异常");
			e.printStackTrace();
		}
		try{
			Connection conn = getConn(out);
			if(conn == null){
				out.println("连接异常!");
			}else{
				sql_Request = "select password from userinfo where username=?";
				pres = conn.prepareStatement(sql_Request);
				pres.setString(1, username);
				res = pres.executeQuery();
				while(res.next()){
					correct_password = res.getString(1);
				}
				pres.close();
				conn.close();
			}
			if(password.equals(correct_password)){
				out.println("Login Successful !! <br><br> Please wait for 3 seconds...");
				out.println("<meta http-equiv='refresh' content=\"3;url='Oper.jsp'\">s ");
			}else{
				out.println("Login Failed !! <br><br> Please wait for 3 seconds...");
				out.println(" <meta http-equiv='refresh' content=\"3;url='index.html'\"> ");
			}
		}catch(SQLException e){
			out.print("帐户检索失败!! 请查证后在试!!");
			e.printStackTrace();
		}
	%>
</body>
</html>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值