当前位置:文档之家› Java课程大作业

Java课程大作业

Java课程大作业课程名称:网上书店组长:张为晓(1201050244)组员:包志英(1201050201)组员:常铭(1201050203)组员:方坦(1201050208)专业:计算机2012-2指导教师:仇丽青山东科技大学2015年1月15日小组完成任务情况统计表一、课程设计概述(目的、任务及分工情况、开发环境、参考资料)目的设计一个网上书店系统,该系统可以提供基本的书店相关的服务,并且有一定的可维护性任务及分工张为晓:组长主程序员、数据库设计包志英:辅程序员、网站设计常铭:辅程序员、文本设计方坦:辅程序员、素材收集开发环境运行平台:win7操作系统编译语言 Java编译程序 MyEclipse参考资料钱乐秋赵文耘牛军钰《软件工程(第2版)》清华大学出版社王珊《数据库系统概论》高等教育出版社陆杰《Web系统设计》浙江大学出版社二、项目的需求分析过程及需求模型可行性研究经过讨论得出了数据字典以及数据流图,并且在具体的实现方面做了完善的处理该系统主要由用户、图书信息、购买等模块构成用户管理系统,图书管理系统等。

具体为:用户管理系统:用户注册、用户登录、用户资料图书管理系统:加入图书、图书数量、图书购买三、项目设计模型(包括总体设计和详细设计),重点阐述采用的主要方法与技术总体设计本系统开发主要采用自顶向下的开发方式,瀑布模型,如图:四、实现项目的过程与步骤1. 由组长带领下全组进行设计讨论会议;2. 由组长分派各组员任务,素材收集员收集素材;3. 各组员分别完成系统功能的一部分,交由组长;4. 组长进行整合并完善代码;5. 全组总结,完成设计报告。

