📄 viewcategory.java
字号:
package com.jspdev.ch12;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import com.jspdev.util.DataBaseConnection;
public class ViewCategory {
private Connection con;
//公共方法,获得数据库连接
public ViewCategory() {
this.con = DataBaseConnection.getConnection();
}
/**
*返回商品的所有分类,返回的Collection中包含Category值对象
*/
public Collection getAllCategory() throws Exception {
Statement stmt = null;
try {
stmt = con.createStatement();
} catch(Exception e) {
e.printStackTrace();
}
ResultSet rst = stmt.executeQuery("select * from category");
Collection ret = new ArrayList();
while(rst.next()) {
Category temp = new Category();
temp.setCategoryId(rst.getString("catid"));
temp.setName(rst.getString("name"));
temp.setDescription(rst.getString("descn"));
ret.add(temp);
}
//关闭连接,rst和stmt将自己关闭
con.close();
return ret;
}
public static void main(String[] args) {
ViewCategory a = new ViewCategory();
Collection b = null;
try {
b = a.getAllCategory();
} catch (Exception e) {
e.printStackTrace();
}
// if(b != null) {
// System.out.print("到这里是正确的!");
// }
Iterator ite = b.iterator() ;
while(ite.hasNext()) {
Category temp = (Category)ite.next();
System.out.print(temp.getCategoryId());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -