网站系统开发需要掌握的技术:
1. 网页上要显示内容,需要学学HTML
2. 光有文字还不行,还要考虑不同的颜色,布局,排版,如图做出这样页面效果,要学学基本的CSS
3. 有一些交互,比如修改了代码可以实时看到效果,得掌握Javascript
4. 直接写CSS太累,需要套用别人写好的,来得快,也比自己做出来的好看,那么就可以用用Bootstrap
5.这些都需要维护在数据库里面,那就学一个简单的MySQL
6. 怎么表之间的关系~,彼此如何依赖,外键如何关联,这个需要学习表关系的相关知识
7. 有了数据库,也有了表,需要用到JDBC从这些表里取数据
8. 需要如下JAVA基础:面向对象,变量 操作符 控制流程,数字与字符串,异常处理,集合
9. 需要用到JSP
10. 为了JSP可以运行,需要一个Servlet容器,TOMCAT
课堂测试源代码:
连接数据库:
package com.user.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.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
String user="****";
String password="****";
String url = "jdbc:mysql://localhost:3306/user_msg";
Connection connection=null;
try {
connection=DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection)
{
try {
connection.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement)
{
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void close(ResultSet resultSet)
{
try {
resultSet.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
异常:
package com.user.msg.Util; public class UserException extends RuntimeException{ public UserException() { super(); // TODO Auto-generated constructor stub } public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); // TODO Auto-generated constructor stub } public UserException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public UserException(String message) { super(message); // TODO Auto-generated constructor stub } public UserException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } }
user:
package com.user.msg.model; public class User { private int id; private String username; private String passw0rd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassw0rd() { return passw0rd; } public void setPassw0rd(String passw0rd) { this.passw0rd = passw0rd; } }
接口:
package com.user.msg.dao; import com.user.msg.model.User; public interface IUserDao { public void login(User user); public void add(User user); }
UserDaoImpl:
package com.user.msg.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.user.msg.Util.DBUtil; import com.user.msg.Util.UserException; import com.user.msg.model.User; public class UserDaoImpl implements IUserDao{ @Override public void login(User user) { //Connection connection=DBUtil.getConnection(); // String sql = "select count(*) from t_user where username = ?"; //PreparedStatement preparedStatement = null; //ResultSet resultSet = null; //try { //preparedStatement = connection.prepareStatement(sql); //preparedStatement.setString(1, user.getUsername()); //resultSet = preparedStatement.executeQuery(); //while(resultSet.next()) { //if (resultSet.getInt(1) <=0) { //throw new UserException("用户不存在") ; //} //} //String sql = "select username from user_login where username = ?"; //preparedStatement = connection.prepareStatement(sql); // preparedStatement.setString(1, user.getUsername()); // resultSet = preparedStatement.executeQuery(); // while(resultSet.next()) { // if(resultSet.getString("username").equals(user.getUsername())) // { // System.out.println("登陆成功!"); // } // } // // } catch (SQLException e) { // // TODO 自动生成的 catch 块 // e.printStackTrace(); // }finally { // //关闭资源 // DBUtil.close(resultSet); // DBUtil.close(preparedStatement); // DBUtil.close(connection); // } // } public void add(User user) { //获得链接对象 Connection connection = DBUtil.getConnection(); //准备sql语句 String sql = "select count(*) from user_login where username = ?"; //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getUsername()); //接收结果集 resultSet = preparedStatement.executeQuery(); //遍历结果集 while(resultSet.next()) { if (resultSet.getInt(1) > 0) { throw new UserException("用户已存在") ; } } sql = "insert into user_login(username,password) value (?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getUsername()); preparedStatement.setString(2, user.getPassw0rd()); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //关闭资源 DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } } }
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> <title>登录</title> </head> <body "> <%//=request.getAttribute("error") %> <center style="color:red ; font-family:宋体;font-size:30px">登录</center> <form action="login2.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> <tr align="center"> <td colspan="2"> <input type="submit" value="登录" /> <a href="addInput.jsp">点击注册</a><br> </td> </tr> </table> <br> <center> <%=request.getAttribute("error") %></center> </form> </body> </html>
<%@page import="com.user.msg.Util.UserException"%> <%@page import="com.user.msg.dao.UserDaoImpl"%> <%@page import="com.user.msg.model.User"%> <%@page import="java.sql.Connection"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.ResultSet"%> <%@page import="com.user.msg.Util.DBUtil"%> <%@ 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> <body > <% //接收客户端传递过来的参数 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.setPassw0rd(password); UserDaoImpl userDao = new UserDaoImpl(); Connection connection=DBUtil.getConnection(); PreparedStatement preparedStatement = null; ResultSet resultSet = null; String sql = "select * from user_login where username = ?"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getUsername()); resultSet = preparedStatement.executeQuery(); if(resultSet.next()) { /* if(password.equals(rs.getString(2))) { } */ if(resultSet.getString("password").equals(password) ){ %> <br> <br> <br> <center style="color:red ; font-family:华文彩云;font-size:20px">登录成功!</center> <% //response.sendRedirect("loginsuccess.jsp"); //out.print("<script language='javaScript'> alert('登陆成功');</script>"); // response.setHeader("refresh", "0;url=login.jsp"); } else{ %> <br> <br> <br> <table align="center" border="1" width="500"> <tr> <td><center style="color:red ; font-family:华文彩云;font-size:20px">密码错误!</center></td> <tr> <td><center><a href="login.jsp">重新登录</a></center></td> </table> <% // out.print("<script language='javaScript'> alert('密码错误');</script>"); // response.setHeader("refresh", "0;url=login.jsp"); } } else { %> <br> <br> <br> <center style="color:red ; font-family:华文彩云;font-size:20px">账号错误!</center> <center><a href="login.jsp">重新登录</a></center> <% //out.print("<script language='javaScript'> alert('账号错误——else');</script>"); //response.setHeader("refresh", "0;url=login.jsp"); } %> </body> </html>
注册(添加)
<%@page import="com.user.msg.Util.UserException"%> <%@page import="com.user.msg.dao.UserDaoImpl"%> <%@page import="com.user.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> <body > <% //接收客户端传递过来的参数 String username = request.getParameter("username"); String password = request.getParameter("password"); if(username == null || "".equals(username.trim())){ request.setAttribute("error", "用户名不能为空"); %> <jsp:forward page="addInput.jsp"></jsp:forward> <% } User user = new User(); user.setUsername(username); user.setPassw0rd(password); UserDaoImpl userDao = new UserDaoImpl(); try{ userDao.add(user); %> <br> <br> <br> <br> <center>注册成功!!<br></center>> <center><a href="addInput.jsp">继续注册</a><br></center> <center><a href="login.jsp">返回登录界面</a></center> <% }catch(UserException e){ %> <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2> <% } %> </body> </html>
<%@ 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 >
<%=request.getAttribute("error") %>
<form action="add.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>
<tr align="center">
<td colspan="2">
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</body>
</html>
运行结果截图
这门课的希望和目标:
希望能够在学完这门课后,自己能够独立的完成一个具有比较完整功能的Javaweb系统。
周一周二晚上有课,一个小时,周三四五每天晚上两个小时,周六一天上课学习,周末看作业情况