五、关键代码:package dao;import java.sql.Connection;import java.sql.PreparedStatement;import util.ConnDB;//添加图书信息public class AddBook {public int addbook(String bookname,String press,String author,double price){ int id=0;String sql="insert into BookInfo(BookName,Press,Author,Price) values(?,?,?,?)";PreparedStatement ps=null;try{ConnDB cdb=new ConnDB();Connection conn=cdb.getConn();ps=conn.prepareStatement(sql);ps.setString(1, bookname);ps.setString(2, press);ps.setString(3, author);ps.setDouble(4, price);id=ps.executeUpdate();}catch(Exception e){e.printStackTrace();}return id;}//添加到购物车public int addshopcar(String bookname,String press,String author,double price,String username,String img){int id=0;String sql="insert into ShopCarInfo(name,press,author,price,username,img) values(?,?,?,?,?,?)";PreparedStatement ps=null;try{ConnDB cdb=new ConnDB();Connection conn=cdb.getConn();ps=conn.prepareStatement(sql);ps.setString(1, bookname);ps.setString(2, press);ps.setString(3, author);ps.setDouble(4, price);ps.setString(5, username);ps.setString(6, img);id=ps.executeUpdate();}catch(Exception e){e.printStackTrace();}return id;}}package dao;import java.sql.Connection;import java.sql.Date;import java.sql.PreparedStatement;import util.ConnDB;public class AddNews {public int addnews(String name,String news,Date date){int id=0;String sql="insert into News(name,news,date) values(?,?,?)";PreparedStatement ps=null;ConnDB cdb=new ConnDB();try{Connection conn=cdb.getConn();ps=conn.prepareStatement(sql);ps.setString(1, name);ps.setString(2, news);ps.setDate(3, date);id=ps.executeUpdate();}catch(Exception e){e.printStackTrace();}return id;}}package dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import util.ConnDB;import entity.Admin;public class AdminLogin {public boolean adminlogin(String username,String password){boolean id=false;String sql="select * from AdminInfo where Username=? and Password=?";ConnDB cdb=new ConnDB();PreparedStatement ps=null;try{Connection conn=cdb.getConn();ps=conn.prepareStatement(sql);ps.setString(1,username);ps.setString(2,password);ResultSet rs=ps.executeQuery();while(rs.next()){id=true;Admin ad=new Admin();ad.setUsername(rs.getString("username"));ad.setPassword(rs.getString("password"));}}catch(Exception e){e.printStackTrace();}return id;}}package dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import util.ConnDB;import entity.Book;import entity.News;import entity.ShopCar;public class BookshopDao {//显示所有图书信息public List<Book> allbooks(){List<Book> list=new ArrayList<Book>();String sql="select * from BookInfo";try{ConnDB cdb=new ConnDB();Connection conn=cdb.getConn();PreparedStatement ps=null;ResultSet rs=null;ps=conn.prepareStatement(sql);rs=ps.executeQuery();while(rs.next()){Book book=new Book();book.setBookname(rs.getString("BookName"));book.setPrice(rs.getDouble("Price"));book.setAuthor(rs.getString("Author"));book.setPress(rs.getString("Press"));book.setImg(rs.getString("img"));list.add(book);}ps.close();}catch(Exception ex){ex.printStackTrace();}return list;}//查询符合条件的图书信息public List<Book> bookquery(String option,String bookoption){ List<Book> list=new ArrayList<Book>();try{ConnDB cdb=new ConnDB();Connection conn=cdb.getConn();PreparedStatement ps=null;ResultSet rs=null;if(option.equals("BookName")){String sql="select * from BookInfo where BookName like ?";ps=conn.prepareStatement(sql);ps.setString(1,'%'+bookoption+'%');rs=ps.executeQuery();while(rs.next()){Book book=new Book();book.setBookname(rs.getString("BookName"));book.setPrice(rs.getDouble("Price"));book.setAuthor(rs.getString("Author"));book.setPress(rs.getString("Press"));book.setImg(rs.getString("img"));list.add(book);}ps.close();}else if(option.equals("Press")){String sql2="select * from BookInfo where Press like ?";ps=conn.prepareStatement(sql2);ps.setString(1,'%'+bookoption+'%');rs=ps.executeQuery();while(rs.next()){Book book=new Book();book.setBookname(rs.getString("BookName"));book.setPrice(rs.getDouble("Price"));book.setAuthor(rs.getString("Author"));book.setPress(rs.getString("Press"));book.setImg(rs.getString("img"));list.add(book);}ps.close();}else if(option.equals("Author")){String sql3="select * from BookInfo where Author like ?";ps=conn.prepareStatement(sql3);ps.setString(1,'%'+bookoption+'%');rs=ps.executeQuery();while(rs.next()){Book book=new Book();book.setBookname(rs.getString("BookName"));book.setPrice(rs.getDouble("Price"));book.setAuthor(rs.getString("Author"));book.setPress(rs.getString("Press"));book.setImg(rs.getString("img"));list.add(book);}ps.close();}}catch(Exception ex){ex.printStackTrace();}return list;}//根据bookname查询图书信息以便更新使用public Book getbookname(String bookname){Book book =null;String sql ="select * from BookInfo where BookName=?";try{ConnDB dbc=new ConnDB();Connection con=dbc.getConn();PreparedStatement psmt =con.prepareStatement(sql);psmt.setString(1,bookname);ResultSet rs=psmt.executeQuery();if(rs.next()){book= new Book();book.setBookname(rs.getString("BookName"));book.setPress(rs.getString("Press"));book.setAuthor(rs.getString("Author"));book.setPrice(rs.getDouble("Price"));book.setImg(rs.getString("img"));}psmt.close();}catch(Exception e) {e.printStackTrace();}return book;}//显示购物车信息public List<ShopCar> shopcar(String username){List<ShopCar> list=new ArrayList<ShopCar>();String sql="select * from ShopCarInfo where username=?";try{ConnDB cdb=new ConnDB();Connection conn=cdb.getConn();PreparedStatement ps=null;ResultSet rs=null;ps=conn.prepareStatement(sql);ps.setString(1, username);rs=ps.executeQuery();while(rs.next()){ShopCar book=new ShopCar();book.setId(rs.getInt("id"));book.setName(rs.getString("name"));book.setPrice(rs.getDouble("price"));book.setAuthor(rs.getString("author"));book.setPress(rs.getString("press"));book.setUsername(rs.getString("username"));book.setImg(rs.getString("img"));list.add(book);}ps.close();}catch(Exception ex){ex.printStackTrace();}return list;}//显示网站公告信息public List<News> allnews(){List<News> list=new ArrayList<News>();String sql="select * from News";try{ConnDB cdb=new ConnDB();Connection conn=cdb.getConn();PreparedStatement ps=null;ResultSet rs=null;ps=conn.prepareStatement(sql);rs=ps.executeQuery();while(rs.next()){News news=new News();news.setId(rs.getInt("id"));news.setName(rs.getString("name"));news.setNews(rs.getString("news"));news.setDate(rs.getDate("date"));list.add(news);}ps.close();}catch(Exception ex){ex.printStackTrace();}return list;}}package dao;import java.sql.Connection;import java.sql.PreparedStatement;import util.ConnDB;public class DeleteBook {//删除图书public int deletebook(String bookname){int id=0;String sql="delete from BookInfo where BookName=?";try{PreparedStatement ps=null;ConnDB cdb=new ConnDB();Connection conn=cdb.getConn();ps=conn.prepareStatement(sql);ps.setString(1, bookname);id=ps.executeUpdate();}catch(Exception e){e.printStackTrace();}return id;}//删除购物车中的图书public int deleteshopcar(int id){int i=0;String sql="delete from ShopCarInfo where id=?";try{PreparedStatement ps=null;ConnDB cdb=new ConnDB();Connection conn=cdb.getConn();ps=conn.prepareStatement(sql);ps.setInt(1, id);i=ps.executeUpdate();}catch(Exception e){e.printStackTrace();}return i;}}package dao;import java.sql.Connection;import java.sql.Date;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import util.ConnDB;import entity.Message;public class MessageDao {public List<Message> allmessage()//显示用户的留言{List<Message> list=new ArrayList<Message>();String sql="select * from MessageInfo";try{ConnDB cdb=new ConnDB();Connection conn=cdb.getConn();PreparedStatement ps=null;ResultSet rs=null;ps=conn.prepareStatement(sql);rs=ps.executeQuery();while(rs.next()){Message ms=new Message();ms.setId(rs.getInt("id"));ms.setName(rs.getString("name"));ms.setMessage(rs.getString("message"));ms.setDate(rs.getDate("date"));list.add(ms);}ps.close();}catch(Exception ex){ex.printStackTrace();}return list;}//保存用户留言public int message(String name,String message,Date date){int id=0;String sql="insert into MessageInfo(name,message,date) values(?,?,?)";PreparedStatement ps=null;ConnDB cdb=new ConnDB();try{Connection conn=cdb.getConn();ps=conn.prepareStatement(sql);ps.setString(1, name);ps.setString(2, message);ps.setDate(3, date);id=ps.executeUpdate();}catch(Exception e){e.printStackTrace();}return id;}//删除用户留言public int delmessage(int id){int i=0;String sql="delete from MessageInfo where id=?";PreparedStatement ps=null;ConnDB cdb=new ConnDB();try{Connection conn=cdb.getConn();ps=conn.prepareStatement(sql);ps.setInt(1, id);i=ps.executeUpdate();}catch(Exception e){e.printStackTrace();}return i;}}package dao;import java.sql.Connection;import java.sql.PreparedStatement;import util.ConnDB;public class ModifyBook {public int modifybook(String bookname,String press,String author,double price){ int id=0;String sql="update BookInfo set Price=?,Author=?,Press=? where BookName=?";PreparedStatement ps=null;ConnDB cdb=new ConnDB();try{Connection conn=cdb.getConn();ps=conn.prepareStatement(sql);ps.setDouble(1, price);ps.setString(2, author);ps.setString(3, press);ps.setString(4, bookname);id=ps.executeUpdate();}catch(Exception e){e.printStackTrace();}return id;}}package dao;import java.sql.Connection;import java.sql.PreparedStatement;import util.ConnDB;public class ModifyPwd {public int modifypwd(String username,String oldpwd,String newpwd){int i=0;String sql="update UserInfo set Password=? where Username=? and Password=?";ConnDB cdb=new ConnDB();PreparedStatement ps=null;try{Connection conn=cdb.getConn();ps=conn.prepareStatement(sql);ps.setString(1, newpwd);ps.setString(2,username);ps.setString(3,oldpwd);i=ps.executeUpdate();}catch(Exception e){e.printStackTrace();}return i;}}package dao;import java.sql.Connection;import java.sql.PreparedStatement;import util.ConnDB;public class OrderDao {public int addorder(String username,String bookname,int quantity,double price,String address,String phone){int id=0;String sql="insert into Orders(username,bookname,quantity,price,address,phone) values(?,?,?,?,?,?)";PreparedStatement ps=null;ConnDB cdb=new ConnDB();try{Connection conn=cdb.getConn();ps=conn.prepareStatement(sql);ps.setString(1, username);ps.setString(2, bookname);ps.setInt(3, quantity);ps.setDouble(4, price);ps.setString(5, address);ps.setString(6, phone);id=ps.executeUpdate();}catch(Exception e){e.printStackTrace();}return id;}}package dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import util.ConnDB;public class Register {//注册新用户public int register(String username,String password,String password2,String sex,String phone,String email){int id=0;String sql="insert into UserInfo values(?,?,?,?,?)";ConnDB cdb=new ConnDB();Connection conn=cdb.getConn();PreparedStatement ps=null;try{ps=conn.prepareStatement(sql);ps.setString(1, username);ps.setString(2, password);ps.setString(3, sex);ps.setString(4, phone);ps.setString(5, email);id=ps.executeUpdate();}catch(Exception e){e.printStackTrace();}return id;}//注册时候判断用户名是否存在public boolean registercheck(String username){boolean id=false;String sql="select username from UserInfo where Username=?";PreparedStatement ps=null;ConnDB cdb=new ConnDB();Connection conn=cdb.getConn();try{ps=conn.prepareStatement(sql);ps.setString(1, username);ResultSet rs=ps.executeQuery();if(rs.next()){id=true;}}catch(Exception e){e.printStackTrace();}return id;}}package dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import util.ConnDB;import er;public class UserLogin {public boolean login(String username,String password){boolean id=false;String sql="select * from UserInfo where Username=? and Password=?";ConnDB cdb=new ConnDB();PreparedStatement ps=null;try{Connection con=cdb.getConn();ps=con.prepareStatement(sql);ps.setString(1,username);ps.setString(2,password);ResultSet rs=ps.executeQuery();if(rs.next()){id=true;String name=rs.getString("username");String pwd=rs.getString("password");User user1=new User();user1.setUsername(name);user1.setPassword(pwd);}}catch(Exception e){e.printStackTrace();}return id;}}六、个人任务(软件配置、个人完成的程序模块和文档清单)软件配置:PC机,服务器个人完成程序模块:包志英图书查询购买界面常铭主界面及接口张为晓用户注册登录界面方坦用户查询界面七、界面展示开始运行时看到的网站主页:用户注册界面八、遇到的困难与获得的主要成果(本节内容不得少于3页)在这次课程设计过程中,遇到了很多问题。

相关主题