/**
* mvc中的控制器材,C
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class BookDao implements Dao {
private Connection con = null;
private String url, username, password;
private String driverclass;
private Statement gs = null;
private PreparedStatement statement = null;
private ResultSet result = null;
public int addBook(Book book) throws Exception {
statement = con.prepareStatement("INSERT INTO book (name,price,image,data,page,number) VALUES(?,?,?,?,?,?)");
statement.setString(1, book.getName());
statement.setDouble(2, book.getPrice());
statement.setString(3, book.getImage());
statement.setString(4, book.getData());
statement.setInt(5, book.getPage());
statement.setInt(6, book.getNumber());
int x=statement.executeUpdate();
return x;
}
public int delBook(int id) throws Exception {
gs = con.createStatement();
int x=gs.executeUpdate("DELETE FROM book WHERE id=" + id);
return x;
}
public void close() {
try {
if (statement != null) {
statement.close();
}
if (gs != null) {
gs.close();
}
if (!con.isClosed()) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public Book listBook(String name) throws Exception {
if (gs == null) {
gs = con.createStatement();
}
Book book = new Book();
result = gs.executeQuery("SELECT * FROM book WHERE name='" + name+"'");
while (result.next()) {
book.setId(result.getInt(1));
book.setName(result.getString(2));
book.setPrice(result.getDouble(3));
book.setImage(result.getString(3));
book.setData(result.getString(5));
book.setPage(result.getInt(6));
book.setNumber(result.getInt(7));
}
return book;
}
public ArrayList listBooks() throws Exception {
ArrayList<Book> books = new ArrayList<Book>();
if (gs == null) {
gs = con.createStatement();
}
result = gs.executeQuery("SELECT * FROM book");
while (result.next()) {
Book book = new Book();
book.setId(result.getInt(1));
book.setName(result.getString(2));
book.setPrice(result.getDouble(3));
book.setImage(result.getString(4));
book.setData(result.getString(5));
book.setPage(result.getInt(6));
book.setNumber(result.getInt(7));
books.add(book);
}
return books;
}
public int updateBook(Book book, int id) throws Exception {
gs = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
result = gs.executeQuery("SELECT * FROM book WHERE id=" + id);
if (result.next()) {
result.first();
result.updateString("name", book.getName());
result.updateDouble("price", book.getPrice());
result.updateString("image", book.getImage());
result.updateString("data", book.getData());
result.updateInt("page", book.getPage());
result.updateInt("number", book.getNumber());
result.updateRow();
}
return 0;
}
public Connection getCon() {
return con;
}
public void setCon(Connection con) {
this.con = con;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
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;
}
public String getDriverclass() {
return driverclass;
}
public void setDriverclass(String driverclass) {
this.driverclass = driverclass;
}
public Connection setCon() {
try {
Class.forName(getDriverclass());
con = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
}
MVC模式范例 jsp的三层架构
4星 · 超过85%的资源 需积分: 0 36 浏览量
2009-06-04
15:46:49
上传
评论
收藏 671KB RAR 举报
smith1020
- 粉丝: 0
- 资源: 3
最新资源
- 基于matlab实现的光谱数据的一阶微分处理和s-g平滑,处理光谱数据的预处理方法.rar
- 基于matlab实现的光谱数据多元散射校正MSC预处理程序,对光谱数据进行建模前的预处理 - 副本.rar
- 基于matlab实现的光谱数据多元散射校正MSC预处理程序,对光谱数据进行建模前的预处理 .rar
- 基于matlab实现的光谱数据多元散射校正MSC预处理程序,对光谱数据进行建模前的预处理.rar
- 建表项目总结最终的88张表导入即可
- Python实现多线程采集英雄联盟壁纸图片源代码
- 基于matlab实现的光谱数据预处理程序,包括MSC,SNV,归一化,中心化,导数等等.rar
- 基于matlab实现的光束偏转动画.rar
- 基于matlab实现的航天器姿态控制仿真程序,利用simulink中sfunction方法建立航天器姿态动力学模型和运动学模型
- 基于matlab实现的航天器姿态自由运动以及PID控制simulink仿真航天器姿态自由运动以及PID控制simulink仿真
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