天天看點

Jsp_資料庫操作(2)前言一、目的二、開始總結

目錄

前言

一、目的

二、開始

1.需要工具

2.環境

3.代碼

總結

前言

提示:JSP的日常一些Demo案例!

提示:以下是本篇文章正文内容,下面案例可供參考

一、目的

為了實作通過頁面對資料庫進行增 查功能

二、開始

1.需要工具

  • eclipse
  • java環境
  • tomcat
  • mysql-connect-jdbc

2.環境

  • eclipse能正常打開
  • jsp的預設項目能正常運作

3.代碼

本人的資料庫是mysql5.+

jdbc工具是5.0.8

這是login.jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>登入界面</title>
</head>
<body>
	<form method="POST" action="checkLogin.jsp">
			<table  cellspacing="" cellpadding="">
				<tr>
					<td>賬号:</td>
					<td>
						<input type="text" name="username" id="username" value="" />
					</td>
				</tr>
				<tr>
					<td>密碼:</td>
					<td>
						<input type="password" name="password" id="username" value="" />
					</td>
				</tr>
				<tr>
					<td>角色:</td>
					<td>
						<input type="radio" name="role" id="" value="管理者" /><label>管理者</label>
						<input type="radio" name="role" id="" value="普通使用者" checked=""/><label>普通使用者</label>
					</td>
				</tr>
				<tr>
					<td align="center" colspan="2">
						<input type="submit" name="" id="" value="登入" />
						<input type="reset" name="" id="" value="重置" />
					</td>
				</tr>
			</table>
	</form>
</body>
</html>
           
checkLogin.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    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>驗證賬号密碼</title>
</head>
<body>
	<%
	request.setCharacterEncoding("utf-8");
	String username=request.getParameter("username");
	String password=request.getParameter("password");
	String role=request.getParameter("role");
	%>
	
	<% 
			//插入資料庫
        try {
            Class.forName("com.mysql.jdbc.Driver");
            out.println("加載驅動類成功<br/>");
            String url="jdbc:mysql://localhost:3306/yahoodb?user=root&password=123456&useUnicode=true&characterEncoding=utf-8";
            Connection conn = DriverManager.getConnection(url);
        	if(conn!=null){
        		out.println("資料庫連接配接成功<br/>");
        	}
        	String sql="select * from users where username=? and password =? and role=?";
        	//"insert into t_email values('?','?','?','?','?','?','?','?')"
        	PreparedStatement ps=conn.prepareStatement(sql);
        	ps.setString(1,username );
        	ps.setString(2,password );
        	ps.setString(3,role );
        	ResultSet result=ps.executeQuery();
        	if(result.next()){
        		System.out.print("登入成功<br/>");
        		session.setAttribute("username", username);
        		response.sendRedirect("addEmail.jsp");
        	}else{
        		System.out.println("登入失敗");
        	%>
        		<jsp:forward page="login.jsp"></jsp:forward>
     <% 
        	} 
        	
        } catch (Exception e) {
            out.println("找不到驅動類<br/>"+e);
        }
    %>
	

</body>
</html>
           
這是logined.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>驗證賬号密碼</title>
</head>
<body>
	<%
	if(session.getAttribute("username")==null||session.getAttribute("username").equals("")){
		out.println("<script type='text/javascript'>  alert('請先登入!'); location.href='login.jsp'; </script>");
		//response.setHeader("refresh", "2;url='login.jsp'");
	}else{
		}
	%>
