Employee.java
package com.springJdbc.insert;
public class Employee {
private int eId;
private String eName;
private Float eSal;
public Employee()
{
}
public Employee(int id, String name, Float sal) {
this.eId = id;
this.eName = name;
this.eSal = sal;
}
getters & Setters
}
あるJdbcEmpDAOImpl
package com.springJdbc.insert;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.Row Mapper;
import org.springframework.web.servlet.tags.EscapeBodyTag;
public class JdbcEmpDAOImpl {
//private DataSource ds;
private JdbcTemplate jt;
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
public Employee getEmpById(int id)
{
String sql = "select * from emp where ENO = ?";
//jt=new JdbcTemplate(ds);
@SuppressWarnings("unchecked")
Employee e = (Employee)jt.queryForObject(sql,new Object[] {id},new EmployeeRowMapper());
return e;
}
}
EmployeeRpwマッパー
package com.springJdbc.insert;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class EmployeeRowMapper implements RowMapper{
public Object mapRow(ResultSet rs,int rowNmu) throws SQLException
{
Employee e = new Employee();
e.seteId(rs.getInt("ID"));
e.seteName(rs.getString("name"));
e.seteSal(rs.getFloat("sal"));
return e;
}
}
applicationContext.xmlを
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<bean id = "empDAO" class = "com.springJdbc.insert.JdbcEmpDAOImpl">
<property name="jt" ref= "myJdbcTemplate"></property>
</bean>
<bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="ds"/>
</bean>
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="system"/>
<property name="password" value="manager"/>
</bean>
</beans>
JdbcDemo.java
package com.springJdbc.insert;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
public class jdbcDemo{
public static void main(String[] args) throws Exception{
ApplicationContext ctx=new ClassPathXmlApplicationContext("appCntxt.xml");
JdbcEmpDAOImpl jdbcEmpl = (JdbcEmpDAOImpl) ctx.getBean("empDAO");
// Employee employee3 = new Employee(101,"Balu",300000.06f);
//jdbcEmpl.insert(employee3);
int a=102;
try{
Employee e = jdbcEmpl.getEmpById(103);
System.out.println("from db"+e.geteId());
}
catch(DataAccessException e)
{
e.printStackTrace();
}
// Employee employee4 = jdbcEmployeeDAO.findById(456);
// System.out.println(employee4);
// context.close();
}
}
あなたは完全なスタックトレースをしてください追加することはできますか? – Taylor