jsp代码:
<%
//定义
String name = null;
String mima=null;
String name2 = null;
String mima2=null;
String headImgUrl=null;
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名test1
String url = "jdbc:mysql://ip:3306/数据库名称";
//MySQL配置时的用户名
String user = "数据账号";
//Java连接MySQL配置时的密码
String password = "**********";
//测试sql
String sqlt1 = "SELECT headImgUrl,headImgUrl as headImgUrl2 from userinfo WHERE province='河北' ORDER BY subscribeTime desc LIMIT 1";
String sqlt2 = "SELECT headImgUrl,headImgUrl as headImgUrl2 from userinfo WHERE province='河南' ORDER BY subscribeTime desc LIMIT 1";
String sqlt3 = "SELECT nickname,subscribeTime,headImgUrl from userinfo WHERE province='北京' ORDER BY subscribeTime desc LIMIT 5";
String sqllb4 = "SELECT nickname,subscribeTime ,headImgUrl from userinfo WHERE province='河北' ORDER BY subscribeTime desc LIMIT 5";
String sqllb5 = "SELECT nickname,subscribeTime,headImgUrl from userinfo WHERE province='河南' ORDER BY subscribeTime desc LIMIT 5";
String sqllb6 = "SELECT nickname,subscribeTime,headImgUrl from userinfo WHERE province='北京' ORDER BY subscribeTime desc LIMIT 5";
try {
// 1 加载驱动程序
Class.forName(driver);
// 2 连接数据库
Connection connmysql = DriverManager.getConnection(url, user, password);
// 3 用来执行SQL语句
Statement statement = connmysql.createStatement();
// 要执行的SQL语句
%>
<!--执行 -->
<%
ResultSet rs1 = statement.executeQuery(sqllb5);
/* ResultSet rs2 = statement.executeQuery(sql2);
ResultSet rs3 = statement.executeQuery(sql3);
ResultSet rs4 = statement.executeQuery(sql4);
ResultSet rs5 = statement.executeQuery(sql5);
ResultSet rs6 = statement.executeQuery(sql6); */
//将数据存入result
List<Map> result = new ArrayList<>();
while (rs1.next()) {
Map map = new HashMap();
name = rs1.getString("nickname");
mima = rs1.getString("subscribeTime");
headImgUrl = rs1.getString("headImgUrl");
map.put("name", name);
map.put("mima", mima);
map.put("headImgUrl", headImgUrl);
result.add(map);
}
rs1.close();
%>
在这里写jsp循环取数据
<ul>
<%
if (!result.isEmpty()) {
int i = 0;
for (; i < result.size(); i++) {
System.out.print(result);
String title =result.get(i).get("name").toString();
String urlpath =result.get(i).get("headImgUrl").toString();
String timeString =result.get(i).get("mima").toString();
%>
<li>
<p>
<a
href=" <%=urlpath %>"
target="_blank" title="<%=title%>"><%=title%></a>
</p>
<%-- <span><%=timeString%></span> --%>
</li>
<%
}
for (; i < result.size(); i++) {
%>
<li style="list-style: none; background: none;">
<p>
</p>
</li>
<%
}
}
%>
</ul>
<!--jdbc流关闭 -->
<%
/* rs2.close();
rs3.close();
rs4.close();
rs5.close();
rs6.close(); */
connmysql.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
%>