《Spring 2.0核心技术与最佳实践》第四天

 hsqldb文档(把数据以文件的形式保存下来的方法?)
HSQLDB是一个纯Java编写的数据库.
使用方法
1.导入hsqldb.jar  
    org.hsqldb.jdbcDriver
   jdbc:hsqldb:mem:bookstore
2. 编程  (org.hsqldb.Server;)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.hsqldb.Server;
public class HsqldbUtil {
    private static final String DROP_TABLE = "drop table Book if exists";
    private static final String CREATE_TABLE = "create table Book "
        + "(id varchar(36) not null primary key, "
        + "name varchar(100) not null, "
        + "author varchar(100) not null)";
    private static final String INSERT_BOOK1 = "insert into Book "
        + "(id, name, author) values "
        + "('12345678-abcd-effe-dcba-87654321', 'Thinking in Java', 'Bruce Eckel')";
    private static final String INSERT_BOOK2 = "insert into Book "
        + "(id, name, author) values "
        + "('abcdefed-cba9-8765-4321-01234567', 'JUnit in Action', 'Craig Walls')";
    private static Server server = null;
    public static void startDatabase() {
        server = new Server();
        server.setDatabaseName(0, "test");
        server.setDatabasePath(0, "mem:bookstore");
        server.setLogWriter(null);
        server.setErrWriter(null);
        server.start();
        try {
            Class.forName("org.hsqldb.jdbcDriver" );
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        Connection c = null;
        Statement st = null;
        try {
            c = DriverManager.getConnection("jdbc:hsqldb:mem:bookstore", "sa", "");
            st = c.createStatement();
            st.execute(DROP_TABLE);
            st.execute(CREATE_TABLE);
            st.execute(INSERT_BOOK1);
            st.execute(INSERT_BOOK2);
        }
        catch(SQLException e) {
            throw new RuntimeException(e);
        }
        finally {
            if(st!=null) {
                try {
                    st.close();
                }
                catch(SQLException e) {}
            }
            if(c!=null) {
                try {
                    c.close();
                }
                catch(SQLException e) {}
            }
        }
    }

}


解析:
Server
server.setDatabaseName,setDatabasePath,setLogWriter,setErrWriter,start
statement execute 通用drop ,create,insert


2使用spring连接Dao   <!-- 使用JdbcTemplate的DAO -->
最优
config.xml
<beans >
    <!-- 定义DataSource -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
        <property name="url" value="jdbc:hsqldb:mem:bookstore" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>
    <!-- 使用JdbcTemplate的DAO -->
    <bean id="jdbcTemplateBookDao" class="example.chapter5.JdbcTemplateBookDao">
        <property name="dataSource" ref="dataSource" />
    </bean>
  </beans>


JdbcTemplateBookDao.java(使用JdbcDaoSupport的getJdbcTemplet方法,query,update)
package example.chapter5;

import java.sql.*;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class JdbcTemplateBookDao extends JdbcDaoSupport implements BookDao {

 class BookRowMapper implements RowMapper {

  public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
   Book book = new Book();
   book.setId(rs.getString("id"));
   book.setName(rs.getString("name"));
   book.setAuthor(rs.getString("author"));
   return book;
  }
 }

 @SuppressWarnings("unchecked")
 public List<Book> queryAll() {
  return getJdbcTemplate().query("select * from Book",new BookRowMapper());
 }

 @SuppressWarnings("unchecked")
 public List<Book> queryByAuthor(String author) {
  return getJdbcTemplate().query("select * from Book where author=?",
    new Object[] { author }, new BookRowMapper());
 }

 public void create(Book book) {
  getJdbcTemplate()
    .update(
      "insert into Book (id, name, author) values (?, ?, ?)",
      new Object[] { book.getId(), book.getName(),
        book.getAuthor() });
 }

 public void update(Book book) {
  getJdbcTemplate()
    .update(
      "update Book set name=?, author=? where id=?",
      new Object[] { book.getName(), book.getAuthor(),
        book.getId() });
 }

 public void delete(String id) {
  getJdbcTemplate().update("delete from Book where id=?",
    new Object[] { id });
 }

}


使用了
 HsqldbUtil.startDatabase();//建hsql数据库,表,插入数据等
        ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
 BookDao bookDao = (BookDao) context.getBean("jdbcTemplateBookDao");
         
 接口BookDao
package example.chapter5;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
public interface BookDao {
    List<Book> queryAll();
    List<Book> queryByAuthor(String author);
    void create(Book book);
    void update(Book book);
    void delete(String id);
}
调用接口BookDao的这五个方法了....方便了,
Spring此种注入方法写法很简洁,也方便DB的连接的修改.

*3.练习 ssh对一个表进行添加,删除,更新,显示所有的操作

增加 Spring 框架
增加 Hibernate 框架
在配置选项中选择使用 Spring 的配置文件,
并在sessionFacotry ID处输入 sessionFactory

jdbc:mysql://localhost:3306/musichouse
com.mysql.jdbc.Driver
DAO session factory not selected

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值