下面演示向数据库Springtest中表tb_user中插入数据:
(1)通过xml实现DataSource(数据源注入)。
(2)使用jdbcTemplate访问数据。
(3)使用ORM工具访问数据。
以第一种方式介绍:
1、创建包cn.xc.entity.在该包下建立一个实体类StudentInfo.
类中定义对应数据库表中的属性。
代码如下:
package cn.xc.entity;
public class StudentInfo {
private int id;
private String name;
private int age;
private String score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
2、创建包cn.xc.dao。
在该包下建立接口StudentDao。
在该接口中定义用来向数据库表中插入数据的方法: addStudent().
代码如下:
package cn.xc.dao;
import cn.xc.entity.StudentInfo;
public interface StudentDao {
public void addStudent(StudentInfo student);
}
注:student是StudentInfo的实体对象。
3、创建包cn.xc.Impdao。在该包下创建类StudentDaoImpl并实现StudentDao接口。
代码如下:
package cn.xc.Impdao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import cn.xc.dao.StudentDao;
import cn.xc.entity.StudentInfo;
public class StudentDaoImpl implements StudentDao {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void addStudent(StudentInfo student) {
String name=student.getName();
int age=student.getAge();
String score=student.getScore();
Connection conn = null;
PreparedStatement ps = null;
try {
//获取数据库连接
conn = dataSource.getConnection();
//添加数据的SQL语句
String sql = “insert into tb_user(name,age,score) values(?,?,?)”;
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setInt(2, age);
ps.setString(3, score);
//执行SQL语句
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4、创建Spring的配置文件applicationContext.xml。
(1)定义数据源DataSource。它是Spring中的DriverManagerDataSource类的实例。
(2)配置类StudentDaoImpl。注入DataSource属性值。
5、创建包cn.xc.test。
在该包下创建测试类Test。
代码如下:
package cn.xc.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import cn.xc.dao.StudentDao;
import cn.xc.entity.StudentInfo;
public class Test {
public static void main(String[] args){
//装载配置文件
Resource resource = new ClassPathResource(“applicationContext.xml”);
BeanFactory factory = new XmlBeanFactory(resource);
//实例化StudentInfo对象
StudentInfo student = new StudentInfo();
student.setName(“李四”);
student.setAge(new Integer(22));
student.setScore(“98”);
//获取student
StudentDao studentDAO = ( StudentDao) factory.getBean(“student”);
//执行添加方法
studentDAO.addStudent(student);
System.out.println(“数据添加成功”);
}
}