public class StudentManager
{
private JdbcTemplate jdbcTemplate;
private static StudentManager instance = new StudentManager();
public static StudentManager getInstance()
{
return instance;
}
public JdbcTemplate getJdbcTemplate()
{
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
{
this.jdbcTemplate = jdbcTemplate;
}
}
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <!-- 驱动包名 --> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <!-- 数据库地址 --> <property name="url" value="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8;" /> <!-- 用户名 --> <property name="username" value="root" /> <!-- 密码 --> <property name="password" value="root" /> <!-- 最大连接数量 --> <property name="maxActive" value="150" /> <!-- 最小空闲连接 --> <property name="minIdle" value="5" /> <!-- 最大空闲连接 --> <property name="maxIdle" value="20" /> <!-- 初始化连接数量 --> <property name="initialSize" value="30" /> <!-- 连接被泄露时是否打印 --> <property name="logAbandoned" value="true" /> <!-- 是否自动回收超时连接 --> <property name="removeAbandoned" value="true" /> <!-- 超时等待时间(以秒为单位) --> <property name="removeAbandonedTimeout" value="10" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="studentManager" class="com.xrq.jdbc.StudentManager" factory-method="getInstance"> <property name="jdbcTemplate" ref="jdbcTemplate" /> </bean> </beans>
public static void main(String[] args)
{
ApplicationContext ac =
new ClassPathXmlApplicationContext("jdbc.xml");
System.out.println(StudentManager.getInstance());
System.out.println(StudentManager.getInstance().getJdbcTemplate());
}
// 添加学生信息
public boolean addStudent(Student student)
{
try
{
jdbcTemplate.update("insert into student values(null,?,?,?)",
new Object[]{student.getStudentName(), student.getStudentAge(), student.getStudentPhone()},
new int[]{Types.VARCHAR, Types.INTEGER, Types.VARCHAR});
return true;
}
catch (Exception e)
{
return false;
}
}
// 根据Id删除单个学生信息
public boolean deleteStudent(int id)
{
try
{
jdbcTemplate.update("delete from student where studentId = ?", new Object[]{id}, new int[]{Types.INTEGER});
return true;
}
catch (Exception e)
{
return false;
}
}
// 根据Id更新指定学生信息
public boolean updateStudent(int Id, Student student)
{
try
{
jdbcTemplate.update("update student set studentName = ?, studentAge = ?, studentPhone = ? where studentId = ?",
new Object[]{student.getStudentName(), student.getStudentAge(), student.getStudentPhone(), Id},
new int[]{Types.VARCHAR, Types.INTEGER, Types.VARCHAR, Types.INTEGER});
return true;
}
catch (Exception e)
{
return false;
}
}
// 根据学生Id查询单个学生信息
public Student getStudent(int id)
{
try
{
return (Student)jdbcTemplate.queryForObject("select * from student where studentId = ?",
new Object[]{id}, new int[]{Types.INTEGER}, new RowMapper<Student>(){
public Student mapRow(ResultSet rs, int arg1) throws SQLException
{
Student student = new Student(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4));
return student;
}
});
}
// 根据Id查询学生信息抛异常, 不管什么原因, 认为查询不到该学生信息, 返回null
catch (DataAccessException e)
{
return null;
}
}
// 查询所有学生信息
public List<Student> getStudents()
{
List<Map<String, Object>> resultList = jdbcTemplate.queryForList("select * from student");
List<Student> studentList = null;
if (resultList != null && !resultList.isEmpty())
{
studentList = new ArrayList<Student>();
Map<String, Object> map = null;
for (int i = 0; i < resultList.size(); i++)
{
map = resultList.get(i);
Student student = new Student(
(Integer)map.get("studentId"), (String)map.get("studentName"),
(Integer)map.get("studentAge"), (String)map.get("studentPhone")
);
studentList.add(student);
}
}
return studentList;
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 驱动包名 -->
<property name="driverClassName" value="${mysqlpackage}" />
<!-- 数据库地址 -->
<property name="url" value="${mysqlurl}" />
<!-- 用户名 -->
<property name="username" value="${mysqlname}" />
<!-- 密码 -->
<property name="password" value="${mysqlpassword}" />
<!-- 最大连接数量 -->
<property name="maxActive" value="150" />
<!-- 最小空闲连接 -->
<property name="minIdle" value="5" />
<!-- 最大空闲连接 -->
<property name="maxIdle" value="20" />
<!-- 初始化连接数量 -->
<property name="initialSize" value="30" />
<!-- 连接被泄露时是否打印 -->
<property name="logAbandoned" value="true" />
<!-- 是否自动回收超时连接 -->
<property name="removeAbandoned" value="true" />
<!-- 超时等待时间(以秒为单位) -->
<property name="removeAbandonedTimeout" value="10" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="studentManager" class="com.xrq.jdbc.StudentManager" factory-method="getInstance">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
</beans>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有