package com.zw.spring;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.sql.DataSource;
import com.zw.spring.dao.BookAddInterface;
public class BookAddImpl implements BookAddInterface {
private DataSource dataSource;
public Connection conn;
private Book book;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public boolean addBook(Book book) {
// TODO Auto-generated method stub
System.out.println(book.getAuthor()+","+book.getBookname());
try {
conn=getDataSource().getConnection();
String sql="insert into dbo.book (bookname,price,author,press,pressdate) values (?,?,?,?,?)";
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setString(1,book.getBookname());
ptmt.setString(2, book.getPrice());
ptmt.setString(3, book.getAuthor());
ptmt.setString(4, book.getPress());
ptmt.setString(5,book.getPressdate());
boolean f=ptmt.execute();
if(f==false)
{
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("失败了");
}
return false;
}
public boolean delete(String ids) {
// TODO Auto-generated method stub
try {
conn=getDataSource().getConnection();
System.out.println(ids);
String[] id=ids.split(",");
for(int i=1;i<id.length;i++)
{
String sql="delete from dbo.book where id="+id[i];
System.out.println(sql);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public ArrayList<Book> resultList(String bookname) {
// TODO Auto-generated method stub
ArrayList<Book> array=new ArrayList<Book>();
try {
conn=dataSource.getConnection();
String sql="select * from dbo.book where bookname like '%"+bookname+"%'";
java.sql.Statement stmt=conn.createStatement();
ResultSet result=stmt.executeQuery(sql);
while(result.next())
{
book=new Book();
book.setBookname(result.getString(1));
book.setPrice(result.getString(2));
book.setAuthor(result.getString(3));
book.setPress(result.getString(4));
book.setPressdate(result.getString(5));
book.setId(result.getInt(6));
array.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return array;
}
public ArrayList<Book> getAllBooks() {
// TODO Auto-generated method stub
ArrayList<Book> array=new ArrayList<Book>();
try {
conn=dataSource.getConnection();
String sql="select * from book";
java.sql.Statement stmt=conn.createStatement();
ResultSet result=stmt.executeQuery(sql);
while(result.next())
{
book=new Book();
book.setBookname(result.getString(1));
book.setPrice(result.getString(2));
book.setAuthor(result.getString(3));
book.setPress(result.getString(4));
book.setPressdate(result.getString(5));
book.setId(result.getInt(6));
array.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return array;
}
public boolean updateBook(Book book) {
// TODO Auto-generated method stub
try {
System.out.println(book.getId());
conn=dataSource.getConnection();
String sql="update book set bookname=?,price=?,author=?,press=?,pressdate=? where id=?";
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setString(1, book.getBookname());
ptmt.setString(2, book.getPrice());
ptmt.setString(3, book.getAuthor());
ptmt.setString(4, book.getPress());
ptmt.setString(5, book.getPressdate());
ptmt.setInt(6, book.getId());
int f=ptmt.executeUpdate();
System.out.println(f);
if(f==1)
{
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return false;
}
public Book getBook(int id) {
// TODO Auto-generated method stub
try {
conn=dataSource.getConnection();
Statement stmt=conn.createStatement();
String sql="select * from book where id="+id;
ResultSet result=stmt.executeQuery(sql);
while(result.next())
{
book.setBookname(result.getString(1));
book.setPrice(result.getString(2));
book.setAuthor(result.getString(3));
book.setPress(result.getString(4));
book.setPressdate(result.getString(5));
book.setId(result.getInt(6));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return book;
}
}