单页js文件访问数据库

最原始的编程方式,业务逻辑混杂在html中

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>mldn高端java培训</title>
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  <%!
  	//定义数据库驱动程序
  	public static final String DBDRIVER="com.mysql.jdbc.Driver";
  	//数据库连接地址
  	public static final String DBURL="jdbc:mysql://localhost:3306/mldn";
  	public static final String DBUSER="root";
  	public static final String DBPASS="root";
  %>
  <%
  	Connection conn=null;
  	PreparedStatement pstmt=null;
  	ResultSet rs=null;
  %>
  <%
  try{
  	Class.forName(DBDRIVER);
  	conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);//取得数据库连接
  	String sql="SELECT empno,ename,job,sal,hiredate FROM emp";
  	pstmt=conn.prepareStatement(sql);
  	rs=pstmt.executeQuery();
  %>
  <center>
  	<table border="1" width="80%">
  		<tr>
  			<td>雇员编号</td>
  			<td>雇员姓名</td>
  			<td>雇员工作</td>
  			<td>雇员工资</td>
  			<td>雇佣日期</td>
  		</tr>
<%
  	while(rs.next()){
  		int empno=rs.getInt(1);
  		String ename=rs.getString(2);
  		String job=rs.getString(3);
  		float sal=rs.getFloat(4);
  		java.util.Date date=rs.getDate(5);
%>
  	<tr>
  		<td><%=empno%></td>
  		<td><%=ename%></td>
  		<td><%=job%></td>
  		<td><%=date%></td>
  		<td><%=sal%></td>
  		
  	</tr>
<%
	}
%>
 	</table>
  </center>
<%
	}catch(Exception e){
		System.out.println(e);
}finally{
	rs.close();
	pstmt.close();
	conn.close();
}
%>
  </body>
</html>

  

posted @ 2013-11-28 16:00  山貓  阅读(316)  评论(0编辑  收藏  举报