1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| package Dao;
import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate;
public class StudentJDBCTemplate implements TestuserDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject;
public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); }
public void create(String name, int age) { String SQL = "insert into testuser (name,age) values (?,?)"; jdbcTemplateObject.update(SQL,name,age); System.out.println("Name:" + name + "Age:" + age); return; }
public Student getStudent(int id) { String SQL = "select * from testuser where id = ?"; Student student = jdbcTemplateObject.queryForObject(SQL, new Object[] {id},new StudentMapper()); return student; }
public List<Student> listStudents() { String SQL = "select * from testuser"; List <Student> students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; }
public void delete(int id) { String SQL = "delete from testuser where id = ?"; jdbcTemplateObject.update(SQL, id); System.out.println("Delete id:" + id); return; }
public void update(int id, int age) { String SQL = "update testuser set age = ? where id = ?"; jdbcTemplateObject.update(SQL,age,id); System.out.println("Update id:" + id); return; }
@Override public void setDaotaSource(DataSource ds) { }
}
|