【Java开源代码栏目提醒】:网学会员为广大网友收集整理了,BookJDBCDAO.java,希望对大家有所帮助!
/* $Id$
*
* Copyright (c) DAKOSY AG and Riege Software. All rights reserved.
* Use is subject to license terms.
*/
package com.h3book.penguin.examples.dao.c1;
import java.io.IOException;
import java.io.Reader;
import java.sql.*;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowCallbackHandler;
import com.h3book.penguin.examples.domain.Book;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
/**
* Ge: Add docs
*
* @author <a href="mailto:3710167@qq.com">Jing Ge</a>
* @version $Revision$
*/
public class BookJDBCDAO {
Logger LOG = Logger.getLogger(BookJDBCDAO.class);
public Connection getConnection() {
String driver = "org.h
sqldb.jdbcDriver";
String url = "jdbc:hsqldb:hsql://localhost/";
String username = "sa";
String password = "";
try {
Class.forName(driver);
return DriverManager.getConnection(url, username, pass
word);
} catch (ClassNotFoundException e) {
LOG.error("JDBC driver not found!", e);
return null;
} catch (SQLException e) {
LOG.error("could not select the book's name", e);
return null;
}
}
public DataSource getDataSource() {
try {
return (DataSource) new InitialContext()
.lookup("JNDI path to the DataSource");
} catch (NamingException e) {
LOG.error("not DataSource is found!", e);
return null;
}
}
public Book getBookByOid(int oid) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
Book book = null;
try {
stmt = conn.prepareStatement("select OID, NAME, PUBLISH, DESCRIPTION " +
"from BOOK WHERE OID = ?");
stmt.setInt(1, oid);
rs = stmt.executeQuery();
if (rs.next()) {
book = new Book();
book.setOid(rs.getInt("OID"));
book.setName(rs.getString("NAME"));
book.setPublish(rs.getDate("PUBLISH"));
book.setDescription(rs.getString("DESCRIPTION"));
}
} catch (SQLException e) {
LOG.error("could not get all books", e);
return book;
} finally {
// close the statement
close(stmt);
// close the connection
close(conn);
}
return book;
}
public Book getBookByOidThroughIbatis(int oid){
// JDBC code for getting Book by the given oid
String resource = "pathTo/SqlMapConfig.xml";
Reader reader;
try {
reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
return (Book) sqlMapper.queryForObject("getBookByOid", oid);
} catch (IOException e) {
LOG.error("failt to read the SqlMapConfig.
xml file", e);
return null;
} catch (SQLException e) {
LOG.error("SQL Map with id 'getBookByOid' does not work correctly!", e);
return null;
}
}
public Book getBookByOidThroughSpring(int oid){
final Book book = new Book();
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
jdbcTemplate.query("select OID, NAME, PUBLISH, DESCRIPTION " +
"from BOOK WHERE OID = ?",
new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
ps.setInt(1, 2);
}
},
new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
book.setOid(rs.getInt("oid"));
book.setName(rs.getString("name"));
book.setPublish(rs.getDate("publish"));
book.setDescription(rs.getString("description"));
}
});
return book;
}
public void update(Book book) {
// JDBC code for updating entry in the Book table by the given book object.
}
public void delete(Book book) {
// JDBC code for deleting the entry in the Book table by the given book object.
}
// public List<Book> findByName(String name) {
// // JDBC code for querying the Book table with given book name
// }
private void close(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
LOG.warn("Satement could not be closed!", e);
}
}
}
private void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
LOG.warn("Connection could not be closed!", e);
}
}
}
}