第一步:Spring訪問數據庫(jdbcTemplate)


      首先固然是訪問數據庫,之前學習php的時候,就是從留言板、登陸等功能開始學習的,之因此這樣學習無非就是由於從前臺到後臺,該有的全都有了,完整而簡潔的生命週期。 php

       用的是jdbcTemplate,這仍是最近才瞭解到的。數據庫是Oracle 11g,剛好虛擬機裏裝了,因此就用上了。 java



public class PlanDao {

	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public Connection getConnection(){
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		   DataSource springDataSource = (DataSource)ctx.getBean("springDataSource");
		   JdbcTemplate jdbcTemplate = (JdbcTemplate)ctx.getBean("jdbcTemplate");
		   /*jdbcTemplate.setDataSource(springDataSource);   
		   setJdbcTemplate(jdbcTemplate);     */
		   DataSource dataSource = jdbcTemplate.getDataSource();
		   Connection conn = null;
		   try {
		    conn = dataSource.getConnection();
		   } catch (SQLException e) {
		    e.printStackTrace();
		   }
		   return conn;
	}
	
	public ResultSet queryPersons(String sql){
		   ResultSet rs = null;
		   try {
		    Connection connection = getConnection();
		    Statement st = connection.createStatement();
		    rs = st.executeQuery(sql);
		   } catch (SQLException e) {
		    e.printStackTrace();
		   }
		   return rs;
		} 
	
}



代碼是複製別人的,倒也簡潔明瞭,只是getConnection()方法第一行,本來複制來的時候ctx是用ApplicationContext定義的,可是ApplicationContext好像並非後來new的對象的父類,報了錯,因此修改了這麼一點地方。另外applicationContext.xml我是放在了src目錄下(Eclipse的動態網站工程)才試驗成功的,放WEB-INF下報了「找不到文件」之類的錯誤。 spring

applicationContext.xml以下: sql

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="springDataSource"
   class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName"
    value="oracle.jdbc.OracleDriver">
   </property>
   <property name="url"
    value="jdbc:oracle:thin:@192.168.1.152:1521:orcl">
   </property>
   <property name="username" value="plan"></property>
   <property name="password" value="admin"></property>
</bean>

<bean id="jdbcTemplate"
   class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
   lazy-init="false" autowire="default" dependency-check="default">
   <property name="dataSource">
    <ref bean="springDataSource" />
   </property>
</bean>
</beans>



用來測試的main方法類以下 數據庫

public class Main {
	public static void main(String[] args) {
		PlanDao pd = new PlanDao();
		ResultSet rs = pd.queryPersons("select * from plan");
		int n = 0;
		try {
			while (rs.next()) {
				n++;
				System.out.println("id     = " + rs.getString(1));
				System.out.println("name   = " + rs.getString(2));
				System.out.println("gender = " + rs.getString(3));
				System.out.println("*******************************");
			}
			System.out.println("數據庫中共有記錄 " + n + " 條");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
成功訪問了數據庫。 代碼是複製的,因此貼上來了。這最初的代碼能夠測試一下。之後的代碼更願意貼圖上來,否則在瀏覽器和在線編輯器裏雙滾動條滾得麻煩得很。
相關文章
相關標籤/搜索