1.源代码
package com.jaovo.msg.Util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { public static Connection getConnection() { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String user = "sa"; String password = "123456"; String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=jaovo_msg"; Connection connection = null; try { 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> <%String res=(String)request.getAttribute("error"); if(res=="null"||res==null) { res=""; } %> <%=res%> <form action="logincheck.jsp" method="get"> <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="重置" /> <input type="button" value="注册" onClick="window.location.href='addInput.jsp'"/> </td> </tr> </table> </form> </body> </html> //网页 html
<%@page import="com.jaovo.msg.Util.UserException"%> <%@page import="com.jaovo.msg.dao.UserDaoImpl"%> <%@page import="com.jaovo.msg.model.User"%> <%@ 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> <% //接收客户端传递过来的参数 String username = request.getParameter("username"); String password = request.getParameter("password"); if (username == null || "".equals(username.trim())) { request.setAttribute("error", "用户名不能为空"); %> <jsp:forward page="login.jsp"></jsp:forward> <% } %> <% User user = new User(); user.setUsername(username); user.setPassword(password); UserDaoImpl userDao = new UserDaoImpl(); try { if (userDao.checkuser(user)) { %> <jsp:forward page="success.jsp"></jsp:forward> <% } else { request.setAttribute("error", "用户名/密码有误"); %> <jsp:forward page="login.jsp"></jsp:forward> <% } } catch (UserException e) { %> <h2 style="color: red; font-size: 50px"> 发生错误 : <%=e.getMessage()%></h2> <% } %> </html>
2.截图:
随后还要写一个有关Tomcat安装和作用的博客园
完成上述内容需要一个j较高版本的jdk,MyEclipse10自带的jdk1.6我无法执行出结果用的是自己配置变量时的jdk1.8.
Java web感觉学了Java连门都没入,不会操作数据库,不会写html,没有连接。。。。。。。各种问题都有。
希望以后每星期都有两个小时来学习Java web开发。