資源簡介
2-14.rar
代碼片段和文件信息
package?com.lyq.bean;
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;
/**
?*?商品數據庫操作
?*
?*/
public?class?BookDao?{
/**
?*?獲取數據庫連接
?*?@return?Connection對象
?*/
public?Connection?getConnection(){
//?數據庫連接
Connection?conn?=?null;
try?{
//?加載數據庫驅動,注冊到驅動管理器
Class.forName(“com.mysql.jdbc.Driver“);
//?數據庫連接字符串
String?url?=?“jdbc:mysql://localhost:3306/db_database10“;
//?數據庫用戶名
String?username?=?“root“;
//?數據庫密碼
String?password?=?“111“;
//?創建Connection連接
conn?=?DriverManager.getConnection(urlusernamepassword);
}?catch?(ClassNotFoundException?e)?{
e.printStackTrace();
}?catch?(SQLException?e)?{
e.printStackTrace();
}
//?返回數據庫連接
return?conn;
}
/**
?*?分頁查詢所有商品信息
?*?@param?page?頁數
?*?@return?List
?*/
public?List?find(int?page){
//?創建List
List?list?=?new?ArrayList();
//?獲取數據庫連接
Connection?conn?=?getConnection();
//?分頁查詢的SQL語句
String?sql?=?“select?*?from?tb_product?order?by?id?desc?limit???“;
try?{
//?獲取PreparedStatement
PreparedStatement?ps?=?conn.prepareStatement(sql);
//?對SQL語句中的第1個參數賦值
ps.setInt(1?(page?-?1)?*?Product.PAGE_SIZE);
//?對SQL語句中的第2個參數賦值
ps.setInt(2?Product.PAGE_SIZE);
//?執行查詢操作
ResultSet?rs?=?ps.executeQuery();
//?光標向后移動,并判斷是否有效
while(rs.next()){
//?實例化Product
Product?p?=?new?Product();
//?對id屬性賦值
p.setId(rs.getInt(“id“));
//?對name屬性賦值
p.setName(rs.getString(“name“));
//?對num屬性賦值
p.setNum(rs.getInt(“num“));
//?對price屬性賦值
p.setPrice(rs.getDouble(“price“));
//?對unit屬性賦值
p.setUnit(rs.getString(“unit“));
//?將Product添加到List集合中
list.add(p);
}
//?關閉ResultSet
rs.close();
//?關閉PreparedStatement
ps.close();
//?關閉Connection
conn.close();
}?catch?(SQLException?e)?{
e.printStackTrace();
}
return?list;
}
/**
?*?查詢總記錄數
?*?@return?總記錄數
?*/
public?int?findCount(){
//?總記錄數
int?count?=?0;
//?獲取數據庫連接
Connection?conn?=?getConnection();
//?查詢總記錄數SQL語句
String?sql?=?“select?count(*)?from?tb_product“;
try?{
//?創建Statement
Statement?stmt?=?conn.createStatement();
//?查詢并獲取ResultSet
ResultSet?rs?=?stmt.executeQuery(sql);
//?光標向后移動,并判斷是否有效
if(rs.next()){
//?對總記錄數賦值
count?=?rs.getInt(1);
}
//?關閉ResultSet
rs.close();
//?關閉Connection
conn.close();
}?catch?(SQLException?e)?{
e.printStackTrace();
}
//?返回總記錄數
return?count;
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????353??2016-03-21?11:59??第2章\2.1\index.html
?????文件????????531??2016-03-21?11:59??第2章\2.10\index.html
?????文件???????1101??2016-03-21?11:59??第2章\2.11\index.html
?????文件???20643501??2016-03-21?11:59??第2章\2.12\Big.ogv
?????文件????????453??2016-03-21?11:59??第2章\2.12\index.html
?????文件?????857894??2016-03-21?11:59??第2章\2.12\mingrisoft.mp4
?????文件????????958??2016-03-21?11:59??第2章\2.13\index.html
?????文件????????428??2016-03-21?11:59??第2章\2.14\index.html
?????文件????????322??2016-03-21?11:59??第2章\2.15\index.html
?????文件????????364??2016-03-21?11:59??第2章\2.16\index.html
?????文件????????360??2016-03-21?11:59??第2章\2.17\index.html
?????文件????????526??2016-03-21?11:59??第2章\2.18\index.html
?????文件????????697??2016-03-21?11:59??第2章\2.19\index.html
?????文件????????384??2016-03-21?11:59??第2章\2.2\index.html
?????文件????????265??2016-03-21?11:59??第2章\2.20\index.html
?????文件????????336??2016-03-21?11:59??第2章\2.20\newWindow.html
?????文件????????422??2016-03-21?11:59??第2章\2.3\index.html
?????文件????????424??2016-03-21?11:59??第2章\2.4\index.html
?????文件????????259??2016-03-21?11:59??第2章\2.5\header示例.html
?????文件??????19156??2016-03-21?11:59??第2章\2.5\mrlogo.jpg
?????文件???????4096??2016-03-21?11:59??第2章\2.5\Thumbs.db
?????文件????????367??2016-03-21?11:59??第2章\2.6\index.html
?????文件????????381??2016-03-21?11:59??第2章\2.7\index.html
?????文件????????687??2016-03-21?11:59??第2章\2.8\index.html
?????文件????????381??2016-03-21?11:59??第2章\2.9\index.html
?????文件??????19917??2016-03-21?11:59??第2章\sj1\bike.jpg
?????文件????????380??2016-03-21?11:59??第2章\sj1\index.html
?????文件???????5120??2016-03-21?11:59??第2章\sj1\Thumbs.db
?????文件???????2957??2016-03-21?11:59??第2章\sj2\index.html
?????文件??????51372??2016-03-21?12:01??第2章\sj3\images\01.gif
............此處省略2981個文件信息
評論
共有 條評論