首页 > 代码库 > Spring JDBC Example
Spring JDBC Example
1.准备数据库(sqlserver2008)
打开SQL Server 配置管理器进行网络配置
2.重启系统服务
3.创建数据库及表
4.编写模型及业务代码
public class Student { public int ID; public String UserName; public String Password; public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getUserName() { return UserName; } public void setUserName(String userName) { UserName = userName; } public String getPassword() { return Password; } public void setPassword(String password) { Password = password; } }
public interface StudentDAO { public void setDataSource(DataSource ds); public Student getStudent(Integer id); public List<Student> listStudents(); void delete(Integer id) ; void update(Integer id, String password); } public class StudentDaoImp implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); } public Student getStudent(Integer id) { String SQL = "select * from Student where id = ?"; Student student = jdbcTemplateObject.queryForObject(SQL, new Object[]{id}, new StudentMapper()); return student; } public List<Student> listStudents() { String SQL = "select * from Student"; List <Student> students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; } public void delete(Integer id) { String SQL = "delete from Student where id = ?"; jdbcTemplateObject.update(SQL, id); System.out.println("Deleted Record with ID = " + id ); return; } public void update(Integer id, String password){ String SQL = "update Student set Password = ? where id = ?"; jdbcTemplateObject.update(SQL, password, id); System.out.println("Updated Record with ID = " + id ); return; } }
public class StudentMapper implements RowMapper<Student> { public Student mapRow(ResultSet rs, int rowNum) throws SQLException { Student student = new Student(); student.setID(rs.getInt("id")); student.setUserName(rs.getString("UserName")); student.setPassword(rs.getString("Password")); return student; } }
5.配置spring容器
<?xml version = "1.0" encoding = "UTF-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name = "driverClassName" value = "com.microsoft.sqlserver.jdbc.SQLServerDriver"/> <property name = "url" value = "jdbc:sqlserver://localhost:1433;DatabaseName=Qxun"/> <property name = "username" value = "sa"/> <property name = "password" value = "6665508a"/> </bean> <bean id = "studentDaoImp" class = "ttyouni.dao.StudentDaoImp"> <property name = "dataSource" ref = "dataSource" /> </bean> </beans>
6.调用层
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); StudentDaoImp studentJDBCTemplate = (StudentDaoImp)context.getBean("studentDaoImp"); List<Student> list=studentJDBCTemplate.listStudents();
Spring JDBC Example
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。