code090.java
package pack02;
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;
import java.util.List;
//ProductDao�࣬���滻ΪBookDao�࣬��ע��
public class code090
{
public Connection getConnection()
{
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String pwd = "mysql123";
conn = DriverManager.getConnection(url,user,pwd);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
/**
*
* @param page ҳ��
* @return book�༯��
*/
public List<code089> find(int page)
{
List<code089> list1 = new ArrayList<code089>();
Connection conn = this.getConnection();
String sql = "select * from tb_books order by id desc limit ?,?"; //mysql���ݿ�ķ�ҳ
try
{
PreparedStatement param = conn.prepareStatement(sql);
param.setInt(1, (page - 1) * code089.PAGE_SIZE); //mysql�ӵڼ�����¼��ʼ��
param.setInt(2, code089.PAGE_SIZE); //���ѡȡ����
ResultSet rs = param.executeQuery();
while (rs.next())
{
code089 book = new code089();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setPrice(rs.getDouble("price"));
book.setBookCount(rs.getInt("bookCount"));
book.setAuthor(rs.getString("author"));
list1.add(book);
}
rs.close();
param.close();
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
return list1;
}
/**
*
* @return ���ر���ܼ�¼��
*/
public int findCount()
{
int count = 0;
Connection conn = this.getConnection();
String sql = "select count(*) from tb_books";
try
{
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs.next())
{
count = rs.getInt(1);
}
rs.close();
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
return count;
}
}