Spring JDBC 例子

Spring JDBC 例子

Spring JDBC 很好用。
-------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource"
  abstract="false" singleton="true" lazy-init="default"
  autowire="default" dependency-check="default">
 <property name="driverClassName"><value>oracle.jdbc.OracleDriver</value></property>
 <property name="url"><value>jdbc:oracle:thin:@10.240.144.11:1521:XYMES</value></property>
 <property name="username"><value>reel</value></property>
 <property name="password"><value>reel</value></property>
 </bean>
</beans>
------------------------------------
package com.kim.spring;
import java.util.List;

import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class Test
{

 /**
  * @param args
  */
 public void aMethod()throws Exception
 { 
  ApplicationContext ac=new FileSystemXmlApplicationContext("F:\\kim\\kimjob\\springkim\\src\\applicationContext.xml");
  DataSource dataSource=(DataSource)ac.getBean("dataSource");
  JdbcTemplate jt=new JdbcTemplate(dataSource);
  List l= jt.queryForList("select * from abc");
  System.out.println(l.get(0).toString());
  System.out.println(l.get(0));
  int i= jt.update("update abc set bb='8'");
  System.out.println(i);
  
 }
 public static void main(String[] args)
 {
  // TODO Auto-generated method stub
  Test t=new Test();
  try
  {
   t.aMethod();
  } catch (Exception e)
  {
   // TODO: handle exception
   e.printStackTrace();
  }
 }
}
---------------------------------
结果:
log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
{AA=2, BB=8, CC=2}
1

你可能感兴趣的:(Spring JDBC 例子)