<?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.xsd">

 

 

    <bean id="dataSource"

       class="org.springframework.jdbc.datasource.DriverManagerDataSource">

       <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />

       <!--DriverManagerDataSource方法中的 ↑setClassName -->

       <property name="url"

           value="jdbc:sqlserver://localhost:1433;database=java"></property>

       <!-- DriverManagerDataSource方法中的 ↑setUrl -->

       <property name="username" value="sa"></property>

       <!--DriverManagerDataSource方法中的 ↑setUsername -->

       <property name="password" value="passw0rd"></property>

       <!--DriverManagerDataSource方法中的setpassword -->

    </bean>

    <bean id="CustomerDAOJdbc" class="model.dao.CustomerDAOJdbc">

       <constructor-arg ref="dataSource"></constructor-arg>

    </bean>

    <bean id="CustomerService" class="model.CustomerService">

       <constructor-arg ref="CustomerDAOJdbc"></constructor-arg>

    </bean>

   

   

    <bean id="ProductDAOJdbc" class="model.dao.ProductDAOJdbc">

       <constructor-arg ref="dataSource"></constructor-arg>

    </bean>

    <bean id="ProductService" class="model.ProductService">

       <constructor-arg ref="ProductDAOJdbc"></constructor-arg>

    </bean>

</beans>



package model.dao;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

import javax.sql.DataSource;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import model.CustomerBean;

import model.CustomerDAO;

//DriverManagerDataSource的方式,使用DataSource取得Connection、不搜尋JNDI Server(lookup)

public class CustomerDAOJdbc implements CustomerDAO {

    //以下是JDBC的方式

//  private static final String URL = "jdbc:sqlserver://localhost:1433;database=java";

//  private static final String USERNAME = "sa";

//  private static final String PASSWORD = "passw0rd";

    //改為以下dataSource的方式

    private DataSource dataSource;

   

   

    public static void main(String[] args) {

//     CustomerDAO dao = new CustomerDAOJdbc();//原本的JDBC

       ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");

       //讀取beans.xml檔,ApplicationContext代表整個Stringxml物件

       CustomerDAO dao = (CustomerDAO) context.getBean("CustomerDAOJdbc");

       //從中取出被new好的CustomerDAOJdbc物件

       CustomerBean bean = dao.select("Babe");

       System.out.println(bean);

      

       boolean result = dao.update("E".getBytes(),

              "ellen@yahoo.com", new java.util.Date(0), "Ellen");

       System.out.println(result);

    }

    public CustomerDAOJdbc(DataSource dataSource){

       //DataSource參數的建構子,使beans.xml檔案可直接new出來DataSource物件,進行連線

       this.dataSource=dataSource;

    }

   

    private static final String SELECT = "select * from customer where custid=?";

    @Override

    public CustomerBean select(String custid) {

       CustomerBean result = null;

       Connection conn = null;

       PreparedStatement stmt = null;

       ResultSet rset = null;

       try {

//         conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);//原本的JDBC

           conn=dataSource.getConnection();

           stmt = conn.prepareStatement(SELECT);

           stmt.setString(1, custid);

           rset = stmt.executeQuery();

           if(rset.next()) {

              result = new CustomerBean();

              result.setCustid(rset.getString("custid"));

              result.setPassword(rset.getBytes("password"));

              result.setEmail(rset.getString("email"));

              result.setBirth(rset.getDate("birth"));

           }

       } catch (SQLException e) {

           e.printStackTrace();

       } finally {

           if (rset!=null) {

              try {

                  rset.close();

              } catch (SQLException e) {

                  e.printStackTrace();

              }

           }

           if (stmt!=null) {

              try {

                  stmt.close();

              } catch (SQLException e) {

                  e.printStackTrace();

              }

           }

           if (conn!=null) {

              try {

                  conn.close();

              } catch (SQLException e) {

                  e.printStackTrace();

              }

           }

       }

       return result;

    }

   

    private static final String UPDATE =

           "update customer set password=?, email=?, birth=? where custid=?";

    @Override

    public boolean update(byte[] password,

           String email, java.util.Date birth, String custid) {

       try(//Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);//原本的JDBC

              Connection conn=dataSource.getConnection();

              PreparedStatement stmt = conn.prepareStatement(UPDATE);) {

          

           stmt.setBytes(1, password);

           stmt.setString(2, email);

           if(birth!=null) {

              long time = birth.getTime();

              stmt.setDate(3, new java.sql.Date(time));

           } else {

              stmt.setDate(3, null);

           }

           stmt.setString(4, custid);

           int i = stmt.executeUpdate();

           if(i==1) {

              return true;

           }

       } catch (SQLException e) {

           e.printStackTrace();

       }

       return false;

    }

}



package model;

 

import java.util.Arrays;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.AbstractXmlApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import model.dao.CustomerDAOJdbc;

 

public class CustomerService {

    // private CustomerDAO customerDao = new CustomerDAOJdbc();

    private CustomerDAO customerDao;

 

    public static void main(String[] args) {

       // CustomerService customerService = new CustomerService();

       ApplicationContext context = new ClassPathXmlApplicationContext(

              "beans.xml");

       CustomerService customerService = (CustomerService) context

              .getBean("CustomerService");

       CustomerBean bean = customerService.login("Babe", "B");

       System.out.println(bean);

 

       customerService.changPassword("Ellen", "E", "ABC");

 

    }

 

    public CustomerService(CustomerDAO customerDao) {

       this.customerDao = customerDao;

    }

 

    public boolean changPassword(String username, String oldPassword,

           String newPassword) {

       CustomerBean bean = this.login(username, oldPassword);

       if (bean != null) {

           byte[] temp = newPassword.getBytes(); // ϥΪ̿J

           return customerDao.update(temp, bean.getEmail(), bean.getBirth(),

                  username);

       }

       return false;

    }

 

    public CustomerBean login(String username, String password) {

       CustomerBean bean = customerDao.select(username);

       if (bean != null) {

           if (password != null && password.length() != 0) {

              byte[] pass = bean.getPassword(); // ��Ʈw��X

              byte[] temp = password.getBytes(); // ϥΪ̿J

              if (Arrays.equals(pass, temp)) {

                  return bean;

              }

           }

       }

       return null;

    }

}

 

創作者介紹

學習筆記專區

乙方 發表在 痞客邦 PIXNET 留言(0) 人氣()