一个关于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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值