编写一个程序,采用JdbcTemplate创建一个数据表student,有一下字段: stuno(学号,主键,设为数据库自增),stuname(姓名),birthday(生日),gender(性别),score(成绩),编写一个程序,向student表中插入5条记录,记录数据自定,数据要合理、正确,将student表中最后一条记录的score的值改为377,查询出性别是女的生日在7月份的同学,分别采用基于XML的和基于Annotation方式的声明式事务管理,删除student表中所有score大于200的记录。若人为加入异常,查看student表中记录情况;若没有发生异常,观察student表中数据情况。
时间: 2024-03-17 14:45:30 浏览: 66
以下是一个使用 JdbcTemplate 创建 student 表、插入数据、修改数据、查询数据和删除数据的示例程序。同时,程序中也包含了基于 XML 和 Annotation 的两种声明式事务管理方式的示例代码。
注意:本示例代码仅供参考,实际项目中需要根据具体情况进行修改和调整。
```java
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
public class StudentManager {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public void createTable() {
String sql = "CREATE TABLE student (stuno INT AUTO_INCREMENT PRIMARY KEY, stuname VARCHAR(20), birthday DATE, gender VARCHAR(10), score INT)";
jdbcTemplate.execute(sql);
}
public void insertData() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String sql = "INSERT INTO student (stuname, birthday, gender, score) VALUES (?, ?, ?, ?)";
Object[] args = new Object[] { "张三", dateFormat.parse("1990-01-01"), "男", 90 };
jdbcTemplate.update(sql, args);
args = new Object[] { "李四", dateFormat.parse("1991-02-02"), "女", 80 };
jdbcTemplate.update(sql, args);
args = new Object[] { "王五", dateFormat.parse("1992-03-03"), "男", 85 };
jdbcTemplate.update(sql, args);
args = new Object[] { "赵六", dateFormat.parse("1993-04-04"), "女", 95 };
jdbcTemplate.update(sql, args);
args = new Object[] { "钱七", dateFormat.parse("1994-05-05"), "男", 88 };
jdbcTemplate.update(sql, args);
}
public void updateData() {
String sql = "UPDATE student SET score = ? WHERE stuno = (SELECT MAX(stuno) FROM student)";
jdbcTemplate.update(sql, 377);
}
public List<Student> queryData() {
String sql = "SELECT * FROM student WHERE gender = ? AND MONTH(birthday) = ?";
Object[] args = new Object[] { "女", 7 };
List<Student> students = jdbcTemplate.query(sql, args, new StudentRowMapper());
return students;
}
@Transactional
public void deleteData() {
String sql = "DELETE FROM student WHERE score > ?";
Object[] args = new Object[] { 200 };
jdbcTemplate.update(sql, args);
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentManager manager = (StudentManager) context.getBean("studentManager");
manager.createTable();
manager.insertData();
manager.updateData();
List<Student> students = manager.queryData();
for (Student student : students) {
System.out.println(student);
}
try {
manager.deleteData();
} catch (Exception e) {
e.printStackTrace();
}
students = manager.queryData();
for (Student student : students) {
System.out.println(student);
}
}
}
```
其中,Student 类用于封装 student 表中的一条记录:
```java
import java.util.Date;
public class Student {
private int stuno;
private String stuname;
private Date birthday;
private String gender;
private int score;
// 省略 getter 和 setter 方法
@Override
public String toString() {
return "Student [stuno=" + stuno + ", stuname=" + stuname + ", birthday=" + birthday + ", gender=" + gender
+ ", score=" + score + "]";
}
}
```
StudentRowMapper 类用于将查询结果映射到 Student 对象:
```java
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.springframework.jdbc.core.RowMapper;
public class StudentRowMapper implements RowMapper<Student> {
@Override
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Student student = new Student();
student.setStuno(rs.getInt("stuno"));
student.setStuname(rs.getString("stuname"));
student.setBirthday(rs.getDate("birthday"));
student.setGender(rs.getString("gender"));
student.setScore(rs.getInt("score"));
return student;
}
}
```
基于 XML 的声明式事务管理:
```xml
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="studentManagerPointcut" expression="execution(* StudentManager.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="studentManagerPointcut" />
</aop:config>
```
基于 Annotation 的声明式事务管理:
```xml
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
@Component
public class StudentManager {
@Transactional
public void deleteData() {
String sql = "DELETE FROM student WHERE score > ?";
Object[] args = new Object[] { 200 };
jdbcTemplate.update(sql, args);
}
}
```
阅读全文