</body>
</html>

           
這是addEmail.jsp檔案  前端資料輸入
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>登入資訊</title>
</head>
	<body>
	<form action="saveEmail.jsp" method="post">
		<table  cellspacing="" cellpadding="">
			<tr>
				<td>
					選擇您的雅虎郵箱:
					<input type="text" name="mail" id="" value="" />
					<span style="color: red;">@</span>
					<SELECT name="domain">
						  <OPTION value="1" name="domain">yahoo.cn</OPTION>
						  <OPTION value="2" name="domain">yahoo.com.cn</OPTION>
					</SELECT>
				</td>
			</tr>
			<tr>
				<td>
					密碼:
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<input type="password" name="passwd1" id="" value="" size="6"/>
				</td>
			</tr>
			<tr>
				<td>
					再次輸入密碼:
					<input type="password" name="passwd2" id="" value="" size="6"/>
				</td>
			</tr>
			<tr>
				<td>
					真實姓名:
					<input type="text" name="name" id="" value="" />
				</td>
			</tr>
			<tr>
				<td>
					出生日期:
					<input type="text" name="birthday" id="" value="" />
				</td>
			</tr>
			<tr>
				<td>
					性别:
					<input type="radio" name="gender" id="" value="男" checked=""/>男
					<input type="radio" name="gender" id="" value="女" />女
				</td>
			</tr>
			<tr>
				<td>雜志訂閱:
					<input type="checkbox" name="habbit" id="" value="旅遊" />旅遊
					<input type="checkbox" name="habbit" id="" value="财經" />财經
					<br />
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<input type="checkbox" name="habbit" id="" value="汽車" />汽車
					<input type="checkbox" name="habbit" id="" value="音樂" />音樂
				</td>
			</tr>
			<tr>
				<td>雅虎服務條款
					<textarea name="tips" rows="5" cols="50">雅虎中國對于任何包含于、經由、或聯結、下載下傳或從任何與本網站有關服務(以下簡稱「服務」)所獲得之資訊、内容或廣告(以下簡稱「資料」),您于此接受并承認信賴任何「資料」所生之風險應自行承擔。雅虎中國,有權但無此義務,改善或更正在「服務」或「資料」任何部分之錯誤或疏失。</textarea>
				</td>
			</tr>
			<tr>
				<td>
					<input type="submit" name="submit" id="submit" value="同意服務條款并送出" style="width: 200px;"/ >
					&nbsp;&nbsp;
					<input type="reset" name="reset" id="reset" value="清空" />
				</td>
			</tr>
		</table>
	</form>
	</body>
</html>
           
saveEmail.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" 
    import="java.util.*" 
    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>插入資料頁面</title>
<style type="text/css">
	table th{
		width:100px;
	}
</style>
</head>
<body>
	<%
		//資料呈現
		request.setCharacterEncoding("utf-8");
		String user=request.getParameter("mail");
		String domains=request.getParameter("domain");
		String domain="";
		if(domains.equals("1")){
			domain="yahoo.cn";
		}else if(domains.equals("2")){
			domain="yahoo.com.cn";
		}
		String pwd=request.getParameter("passwd1");
		String confirm_pwd=request.getParameter("passwd2");
		String realname=request.getParameter("name");
		String birthday=request.getParameter("birthday");
		String sex=request.getParameter("gender");
		String[] habbit=request.getParameterValues("habbit");
		String tips=request.getParameter("tips");
		String habbits="";
		
		for(int i=0;i<habbit.length;i++){
			habbits+=habbit[i];
			if(i!=habbit.length-1){
				habbits+=",";
			}
		} 
	%>
	<%
		//插入資料庫
        try {
            Class.forName("com.mysql.jdbc.Driver");
            out.println("加載驅動類成功<br/>");
            String url="jdbc:mysql://localhost:3306/yahoodb?user=root&password=123456&useUnicode=true&characterEncoding=utf-8";
            Connection conn = DriverManager.getConnection(url);
        	if(conn!=null){
        		out.println("資料庫連接配接成功<br/>");
        	}
        	String sql="insert into t_email values(?,?,?,?,?,?,?,?)";
        	//"insert into t_email values('?','?','?','?','?','?','?','?')"
        	PreparedStatement ps=conn.prepareStatement(sql);
        	ps.setString(1,user );
        	ps.setString(2,domains );
        	ps.setString(3,pwd );
        	ps.setString(4,realname );
        	ps.setString(5,birthday );
        	ps.setString(6,sex );
        	ps.setString(7,habbits );
        	ps.setString(8,tips );
        	int result=ps.executeUpdate();
        	if(result!=0){
        		out.print("成功插入<br/>");
    %>
        		<table >
        		<tr>
        			<th>郵箱</th>
        			<td><%=user %></td>
        		</tr>
        		<tr>
        			<th>郵箱字尾</th>
        			<td><%=domain %></td>
        		</tr>
        		<tr>
        			<th>密碼</th>
        			<td><%=pwd %></td>
        		</tr>
        		<tr>
        			<th>确認密碼</th>
        			<td><%=confirm_pwd %></td>
        		</tr>
        		<tr>
        			<th>真實姓名</th>
        			<td><%=realname %></td>
        		</tr>
        		<tr>
        			<th>生日</th>
        			<td><%=birthday %></td>
        		</tr>
        		<tr>
        			<th>性别</th>
        			<td><%=sex %></td>
        		</tr>
        		<tr>
        			<th>愛好</th>
        			<td>
        			<%
        				out.println(habbits);
        			%>
        			</td>
        		</tr>
        		<tr>
        			<th>條款</th>
        			<td width="300px"><%=tips %></td>
        		</tr>
        	</table>
        		
    <% 	
    		response.setHeader("Refresh", "3;url=selectEmail.jsp");
        	}else{
        		out.print("插入失敗<br/>");
        	}
        	
        } catch (Exception e) {
            out.println("找不到驅動類<br/>"+e);
        }
    %>
		
