项目如何搭建?
考虑使用不使用Maven?依赖, Jar
项目搭建
-
搭建一个maven web项目
-
配置Tomcat
-
测试项目是否能够跑起来
-
导入项目中会遇到的jar包;
jsp,Servlet,mysq|驱动,jstl, . -
创建项目包结构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-i8Fh3GmD-1659278865736)(E:\KangStudy\SMBMS.assets\1659272982306.png)]
-
编写实体类;
-
数据库配置文件
driver=com.mysql.jdbc.Driver #在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8 url=jdbc:musql://localhost:3306/smbms?useSSL=false&useServerPrepStmts=true&characterEncoding=utf-8 username=root password=1234
-
编写数据库的公共类
package com.yang.dao; import java.io.FileInputStream; import java.io.IOException; import java.sql.*; import java.util.Properties; public class BaseDao { private static String driver; private static String url; private static String username; private static String password; //静态代码块,类加载的时候就初始化了 static { Properties proper = new Properties(); try { //通过类加载器读取对应的资源 proper.load(new FileInputStream("db.properties")); } catch (IOException e) { e.printStackTrace(); } driver = proper.getProperty("driver"); url = proper.getProperty("url"); username = proper.getProperty("username"); password = proper.getProperty("password"); } //获取数据库的链接 public static Connection getConnecton() { Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } return conn; } //编辑查询公共类 public static ResultSet execute(Connection connection, String sql, Object[] params, ResultSet resultSet, PreparedStatement preparedStatement) throws SQLException { //预编译的sql,在后面直接执行就可以了 preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < params.length; i++) { //setObject,占位符从1开始,但是我们的数组是从0开始! preparedStatement.setObject(i + 1, params[i]); } resultSet = preparedStatement.executeQuery(); return resultSet; } public static int execute(Connection connection, String sql, Object[] params, PreparedStatement preparedStatement) throws SQLException { preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < params.length; i++) { preparedStatement.setObject(i + 1, params[i]); } int count = preparedStatement.executeUpdate(); return count; } //释放资源 public static boolean closeRresources(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) { boolean flag = true; if (resultSet != null) { try { resultSet.close(); //GC回收 resultSet = null; } catch (SQLException e) { e.printStackTrace(); flag = false; } } if (preparedStatement!=null){ try { preparedStatement.close(); preparedStatement=null; } catch (SQLException e) { e.printStackTrace(); flag=false; } } if (connection != null) { try { connection.close(); connection = null; } catch (SQLException e) { e.printStackTrace(); flag = false; } } return flag; } }
-
编写字符编码过滤器
package com.yang.filter; import javax.servlet.*; import java.io.IOException; /** * //字符码过滤器 * @author Mr.Yang */ public class CharacterEncodingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); chain.doFilter(request,response); } @Override public void destroy() { } }
-