程序如下:
ProvinceDistrictDao proDao = new ProvinceDistrictDaoHibernate();
List<ProvinceDistrict> prolist = proDao.getProvinceDistricts();
int countSize = prolist.size();
int[][] count = new int[countSize][3];
Transaction t = null;
try {
int j = 0;
Session session = createNewSession();
Connection con = session.connection();
for(int i=0;i<countSize;i++){
t = session.beginTransaction();
String procedure = "{call CountBaseStationByYear(?)}";
CallableStatement cstmt = con.prepareCall(procedure);
int proId = prolist.get(i).getProvinceId();
cstmt.setInt(1, proId);
System.out.println("ID"+prolist.get(i).getProvinceName());
cstmt.execute();
t.commit();
ResultSet rs = cstmt.executeQuery();
while(rs.next()) {
count[j][0] = proId;
count[j][1] = rs.getInt("年份");
count[j][2] = rs.getInt("设备数");
System.out.println(count[j][0]+"设备数"+count[j][2]);
j++;
}
}
session.close();
} catch (Exception e) {
if (t != null){
t.rollback();
}
e.printStackTrace();
}
return count;返回结果:
ID北京市
ID上海市
2设备数1
ID广东省
3设备数1
3设备数1
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started我还有一条数据就显示不出来,还报错了,为什么啊
ProvinceDistrictDao proDao = new ProvinceDistrictDaoHibernate();
List<ProvinceDistrict> prolist = proDao.getProvinceDistricts();
int countSize = prolist.size();
int[][] count = new int[countSize][3];
Transaction t = null;
try {
int j = 0;
Session session = createNewSession();
Connection con = session.connection();
for(int i=0;i<countSize;i++){
t = session.beginTransaction();
String procedure = "{call CountBaseStationByYear(?)}";
CallableStatement cstmt = con.prepareCall(procedure);
int proId = prolist.get(i).getProvinceId();
cstmt.setInt(1, proId);
System.out.println("ID"+prolist.get(i).getProvinceName());
cstmt.execute();
t.commit();
ResultSet rs = cstmt.executeQuery();
while(rs.next()) {
count[j][0] = proId;
count[j][1] = rs.getInt("年份");
count[j][2] = rs.getInt("设备数");
System.out.println(count[j][0]+"设备数"+count[j][2]);
j++;
}
}
session.close();
} catch (Exception e) {
if (t != null){
t.rollback();
}
e.printStackTrace();
}
return count;返回结果:
ID北京市
ID上海市
2设备数1
ID广东省
3设备数1
3设备数1
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started我还有一条数据就显示不出来,还报错了,为什么啊
count[j][0] = proId;
count[j][1] = rs.getInt("年份");
count[j][2] = rs.getInt("设备数");
System.out.println(count[j][0]+"设备数"+count[j][2]);
j++;
rs.next();
}