1:实体类内容
private static final long serialVersionUID = 1L;
private String typeNum;//类型数量
private String typeName;//展示类型
public String getTypeNum() {
return typeNum;
}
public void setTypeNum(String typeNum) {
this.typeNum = typeNum;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public Statistical(String typeNum, String typeName) {
super();
this.typeNum = typeNum;
this.typeName = typeName;
}
2:连接过程
public List<Statistical> findByCityListSewageOutlet(DcNode node) {//传过来数据库信息
List<Statistical> list = Lists.newArrayList();//声明一个对象list
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();//1.加载驱动程序
conn = DriverManager.getConnection(
"jdbc:mysql://" + IP地址+ ":" + 端口号+ "/" + 数据库名称+ "?useUnicode=TRUE&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull",
用户名, 密码);//获得连接
String sql = " select * from 表名称";
ps = conn.prepareStatement(sql);//通过数据库的连接操作数据库,实现增删改查
rs = ps.executeQuery();
while (rs.next()) {
Statistical statistical = new Statistical(rs.getString("typeNum"), rs.getString("typeName"));
list.add(statistical);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ps.close();
rs.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}