JDBC基础2之DAO
在说DAO之前我们首先讲一下结果集,前面提到的都是DML的操作也就是(增删改),而当我们查询操作将会返回很多数据表格,这时我们应该用结果集来接收返回的数据
Statement接口中有个方法:ResultSet executeQuery(String sql);//执行DQL操作
ResultSet 接口:查询的结果
boolean next():试图把光标往下移动一行,移动成功后返回true,否则返回false
Xxx getXxx(int columnindex):根据列的索引取值,索引值从1开始
Xxx getXxx(String columnName);根据列的名称取值
我们为什么要有DAO?
因为在取数据库的时候,我们会出现很多代码的重复,这时我们就有DAO的出现
DAO的一般开发步骤:
1.建立项目
2.开发domain组件(参照数据库的表)
3.编写DAO规范(接口)
4.针对接口写实现类
public class StudentDAOImpl implements IStudentDAO {
//insert into t_student(name,age) values('?',?);
public void save(Student stu) {
Connection conn = null;
Statement st = null;
StringBuilder sb = new StringBuilder(60);
sb.append("INSERT INTO t_student(name,age) VALUES(");
sb.append("'").append(stu.getName()).append("',");
sb.append(stu.getAge()).append(")");
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jdbcdemo?useSSL=false",
"root", "admin");
st = conn.createStatement();
st.executeUpdate(sb.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (Exception e2) {
e2.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
public void delete(Long id) {
Connection conn = null;
Statement st = null;
String sql = "DELETE FROM t_student WHERE id ="+id;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jdbcdemo?useSSL=false",
"root", "admin");
st = conn.createStatement();
st.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (Exception e2) {
e2.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
//UPDATE t_student SET name='?', age=? WHERE ID=?
public void update(Student stu) {
Connection conn = null;
Statement st = null;
StringBuilder sb = new StringBuilder(60);
sb.append("UPDATE t_student SET name=");
sb.append("'").append(stu.getName()).append("', age = ");
sb.append(stu.getAge()).append(" WHERE ID = ").append(stu.getId());
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jdbcdemo?useSSL=false",
"root", "admin");
st = conn.createStatement();
System.out.println(sb);
st.executeUpdate(sb.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (Exception e2) {
e2.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
public Student get(Long id) {
Student stu = null;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
String sql = "SELECT * FROM t_student WHERE id = " + id;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jdbcdemo?useSSL=false",
"root", "admin");
st = conn.createStatement();
rs = st.executeQuery(sql);
if (rs.next()) {
id = rs.getLong("id");
String name = rs.getString("name");
Integer age = rs.getInt("age");
stu = new Student(id, name, age);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e2) {
e2.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (Exception e2) {
e2.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
return stu;
}
public List<Student> list() {
List<Student> list = new ArrayList<>();
Connection conn = null;
Statement st = null;
ResultSet rs = null;
String sql = "SELECT * FROM t_student";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jdbcdemo?useSSL=false",
"root", "admin");
st = conn.createStatement();
rs = st.executeQuery(sql);
while (rs.next()) {
long id = rs.getLong("id");
String name = rs.getString("name");
Integer age = rs.getInt("age");
Student stu = new Student(id, name, age);
list.add(stu);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e2) {
e2.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (Exception e2) {
e2.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
return list;
}
}
5.编写测试方法
最后讲一下DAO的编写规范