一,项目搭建
1.搭建一个maven web项目
2.配置tomcat
3.测试项目是否能跑器来
4.配置pom.xml
jsp servlet mysql-connector-java jstl standard
5创建项目包结构
6.编写实体类
ORM映射
7.编写基础公共类
数据库配置文件db.properties
driver = com.mysql.jdbc.Driver url =jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8 username = root password = root
编写dao里面的公共类 package com.sqt.dao; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Parameter; 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 properties = new Properties(); //通过类加载器读取资源 InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties"); try { properties.load(is); } catch (IOException e) { e.printStackTrace(); } driver =properties.getProperty("driver"); url =properties.getProperty("url"); username =properties.getProperty("root"); password = properties.getProperty("password"); } //获取数据库的链接 public static Connection getConnection() { Connection connection=null; try { Class.forName("driver"); connection = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } return connection; } //编写查询公共类(预编译的sql不用传参) public static ResultSet excute(Connection connection, String sql, Object[] params,ResultSet rs,PreparedStatement preparedStatement) throws SQLException { preparedStatement = connection.prepareStatement(sql); for (int i=0;i <params.length;i++){ //setOject是从1开始,而我们的数组是从0开始 preparedStatement.setObject(i+1,params[i]); } rs = preparedStatement.executeQuery(); return rs; } //编写增删改的公共区别 public static int excute(Connection connection, String sql, Object[] params,PreparedStatement preparedStatement) throws SQLException { preparedStatement = connection.prepareStatement(sql); for (int i=0;i <params.length;i++){ //setOject是从1开始,而我们的数组是从0开始 preparedStatement.setObject(i+1,params[i]); } int updateRows = preparedStatement.executeUpdate(); return updateRows; } //释放资源,关闭链接 public static boolean closeResource(Connection connection,ResultSet rs,PreparedStatement preparedStatement){ boolean flag =true; if(rs!=null){ try { rs.close(); //Gc回收 rs =null; } catch (SQLException e) { e.printStackTrace(); flag =false; } } if(connection!=null){ try { connection.close(); //Gc回收 connection =null; } catch (SQLException e) { e.printStackTrace(); flag =false; } } if(preparedStatement!=null){ try { preparedStatement.close(); //Gc回收 preparedStatement =null; } catch (SQLException e) { e.printStackTrace(); flag =false; } } return flag; } } 编写编码过滤器
package com.sqt.filter; import javax.servlet.*; import java.io.IOException; /** * @author mypc */ public class CharacterEncodingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding("utf-8"); servletResponse.setCharacterEncoding("utf-8"); servletResponse.setContentType("text/html;charset=utf-8"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } } web.xml文件
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>com.sqt.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
8.导入静态资源