一個關於DAO設計的小小project

BookTypeDAO.java
package com.jianson.DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.jianson.DB.DBManager;
import com.jianson.ENTITY.BookType;
public class BookTypeDAO {
DBManager dbManager = new DBManager();
public boolean save(BookType bookType){
String sql =
"insert into bookType" +
" values" +
"(" + bookType.getBookTypeId() + "," +
" " + bookType.getParentId() + "," +
" '" + bookType.getBookName() + "'," +
" '" + bookType.getContext() + "'," +
" " + bookType.getIsDelete() + ")";
int row = dbManager.update(sql);
return (row == 1);
}
public boolean update(BookType bookType){
String sql =
"update booktype" +
" set" +
" bookName = '" + bookType.getBookName() + "'," +
" context = '" + bookType.getContext() + "'," +
" isDelete = " + bookType.getIsDelete() + "" +
" where booktypeID = " + bookType.getBookTypeId() + "";
System.out.println(sql);
int row = dbManager.update(sql);
return (row == 1);
}
public boolean delete(int id){
String sql =
"delete from bookInfo where" +
" bookTypeId = " + id;
int row = dbManager.update(sql);
return (row == 1);
}
public BookType findId(int id){
String sql = "select bookTypeId, parentId, bookName, context, isdelete from bookType where bookTypeId = " + id;
ResultSet rs = dbManager.query(sql);
BookType bookType = new BookType();
try {
if( rs.next()){
int i = 1;
bookType.setBookTypeId(rs.getInt(i ++));
bookType.setParentId(rs.getInt(i ++));
bookType.setBookName(rs.getString(i ++));
bookType.setContext(rs.getString(i ++));
bookType.setIsDelete(rs.getInt(i ++));
/* System.out.print(rs.getInt(i ++) + "");
System.out.print(rs.getString(i ++) + " ");
System.out.print(rs.getString(i ++) + " ");
System.out.print(rs.getString(i ++) + " ");
System.out.print(rs.getInt(i ++) + "");*/
}
} catch (SQLException e) {
e.printStackTrace();
}
return bookType;
}
public void findAll(){
String sql = "select bookTypeId, parentId, bookName, context, isdelete from bookType";
ResultSet rs = dbManager.query(sql);
BookType bookType = new BookType();
try {
while( rs.next()){
int i = 1;
bookType.setBookTypeId(rs.getInt(i ++));
bookType.setParentId(rs.getInt(i ++));
bookType.setBookName(rs.getString(i ++));
bookType.setContext(rs.getString(i ++));
bookType.setIsDelete(rs.getInt(i ++));
System.out.println(bookType);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

BookType.java
package com.jianson.ENTITY;
public class BookType {
private int bookTypeId;
private int parentId;
private String bookName;
private String context;
private int isDelete;
public BookType(){
super();
}
public BookType(int bookTypeId, int parentId, String bookName, String context, int isDelete){
super();
this.bookTypeId = bookTypeId;
this.parentId = parentId;
this.bookName = bookName;
this.context = context;
this.isDelete = isDelete;
}
public BookType(int parentId, String bookName, String context, int isDelete){
super();
this.parentId = parentId;
this.bookName = bookName;
this.context = context;
this.isDelete = isDelete;
}

public int getBookTypeId() {
return bookTypeId;
}
public void setBookTypeId(int bookTypeId) {
this.bookTypeId = bookTypeId;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public int getIsDelete() {
return isDelete;
}
public void setIsDelete(int isDelete) {
this.isDelete = isDelete;
}
@Override
public String toString() {
return "BookType [bookName=" + bookName + ", bookTypeId=" + bookTypeId
+ ", context=" + context + ", isDelete=" + isDelete
+ ", parentId=" + parentId + "]";
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((bookName == null) ? 0 : bookName.hashCode());
result = prime * result + bookTypeId;
result = prime * result + ((context == null) ? 0 : context.hashCode());
result = prime * result + isDelete;
result = prime * result + parentId;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BookType other = (BookType) obj;
if (bookName == null) {
if (other.bookName != null)
return false;
} else if (!bookName.equals(other.bookName))
return false;
if (bookTypeId != other.bookTypeId)
return false;
if (context == null) {
if (other.context != null)
return false;
} else if (!context.equals(other.context))
return false;
if (isDelete != other.isDelete)
return false;
if (parentId != other.parentId)
return false;
return true;
}
}

DBManager.java
package com.jianson.DB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBManager {
Connection con = null;
Statement sta = null;
ResultSet rs = null;
int row = 0;
/**
* 用來執行insert update delete
* @param sql 要執行的sql語句
* @return sql影響的行數
* @author
*/
public int update(String sql){
System.out.println(sql);
try {
//定義接口是用JDBC或者ODBC等等:返回與帶有給定字符串名的類或接口相關聯的 Class 對象
Class.forName(config.driver);
//得到連接:試圖建立到給定數據庫 URL 的連接
con = DriverManager.getConnection(config.url, config.user, config.pwd);
//創建一個 Statement 對象來將 SQL 語句發送到數據庫
sta = con.createStatement();
//statement接口實現executeUpdate方法執行sql語句
//row爲執行sql所影響的行數
row = sta.executeUpdate(sql);
System.out.println(row == 1);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
} finally {
this.close();
}
return 1;
}
public ResultSet query(String sql){
try {
//定義接口是用JDBC或者ODBC等等:返回與帶有給定字符串名的類或接口相關聯的 Class 對象
Class.forName(config.driver);
//得到連接:試圖建立到給定數據庫 URL 的連接
con = DriverManager.getConnection(config.url, config.user, config.pwd);
//創建一個 Statement 對象來將 SQL 語句發送到數據庫
sta = con.createStatement();
//statement接口實現executeUpdate方法執行sql語句
//rs爲執行sql所產生的結果集
rs = sta.executeQuery(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
}
return rs;
}
public void close(){
try {
if (rs !=null){
rs.close();
rs = null;
}
if (sta !=null){
sta.close();
sta = null;
}
if (con != null){
con.close();
con = null;
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
}


TestBookTypeDAO.java
package com.jianson.TEST;

import com.jianson.DAO.BookTypeDAO;
import com.jianson.ENTITY.BookType;

public class TestBookTypeDAO {
public static void main(String args[]){
TestBookTypeDAO testBookTypeDAO = new TestBookTypeDAO();
// testBookTypeDAO.testSave();
// testBookTypeDAO.testUpdate();
// testBookTypeDAO.testDelete();
testBookTypeDAO.testFindAll();
// testBookTypeDAO.testFindId();

}
public void testSave(){
BookType bookType = new BookType();
bookType.setBookTypeId(9);
bookType.setParentId(3);
bookType.setBookName("javaweb開發");
bookType.setContext("編程");
bookType.setIsDelete(0);
BookTypeDAO bookTypeDAO = new BookTypeDAO();
if( bookTypeDAO.save(bookType)){
System.out.println("數據添加成功");
}else{
System.out.println("數據添加失敗");
}
}
public void testUpdate(){
BookType bookType = new BookType();
BookTypeDAO bookTypeDAO = new BookTypeDAO();
bookType.setBookTypeId(9);
bookType.setParentId(3);
bookType.setBookName("javaweb");
bookType.setContext("java");
bookType.setIsDelete(0);
if(bookTypeDAO.update(bookType)){
System.out.println("修改數據成功");
}else{
System.out.println("修改數據失敗");
}
}
public void testDelete(){
BookTypeDAO bookTypeDAO = new BookTypeDAO();
boolean success = bookTypeDAO.delete(9);
if(success){
System.out.println("數據刪除成功");
}else{
System.out.println("數據刪除失敗");
}
}
public void testFindId(){
BookTypeDAO bookTypeDAO = new BookTypeDAO();
BookType bookType = bookTypeDAO.findId(2);
System.out.println(bookType);
}
public void testFindAll(){
BookTypeDAO bookTypeDAO = new BookTypeDAO();
bookTypeDAO.findAll();
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章