用sql语句将查询结果存入hashmap中后,怎样写一个servlet取出数据,并在jsp页面显示,最好写出代码,谢谢
这是写好的TestService.javaimportjava.sql.ResultSet;importjava.sql.SQLException;importjava....
这是写好的TestService.java
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import com.trms.bean.Course;
import com.trms.bean.Previous;
import com.trms.dao.DAO;
public class TestService {
public static HashMap<Integer, Previous> getPrevious(){
DAO dao = new DAO();
HashMap<Integer, Previous> map = new HashMap<Integer, Previous>();
String sql = "select * from previous";
ResultSet rs = dao.query(sql);
try {
int i = 0;
while(rs.next()){
i++;
Previous p = new Previous();
p.setcNo(rs.getInt(1));
p.setcPNo(rs.getInt(2));
p.setcName(rs.getString(3));
map.put(1, p);
}
} catch (SQLException e) {
e.printStackTrace();
}
return map;
}
public static HashMap<Integer, Course> getCourse(){
DAO dao = new DAO();
HashMap<Integer, Course> map = new HashMap<Integer, Course>();
String sql = "select * from course";
ResultSet rs = dao.query(sql);
try {
int k = 0;
while(rs.next()){
k++;
Course c = new Course();
HashMap<?, ?> map1 = getPrevious();
HashMap<Integer, Previous> map2 = new HashMap<Integer, Previous>();
int j = 0;
for(int i=0;i<map1.size();i++){
if(((Previous)map1.get(i+1)).getcPNo()==rs.getInt(1)){
j++;
map2.put(j, ((Previous)map1.get(i+1)));
}
}
c.setCNo(rs.getInt(1));
c.setCPNo(rs.getInt(2));
c.setCLen(rs.getInt(3));
c.setDescription(rs.getString(4));
c.setPrevList(map2);
map.put(k, c);
}
} catch (SQLException e) {
e.printStackTrace();
}
return map;
}
} 展开
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import com.trms.bean.Course;
import com.trms.bean.Previous;
import com.trms.dao.DAO;
public class TestService {
public static HashMap<Integer, Previous> getPrevious(){
DAO dao = new DAO();
HashMap<Integer, Previous> map = new HashMap<Integer, Previous>();
String sql = "select * from previous";
ResultSet rs = dao.query(sql);
try {
int i = 0;
while(rs.next()){
i++;
Previous p = new Previous();
p.setcNo(rs.getInt(1));
p.setcPNo(rs.getInt(2));
p.setcName(rs.getString(3));
map.put(1, p);
}
} catch (SQLException e) {
e.printStackTrace();
}
return map;
}
public static HashMap<Integer, Course> getCourse(){
DAO dao = new DAO();
HashMap<Integer, Course> map = new HashMap<Integer, Course>();
String sql = "select * from course";
ResultSet rs = dao.query(sql);
try {
int k = 0;
while(rs.next()){
k++;
Course c = new Course();
HashMap<?, ?> map1 = getPrevious();
HashMap<Integer, Previous> map2 = new HashMap<Integer, Previous>();
int j = 0;
for(int i=0;i<map1.size();i++){
if(((Previous)map1.get(i+1)).getcPNo()==rs.getInt(1)){
j++;
map2.put(j, ((Previous)map1.get(i+1)));
}
}
c.setCNo(rs.getInt(1));
c.setCPNo(rs.getInt(2));
c.setCLen(rs.getInt(3));
c.setDescription(rs.getString(4));
c.setPrevList(map2);
map.put(k, c);
}
} catch (SQLException e) {
e.printStackTrace();
}
return map;
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询