</body>
</html>
           
 selectEmail.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    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>查詢資訊</title>
</head>
<body>

	<table >
		<tr>
			<td>使用者名:</td>
			<td>郵箱:</td>
			<td>真實名字:</td>
			<td>生日:</td>
			<td>性别:</td>
		</tr>
	<%
 	Class.forName("com.mysql.jdbc.Driver");
          String url="jdbc:mysql://localhost:3306/yahoodb?user=root&password=123456&useUnicode=true&characterEncoding=utf-8";
          Connection conn = DriverManager.getConnection(url);
          String sql="select * from t_email";
          Statement sm=conn.createStatement();
          //System.out.println(sql);
          ResultSet result=sm.executeQuery(sql);
          
          result.last();
          int count=result.getRow();
          result.beforeFirst();
          
          while(result.next()){
          	String username=result.getString("username");
          	String domain=result.getString("domain");
          	String realname=result.getString("realname");
          	String birthday=result.getString("birthday");
          	String sex=result.getString("sex");
    %>
    	<tr>
			<td>
				<a href="detail_info.jsp?selectUserName=<%=username%>" target="_blank" rel="external nofollow" >
					<%=username %>
				</a>
			</td>
			<td><%=domain %></td>
			<td><%=realname %></td>
			<td><%=birthday %></td>
			<td><%=sex %></td>
		</tr>
    <% 
          }
   	%>
   	</table>
	

</body>
</html>
           
detail_info.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    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>查詢資訊</title>
</head>
<style type="text/css">
	.info_table{
		text-algin:center;
		margin:10% 30%;
	}
	.info_table table tr td{
		width:500px;
	}
</style>
<body>
	<div class="info_table">
	<table >
	<%
 		  Class.forName("com.mysql.jdbc.Driver");
          String url="jdbc:mysql://localhost:3306/yahoodb?user=root&password=123456&useUnicode=true&characterEncoding=utf-8";
          Connection conn = DriverManager.getConnection(url);
          
          String selectUserName=request.getParameter("selectUserName");
          String sql="select * from t_email where username='"+selectUserName+"'";
          Statement sm=conn.createStatement();
          ResultSet result=sm.executeQuery(sql);
          
          result.last();
          int count=result.getRow();
          result.beforeFirst();
          
          while(result.next()){
          	String username=result.getString("username");
          	String domain=result.getString("domain");
          	String password=result.getString("pwd");
          	String realname=result.getString("realname");
          	String birthday=result.getString("birthday");
          	String sex=result.getString("sex");
          	String order=result.getString("order");
          	String description=result.getString("description");
    %>
    		<tr>
				<th>使用者名:</th>
				<td><%=username %></td>
			</tr>
			<tr>
				<th>郵箱:</th>
				<td><%=domain %></td>
			</tr>
			<tr>
				<th>密碼:</th>
				<td><%=password %></td>
			</tr>
			<tr>
				<th>真實名字:</th>
				<td><%=realname %></td>
			</tr>
			<tr>
				<th>生日:</th>
				<td><%=birthday %></td>
			</tr>
			<tr>
				<th>性别:</th>
				<td><%=sex %></td>
			</tr>
			<tr>
				<th>喜好:</th>
				<td><%=order %></td>
			</tr>
			<tr>
				<th>描述:</th>
				<td><%=description %></td>
			</tr>
    <% 
          }
   	%>
   	</table>
   	</div>
	

</body>
</html>
           

總結

實作了登入根據資料庫中表的資訊來登入,并對資料庫進行 select insert操作!!
jsp