码迷,mamicode.com
首页 > 数据库 > 详细

JSP实现数据库(MySQL)查询

时间:2016-04-16 09:33:40      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

续上一篇文章:《Servlet入门(MyEclipse10,Tomcat7.0,JDK1.7,)》(http://www.cnblogs.com/ccjcjc/p/5383551.html)

 

1.创建数据库表student(数据库test01)

技术分享

2.修改success.jsp页面,修改后的页面整体代码如下:

技术分享
<%@ page language="java" import="java.sql.*,java.io.*,java.util.*"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!-- 参考博文 http://blog.csdn.net/believejava/article/details/39111823 -->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>验证成功界面</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
table {
    border: 2px #CCCCCC solid;
    width: 360px;
}

td,th {
    height: 30px;
    border: #CCCCCC 1px solid;
}
</style>
</head>

<body>
    界面表单提交跳转成功
    <br>
    <a href="index.jsp">返回</a>

    <%  
        //驱动程序名   
        String driverName = "com.mysql.jdbc.Driver";  
        //数据库用户名   
        String userName = "root";  
        //密码   
        String userPasswd = "mysql";  
        //数据库名   
        String dbName = "test01";  
        //表名   
        String tableName = "student";  
        //联结字符串   
        String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="  
                + userName + "&password=" + userPasswd;  
        Class.forName("com.mysql.jdbc.Driver").newInstance();  
        Connection connection = DriverManager.getConnection(url);  
        Statement statement = connection.createStatement();  
        String sql = "SELECT * FROM " + tableName;  
        ResultSet rs = statement.executeQuery(sql);  
    %>
    <br>
    <br>
    <table align="center">
        <tr>
            <th>
                <%  
                    out.print("学号");  
                %>
            </th>
            <th>
                <%  
                    out.print("姓名");  
                %>
            </th>
            <th>
                <%  
                    out.print("专业");  
                %>
            </th>
        </tr>

        <%  
            while (rs.next()) {  
        %>
        <tr>
            <td>
                <%  
                    out.print(rs.getString(1));  
                %>
            </td>
            <td>
                <%  
                    out.print(rs.getString(2));  
                %>
            </td>
            <td>
                <%  
                    out.print(rs.getString(3));  
                %>
            </td>
        </tr>
        <%  
            }  
        %>
    </table>
    <div align="center">
        <br> <br> <br>
        <%  
            out.print("数据查询成功,恭喜你");  
        %>
    </div>
    <%  
        rs.close();  
        statement.close();  
        connection.close();  
    %>
</body>
</html>
View Code

 

 

引用出处:http://blog.csdn.net/eastmount/article/details/45653615

JSP实现数据库(MySQL)查询

标签:

原文地址:http://www.cnblogs.com/ccjcjc/p/5397681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!