首页 > 代码库 > web网页练习

web网页练习

//从数据库获取,并显示在页面上
<%@page import="java.text.SimpleDateFormat"%> <%@page import="java.sql.*"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <table width="100%" border="2" cellspacing="1" cellpadding="1"> <tr style="background-color:#6F3; color:#000"> <td width="15%">代号</td> <td width="15%">姓名</td> <td width="15%">性别</td> <td width="15%">民族</td> <td width="15%">生日</td> <td>操作</td> </tr> <% Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root",""); String sql="select * from info join nation on info.Nation=nation.Code"; //String sql="select code name sex (select * from nation ) birthday from info"; PreparedStatement stat=conn.prepareStatement(sql); ResultSet re=stat.executeQuery(); while(re.next()){ %> <tr> <td><%=re.getString(1)%></td> <td><%=re.getString(2)%></td> <td><%=re.getBoolean(3)?"男":"女"%></td> <td><%=re.getString(7)%></td> <% Date s=re.getDate(5); SimpleDateFormat m=new SimpleDateFormat("yyyy年MM月dd日"); String shijian=m.format(s); %> <td><%=shijian %></td> <td></td> </tr> <% } conn.close(); %> </table> </body> </html>

显示效果如下

技术分享

 

web网页练习