赞
踩
创建一个新的maven,选中maven的webapp,然后创建
新建项目结构
连接数据库并且创建实体类
配置pom.xml文件信息
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies>
配置数据库链接驱动db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8
user=root
password=root
写一个BaseDao来获取数据库的连接信息
public class BaseDao { private static String driver; private static String url; private static String user; 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"); user=properties.getProperty("user"); password=properties.getProperty("password"); } //获取数据库的连接 public static Connection getConnection(){ Connection connection=null; try{ Class.forName(driver); connection= DriverManager.getConnection(url,user,password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return connection; } //编写查询的公共类 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++) { preparedStatement.setObject(i + 1, params[i]);//占位符是从1开始的 } resultSet = preparedStatement.executeQuery(sql); 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]);//占位符是从1开始的 } int updateRows=preparedStatement.executeUpdate(); return updateRows; } //释放资源 public static boolean closeResources(Connection connection, ResultSet resultSet, PreparedStatement preparedStatement){ boolean flag=true; //回收资源 if(resultSet!=null){ try{ resultSet.close(); } catch (SQLException e) { e.printStackTrace(); flag=false; } } if(connection!=null){ try{ connection.close(); } catch (SQLException e) { e.printStackTrace(); flag=false; } } if(preparedStatement!=null){ try{ preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); flag=false; } } return flag; }
过滤器characterEndingFile
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
chain.doFilter(request,response);
}
配置web.xml文件
<!-- 字符编码过滤器-->
<filter>
<filter-name>characterEndingFile</filter-name>
<filter-class>com.kong.filter.characterEndingFile</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEndingFile</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
UserDao
public interface UserDao {
//得到需要的用户
public User getLoginUser(Connection connection,String userCode) throws SQLException;
}
UserDaoImpl
ublic class UserDaoImpl implements UserDao { public User getLoginUser(Connection connection, String userCode) throws SQLException { PreparedStatement pstm=null; ResultSet rs=null; User user=null; if(connection!=null) { String sql = "select * from smbms_user where userCode=?"; Object[] params = { userCode}; rs = BaseDao.execute(connection, pstm, rs, sql, params); if (rs.next()) { user
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。