<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<HTML>
<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->
<!--[if IE 9]> <html lang="en" class="ie9"> <![endif]-->
<!--[if !IE]><!--> <html lang="en"> <!--<![endif]-->
<!-- BEGIN HEAD -->
<HEAD>
<meta charset="utf-8" />
<title>Metronic | Data Tables - Basic Tables</title>
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
<meta content="" name="description" />
<meta content="" name="author" />
<!-- BEGIN GLOBAL MANDATORY STYLES -->
<link href="media/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="media/css/bootstrap-responsive.min.css" rel="stylesheet" type="text/css"/>
<link href="media/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
<link href="media/css/style-metro.css" rel="stylesheet" type="text/css"/>
<link href="media/css/style.css" rel="stylesheet" type="text/css"/>
<link href="media/css/style-responsive.css" rel="stylesheet" type="text/css"/>
<link href="media/css/default.css" rel="stylesheet" type="text/css" id="style_color"/>
<link href="media/css/uniform.default.css" rel="stylesheet" type="text/css"/>
<!-- END GLOBAL MANDATORY STYLES -->
<!-- BEGIN PAGE LEVEL STYLES -->
<link rel="stylesheet" href="media/css/DT_bootstrap.css" />
<!-- END PAGE LEVEL STYLES -->
<link rel="shortcut icon" href="media/image/favicon.ico" />
<div class="span6">
<!-- BEGIN SAMPLE TABLE PORTLET-->
<div class="portlet box purple">
<div class="portlet-title">
<div class="caption"><i class="icon-comments"></i>Striped Table</div>
<div class="tools">
<a href="javascript:;" class="collapse"></a>
<a href="#portlet-config" data-toggle="modal" class="config"></a>
<a href="javascript:;" class="reload"></a>
<a href="javascript:;" class="remove"></a>
</div>
</div>
<div class="portlet-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<%
Connection conn = null;
String sql;
// MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值
// 避免中文乱码要指定useUnicode和characterEncoding
// 执行数据库操作之前要在数据库管理系统上创建一个数据库,名字自己定,
// 下面语句之前就要先创建javademo数据库
String url = "jdbc:mysql://localhost:3306/javademo?"
+ "user=root&password=&useUnicode=true&characterEncoding=UTF8";
try {
// 之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来,
// 可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以
Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动
System.out.println("成功加载MySQL驱动程序");
// 一个Connection代表一个数据库连接
conn = DriverManager.getConnection(url);
// Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等
Statement stmt = conn.createStatement();
sql = "select NO,name from student ";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet resultSet = ps.executeQuery();
String no = null;
String name = null;
while (resultSet.next()){
no = resultSet.getString(1);
name = resultSet.getString(2);
out.print("<tr><td>"+no+"</td><td>"+name+"</td></tr>");
}
} catch (SQLException e) {
System.out.println("MySQL操作错误");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
%>
</tbody>
</table>
</div>
</div>
<!-- END SAMPLE TABLE PORTLET-->
</div>
</HEAD>
<BODY>
</BODY>
</HTML>