Hibernate Jdbc

hibernate

import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
public class SimpleJdbcDaoImpl extends SimpleJdbcDaoSupport {
  public int getCircleCount() {
    String sql = "SELECT COUNT(*) FROM CIRCLE";
    return this.getJdbcTemplate().queryForInt(sql);
  }
}

public class JdbcDemo {
  public static void main(String[] args) {
    ApplicationContext ctx = ClassPathXmlApplicationContext("spring.xml");
    SimpleJdbcDaoImpl dao = ctx.getBean("simpleJdbcDaoImpl", SimpleJdbcDaoImpl.class);
    System.out.println(dao.getCircleCount());
  }
}

<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
  <property name="url" value="jdbc:derby://localhost:1527/db;create=true"/>
  <property name="initialSize" value="2"/>
  <property name="maxActive" value="5"/>
</bean>

<bean id="sessionFactory" 
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="packagesToScan" value="org.companyName.model"/>
  <property name="hibernateProperties">
    <props>
        <prop key="dialect">org.hibernate.dialect.DerbyDialect</prop>
    </props>
  </property>
</bean>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License