图书管理系统 项目源码
package s2.jsp.zhangxiao.dao;import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLException;
/**
* 连接数据库
* @author student1
*
*/
public class BookDao {
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=book";
private static final String DBNAME="sa";
private static final String DBPASS="1234";
public Connection getConnection(){
Connection con=null;
try {
Class.forName(DRIVER);
con=DriverManager.getConnection(URL, DBNAME, DBPASS);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public void closeAll(Connection con ,PreparedStatement past,ResultSet rs){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
past.cancel();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int update(String sql,String[]getValues){
int i=0;
Connection con=null;
PreparedStatement past =null;
con=getConnection();
try {
past=con.prepareStatement(sql);
if(getValues!=null){
for (int j = 0; j < getValues.length; j++) {
past.setString(j+1, getValues);
}
}
i=past.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
}
package s2.jsp.zhangxiao.dao;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import s2.jsp.zhangxiao.entity.Book;
public class BookImpl extends BookDao {
public List bookAll(Book book){
List list=new ArrayList();
String sql="select * from book where 1=1";
Connection con=getConnection();
PreparedStatement past=null;
ResultSet rs=null;
if(book.getBook_name()!=""){
sql=sql+"and book_name='"+book.getBook_name()+"'";
}
if(book.getBook_type()!=""){
sql=sql+"and book_type='"+book.getBook_type()+"'";
}
if(book.getBook_publish()!=""){
sql=sql+"and book_publish='"+book.getBook_publish()+"'";
}
try {
past=con.prepareStatement(sql);
rs=past.executeQuery();
while(rs.next()){
Book books =new Book();
books.setBook_name(rs.getString("book_name"));
books.setBook_type(rs.getString("book_type"));
books.setBook_publish(rs.getString("book_publish"));
books.setBook_date(rs.getString("book_date"));
list.add(books);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public int bookInsert(Book book){
String sql="insert into book values(?,?,?,?)";
String[]getVlaues={book.getBook_name(),book.getBook_type(),book.getBook_publish(),book.getBook_date()};
return update(sql,getVlaues);
}
public List listtAll(){
List list=new ArrayList();
String sql="select * from book";
Connection con=getConnection();
PreparedStatement past=null;
ResultSet rs=null;
try {
past=con.prepareStatement(sql);
rs=past.executeQuery();
while(rs.next()){
Book books =new Book();
books.setBook_name(rs.getString("book_name"));
books.setBook_type(rs.getString("book_type"));
books.setBook_publish(rs.getString("book_publish"));
books.setBook_date(rs.getString("book_date"));
list.add(books);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
源码下载地址:点击下载
package s2.jsp.zhangxiao.entity;
import java.util.Date;
/**
* 实体类
* @author student1
*
*/
public class Book {
// book_id, book_name, book_type, book_publish, book_date
private int book_id;
private String book_name;
private String book_type;
private String book_publish;
private String book_date;
public int getBook_id() {
return book_id;
}
public void setBook_id(int book_id) {
this.book_id = book_id;
}
public String getBook_name() {
return book_name;
}
public void setBook_name(String book_name) {
this.book_name = book_name;
}
public String getBook_type() {
return book_type;
}
public void setBook_type(String book_type) {
this.book_type = book_type;
}
public String getBook_publish() {
return book_publish;
}
public void setBook_publish(String book_publish) {
this.book_publish = book_publish;
}
public String getBook_date() {
return book_date;
}
public void setBook_date(String book_date) {
this.book_date = book_date;
}
public Book() {
super();
}
public Book(int book_id, String book_name, String book_type,
String book_publish, String book_date) {
super();
this.book_id = book_id;
this.book_name = book_name;
this.book_type = book_type;
this.book_publish = book_publish;
this.book_date = book_date;
}
}
谢谢分享 谢谢楼主分享
代码很精简,很明了 学习学习学习学习
好的,谢谢,不够长度:lol 感谢 分享 好资料,可以好好学习下
页:
[1]