本人大连东软信息学院计算机软件工程大三java方向学生,经常在网上查找资源参照问题解决方法知道了csdn这个很不错的网站,所以决定今天我也参加这个网站的博客了并且今天开始记录我的java编程例程了!
有时候编程序正是因为有错误的存在才变得有滋有味,因为解决错误的过程是最宝贵的过程即使一个个小小的疏忽困扰了几个小时;有时候完整的一生也是离不开各式各样的错误,那样的话我们才会发现我们在一步一步的接近成熟。
呵呵不多说了,贴一下代码吧!
BookStore(第一版201104191917):(1.实现前台注册、登录功能;2.实现后台增删改查功能):(完整功能包括购物车、订单等相关功能将会写在下一篇博客BookStore2(第二版201104211936)中,敬请关注!)
1.项目整体浏览:
2.Bean:
(1)Book.java:
package Bean;
public class Book {
private String id = null;
private String bookname = null;
private String publish = null;
private String author = null;
private float price = 0;
public Book(String id, String bookname, String publish, String author,float price) {
this.id = id;
this.bookname = bookname;
this.publish = publish;
this.author = author;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
//2011.04.18
}
(2)User.java:
package Bean;
public class User {
private String username = null;
private String password = null;
public User() {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//2011.04.18
}
(3)DBBean.java:
package Bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class DBBean {
private Connection con;
private PreparedStatement pstmt;
private ResultSet rs;
private static DataSource ds;
//(1)
static {
try{
Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup("books");
}catch(Exception e){
e.printStackTrace();
}
}
//(2)
public ResultSet executeQuery(String sql,List paras) throws Exception {
con = ds.getConnection();
pstmt = con.prepareStatement(sql);
int i = 1;
if(paras!=null)
{
for(Object param:paras)
{
pstmt.setObject(i, param);
i++;
}
}
rs = pstmt.executeQuery();
return rs;
}
//(3)
public int executeUpdate(String sql,List paras) throws Exception {
con = ds.getConnection();
pstmt = con.prepareStatement(sql);
int i = 1;
if(paras!=null)
{
for(Object param:paras)
{
pstmt.setObject(i, param);
i++;
}
}
int n = pstmt.executeUpdate();
return n;
}
//(4)
public void close() {
if(rs!=null)
{
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(pstmt!=null)
{
try{
pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(con!=null)
{
try{
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
//2011.04.18
}
3.Service:
(1)BookManage.java:
package Service;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import Bean.Book;
import Bean.DBBean;
public class BookManage {
//(1)显示(查询)图书信息:(2011.04.18)
public List<Book> showBooks() {
DBBean db = new DBBean();
List<Book> bookinfo = new ArrayList<Book>();
try{
String sql = "select * from books";
ResultSet rs = db.executeQuery(sql, null);
while(rs.next())
{
String id = rs.getString(1);
String bookname = rs.getString(2);
String publish = rs.getString(3);
String author = rs.getString(4);
float price = rs.getFloat(5);
Book book = new Book(id,bookname,publish,author,price);
bookinfo.add(book);
}
}catch(Exception e){
e.printStackTrace();
}finally{
db.close();
}
return bookinfo;
}
//(2)删除图书信息:(2011.04.18)
public boolean deleteBooks(String id) {
DBBean db = new DBBean();
try{
String sql = "delete from books where id=?";
List params = new ArrayList();
params.add(id);
if(db.executeUpdate(sql, params)>0)
{
return true;
}
else
{
return false;
}
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
db.close();
}
}
//(3)显示要修改的图书信息:(2011.04.18)
public List<Book> showUpdateBooks(String id) {
DBBean db = new DBBean();
List<Book> bookinfo = new ArrayList<Book>();
try{
String sql = "select * from books where id=?";
List params = new ArrayList();
params.add(id);
ResultSet rs = db.executeQuery(sql, params);
while(rs.next())
{
String bid = rs.getString(1);
String bookname = rs.getString(2);
String publish = rs.getString(3);
String author = rs.getString(4);
float price = rs.getFloat(5);
Book book = new Book(bid,bookname,publish,author,price);
bookinfo.add(book);
}