课程引言动手动脑

网站系统开发需要掌握的技术

技术内容
HTML语法、CSS语法、JavaScript语法
图像处理
Flash动画创意、GIF动画制作、网页图片处理
后台编程
*数据库:SQLServer设计、MySQL设计、Access设计
*编程语言:ASP、JSP、VBScript、JavaScript、PHP、ASP.net
*编程实例:文章发布系统、留言板、BBS、会员注册系统、在线购物网站

课堂作业:绘制一个登陆界面实现数据库连接。

 输入正确的用户密码:

错误的:

不输入:

package com.jaovo.msg.Util;
import java.sql.*;
public class DBUtil {
	
	public  static  Connection getConnection() {
		try {
			//1 加载驱动
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String user = "root";
		String password = "root";
		String url = "jdbc:mysql://localhost:3306/jaovo_msg";
		Connection connection = null;
		try {
			//2 创建链接对象connection
			 connection = DriverManager.getConnection(url,user,password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}
	
	//关闭资源的方法
	public static void close(Connection connection ) {
		try {
			if (connection != null) {
				connection.close();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void close(PreparedStatement preparedStatement ) {
		try {
			if (preparedStatement != null) {
				preparedStatement.close();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void close(ResultSet resultSet ) {
		try {
			if (resultSet != null) {
				resultSet.close();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  

<%@ 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>
	<title>用户登录界面</title>
</head>
<body>
	<form action="add.jsp" method="get">
	<%=request.getAttribute("a") %>
		<table align="center" border="1" width="500">
			<tr>
				<td>用户名:</td>
				<td>
					<input type="text" name="username"/>
				</td>
			</tr>
			<tr>
				<td>输入密码:</td>
				<td>
					<input type="password" name="password"/>
				</td>
			</tr>
			<tr align="center">
    			<td colspan="2">
    				<input type="submit" value="提交" />
    				<input type="reset" value="重置" />
    			</td>
    		</tr>
		</table>
	</form>>
</body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ page import = "com.jaovo.msg.Util.DBUtil" %> 
<%@ page import = "java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
	//接受客户端传递过来的参数
	String username = request.getParameter("username");
	if(username==null||"".equals(username))
	{
		request.setAttribute("a", "请输入用户名!");	
		%>
		<jsp:forward page="addInput.jsp"></jsp:forward>
		<% 
	}
	String password = request.getParameter("password");
	String sql="select * from t_user1 where username=?";
	Connection connection=DBUtil.getConnection();
	PreparedStatement preparedStatement=connection.prepareStatement(sql);
	preparedStatement.setString(1, username);
	ResultSet resultset=preparedStatement.executeQuery();
	boolean flag=false;
	while(resultset.next())
	{
		flag=true;
		if(resultset.getString("password").equals(password))
		{
			request.setAttribute("a", "登陆成功!");	
			%>
			
			<%=request.getAttribute("a")%>
			<% 
			
		}
		else
		{
			request.setAttribute("a", "登录失败!");
			%>
			<jsp:forward page="addInput.jsp"></jsp:forward>
			<% 
		}
	
	}
	if(!flag)
	{
		request.setAttribute("a","用户不存在!");
		
		%>
		<jsp:forward page="addInput.jsp"></jsp:forward>
		<% 
		
	}
	
	
	
%>
<body>
	
</body>
</html>

  我计划每天花费一小时在这个课程上。

原文地址:https://www.cnblogs.com/xiaojq/p/7885304.html