<%@ page contentType="text/html" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>this is MYSQL</title>
</head>
<body>
<%! //定义数据库驱动
public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
//数据库连接地址
public static final String DBURL="jdbc:mysql://localhost:3306/shujuku";
public static final String DBUSER="root";
public static final String DBPASS="123";
%>
<%
Connection conn =null;
PreparedStatement pstmt =null;
ResultSet rs=null;
%>
<%
try { //数据库操作中会出现异常,所以要使用 try catch处理
Class.forName(DBDRIVER); //驱动加载
conn =DriverManager.getConnection(DBURL,DBUSER,DBPASS); //数据库连接
String sql ="select id, name ,job from person ";
pstmt =conn.prepareStatement(sql);//实例化PreparedStatement 对象
rs =pstmt.executeQuery();//执行查询操作
%>
<center>
<table border="1" width="80%">
<tr>
<td>ID:</td>
<td>名字:</td>
<td>工作:</td>
</tr>
<%
while(rs.next()){
int id=rs.getInt(1);
String name=rs.getString(2);
String job=rs.getString(3);
//java.util.Date date=rs.getDate(5); //去除雇佣日期
%>
<tr>
<td><%=id%></td>
<td><%=name%></td>
<td><%=job%></td>
</tr>
<%
}
%>
</table>
</center>
<%
}catch(Exception e) {
System.out.println(e);
}finally{
rs.close();
pstmt.close();
conn.close();
}
%>
</body>
</html>