package com.goods;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.toolks.DBUtil;
public class TestDAO {
public List<Map<String, Object>> AllGoodsbyid(int pageno, int businessid) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Connection conn = DBUtil.getCon();
PreparedStatement pstm = null;
ResultSet rs = null;
String sql = "select id,businessid,name,introduce,image,typeid,price from goods where businessid='"
+ businessid + "' limit " + (pageno - 1) * 12 + ",12 ";
try {
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
String[] colName = new String[count];
for (int i = 1; i <= count; i++) {
colName[i - 1] = rsmd.getColumnLabel(i);
}
while (rs.next()) {
Map<String, Object> data = new HashMap<String, Object>();
for (int i = 0; i < colName.length; i++) {
data.put(colName[i], rs.getObject(colName[i]));
}
list.add(data);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstm.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Iterator it = new TestDAO().AllGoodsbyid(1, 1).iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.toolks.DBUtil;
public class TestDAO {
public List<Map<String, Object>> AllGoodsbyid(int pageno, int businessid) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Connection conn = DBUtil.getCon();
PreparedStatement pstm = null;
ResultSet rs = null;
String sql = "select id,businessid,name,introduce,image,typeid,price from goods where businessid='"
+ businessid + "' limit " + (pageno - 1) * 12 + ",12 ";
try {
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
String[] colName = new String[count];
for (int i = 1; i <= count; i++) {
colName[i - 1] = rsmd.getColumnLabel(i);
}
while (rs.next()) {
Map<String, Object> data = new HashMap<String, Object>();
for (int i = 0; i < colName.length; i++) {
data.put(colName[i], rs.getObject(colName[i]));
}
list.add(data);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstm.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Iterator it = new TestDAO().AllGoodsbyid(1, 1).iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}