zoukankan      html  css  js  c++  java
  • 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>
    


  • 相关阅读:
    scanf使用尿性
    System : Assembly Programming
    Biology 03: Cardiovascular
    remove the smallest element from a linkedlist
    Relativity 04: CH4CH5
    Relativity 03: Space and Time in Classical Mechanics
    146 __str__和__repr__
    145 __init__和__new__
    144 __call__
    143 __doc__
  • 原文地址:https://www.cnblogs.com/csnd/p/12897061.html
Copyright © 2011-2022 走看看