Java入门项目dao

来源:互联网 发布:小说怎么出版到网络 编辑:程序博客网 时间:2024/04/29 05:01
package com.java1234.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import com.java1234.model.Book;import com.java1234.util.StringUtil;/** * 图书Dao类 * @author Administrator * */public class BookDao {/** * 图书添加 * @param con * @param book * @return * @throws Exception */public int add(Connection con,Book book)throws Exception{String sql="insert into t_book values(null,?,?,?,?,?,?)";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, book.getBookName());pstmt.setString(2, book.getAuthor());pstmt.setString(3, book.getSex());pstmt.setFloat(4, book.getPrice());pstmt.setString(5, book.getBookDesc());pstmt.setInt(6, book.getBookTypeId());return pstmt.executeUpdate();}/** * 图书信息查询 * @param con * @param book * @return * @throws Exception */public ResultSet list(Connection con,Book book)throws Exception{StringBuffer sb=new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");if(StringUtil.isNotEmpty(book.getBookName())){sb.append(" and b.bookName like '%"+book.getBookName()+"%'");}if(StringUtil.isNotEmpty(book.getAuthor())){sb.append(" and b.author like '%"+book.getAuthor()+"%'");}if(StringUtil.isNotEmpty(book.getSex())){sb.append(" and b.sex = '"+book.getSex()+"'");}if(book.getBookTypeId()!=null){sb.append(" and b.bookTypeId = "+book.getBookTypeId());}PreparedStatement pstmt=con.prepareStatement(sb.toString());return pstmt.executeQuery();}/** * 图书信息删除 * @param con * @param id * @return * @throws Exception */public int delete(Connection con,String id)throws Exception{String sql="delete from t_book where id=?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, id);return pstmt.executeUpdate();}/** * 图书信息修改 * @param con * @param book * @return * @throws Exception */public int update(Connection con,Book book)throws Exception{String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookDesc=?,bookTypeId=? where id=?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, book.getBookName());pstmt.setString(2, book.getAuthor());pstmt.setString(3, book.getSex());pstmt.setFloat(4, book.getPrice());pstmt.setString(5, book.getBookDesc());pstmt.setInt(6, book.getBookTypeId());pstmt.setInt(7, book.getId());return pstmt.executeUpdate();}/** * 指定图书类别下是否存在图书 * @param con * @param bookTypeId * @return * @throws Exception */public boolean existBookByBookTypeId(Connection con,String bookTypeId)throws Exception{String sql="select * from t_book where bookTypeId=?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, bookTypeId);ResultSet rs=pstmt.executeQuery();return rs.next();}}


package com.java1234.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import com.java1234.model.BookType;import com.java1234.util.StringUtil;/** * 图书类别Dao类 * @author Administrator * */public class BookTypeDao {/** * 图书类别添加 * @param con * @param bookType * @return * @throws Exception */public int add(Connection con,BookType bookType) throws Exception{String sql="insert into t_bookType values(null,?,?)";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, bookType.getBookTypeName());pstmt.setString(2, bookType.getBookTypeDesc());return pstmt.executeUpdate();}/** * 查询图书类别集合 * @param con * @param bookType * @return * @throws Exception */public ResultSet list(Connection con,BookType bookType) throws Exception{StringBuffer sb=new StringBuffer("select * from t_bookType");if(StringUtil.isNotEmpty(bookType.getBookTypeName())){sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");}PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));return pstmt.executeQuery();}/** * 删除图书类别 * @param con * @param id * @return * @throws Exception */public int delete(Connection con,String id)throws Exception{String sql="delete from t_bookType where id=?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, id);return pstmt.executeUpdate();}/** * 更新图书类别 * @param con * @param bookType * @return * @throws Exception */public int update(Connection con,BookType bookType)throws Exception{String sql="update t_bookType set bookTypeName=?,bookTypeDesc=? where id=?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, bookType.getBookTypeName());pstmt.setString(2, bookType.getBookTypeDesc());pstmt.setInt(3, bookType.getId());return pstmt.executeUpdate();}}

package com.java1234.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import com.java1234.model.User;/** * 用户Dao类 * @author Administrator * */public class UserDao {/** * 登录验证 * @param con * @param user * @return * @throws Exception */public User login(Connection con,User user) throws Exception{User resultUser=null;String sql="select * from t_user where userName=? and password=?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, user.getUserName());pstmt.setString(2, user.getPassword());ResultSet rs=pstmt.executeQuery();if(rs.next()){resultUser=new User();resultUser.setUserName(rs.getString("userName"));resultUser.setPassword(rs.getString("password"));}return resultUser;}}


0 0
原创粉丝点击