-
配置环境
-
测试网页嫩不能跑起来
-
建包
pojo包 实体类
dao 操作数据库,持久化操作
service 业务
servlet
filter 过滤器
util 工具类
-
编写实体类--------------------------一个表就是一个实体类:序列化get,set一下
-
编写基础公共类
数据库配置文件 在resource下写一个file 写drive,url,username,password
在dao层用static初始化
public class User {
public static String username;
public static String password;
public static String driver;
public static String url;
static {
Properties properties = new Properties();
InputStream resourceAsStream = User.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
}
//获取数据库连接
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
//编写查询公共方法
public static ResultSet excute(Connection connection, String sql, Object[] params, PreparedStatement preparedStatement, ResultSet resultSet) throws Exception {
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
//index不能为0
preparedStatement.setObject(i + 1, params[i]);
}
resultSet = preparedStatement.executeQuery();
return resultSet;
}
//编写增删改的公共方法
public static int excute(Connection connection, String sql, Object[] params, PreparedStatement preparedStatement) throws Exception {
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
//index不能为0
preparedStatement.setObject(i + 1, params[i]);
}
int updateRows = preparedStatement.executeUpdate();
return updateRows;
}
//关闭连接
public static boolean closeSourse(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();
//GC回收
preparedStatement = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (