[android]代码库
(1)Dao类
package org.gdufs.dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import org.gdufs.entity.Student;
import org.gdufs.util.DBHelper;
import org.gdufs.util.DBHelper2;
public class StudentDao {
public Student findStudentById(String studentId) {
Student student = null;
Connection con = null;
PreparedStatement stat = null;
con = DBHelper2.connect();
String sql1 = "select * from student where studentId=?";// and password=?";
try {
stat = con.prepareStatement(sql1);
stat.setString(1, studentId);
ResultSet rs = stat.executeQuery();
if (rs.next()) {
student = new Student();
student.setStudentId(studentId);
student.setStudentName(rs.getString("studentName"));
student.setPassword(rs.getString("password"));
student.setClassName(rs.getString("className"));
student.setEmail(rs.getString("email"));
student.setPhoto(rs.getString("photo"));
student.setTelephone(rs.getString("telephone"));
student.setSex(rs.getString("sex"));
student.setSchool(rs.getString("school"));
}
} catch (SQLException ex) {
System.out.println("dao异常"+ex.toString());
} finally {
DBHelper2.closePreparedStatement(stat);
DBHelper2.closeConneciton(con);
}
return student;
}
public List<Student> getStudents() {
List<Student> students = new ArrayList<Student>();
Student student = null;
Connection con = null;
PreparedStatement stat = null;
con = DBHelper2.connect();
String sql1 = "select * from student ";// and password=?";
try {
stat = con.prepareStatement(sql1);
ResultSet rs = stat.executeQuery();
while (rs.next()) {
student = new Student();
student.setStudentId(rs.getString("studentId"));
student.setStudentName(rs.getString("studentName"));
student.setPassword(rs.getString("password"));
student.setClassName(rs.getString("className"));
student.setEmail(rs.getString("email"));
student.setPhoto(rs.getString("photo"));
student.setTelephone(rs.getString("telephone"));
student.setSex(rs.getString("sex"));
student.setSchool(rs.getString("school"));
students.add(student);
}
} catch (SQLException ex) {
System.out.println("dao异常" + ex.toString());
} finally {
DBHelper2.closePreparedStatement(stat);
DBHelper2.closeConneciton(con);
}
return students;
}
}
[源代码打包下载]
by: 发表于:2018-05-31 11:01:08 顶(0) | 踩(0) 回复
??
回复评论