实验九 在JSP中使用数据库

实验性质:验证性 实验学时: 1学时 实验地点:

一 、实验目的与要求

1、 掌握在JSP中使用数据库的方法。

2、 掌握JSP对数据库的基本操作:增、删、改、查。

二、 实验内容

1、JSP访问数据库的准备工作

(1)创建数据库和数据表

启动Navicat for MySQL,建立和数据库服务器的连接,打开连接,创建一个名为"xsgl"的数据库,在其中建立名为"userinfo"的表,表结构如下图所示:

 

保存表,输入表名,确定。双击表名,输入表中的记录,如下图所示:

2、在JSP中实现对数据库的增、删、改、查

(1)编写一个JSP页面,显示userinfo表中的所有记录,如下图所示:

请写出相应的代码:

<%@ page import="java.sql.*" %><%--
  Created by IntelliJ IDEA.
  User: hasee
  Date: 2018/6/21
  Time: 19:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>SQL</title>
</head>
<body>
<%
    //加载驱动
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        out.println("加载驱动失败");


    }

    String url = "jdbc:mysql://localhost:3306/xxgl";
    String user = "root";


    String password = "不让你看";


    try {
        Connection conn = DriverManager.getConnection(url, user, password);
        Statement stmt = conn.createStatement();
//        String sql_update = "update userinfo set weight=78 where name='李振';";
//        String sql_insert = "INSERT INTO userinfo (id,name,birthday,qq,height,weight)" +
//                "VALUES" +
//                "(5,'ZNN','19961121','20145468',1.78,50);";
//        String sql_update = "update userinfo set weight=78 where name='李振';";
//        String sql_insert = "INSERT INTO userinfo ()" +
//                "VALUES" +
//                "(5,'ZNN','123654','19961121','20145468',1.78,50,'1');";
//        String sql_delete = "DELETE FROM userinfo where id=5;";
        String sql = "SELECT * FROM userinfo WHERE height>1.66;";
//        stmt.executeUpdate(sql_update);
//        stmt.executeUpdate(sql_insert);
//        stmt.executeUpdate(sql_delete);
        ResultSet rs = stmt.executeQuery(sql);

        out.print("<table border=1><tr>");
        out.println("<tr><th width=100>编号</th>" +
                "<th width=100>姓名</th>" +
                "<th width=100>出生日期</th>" +
                "<th width=100>qq</th>" +
                "<th width=100>身高</th>" +
                "<th width=100>体重</th></tr>");
        while (rs.next()) {
            out.print("<tr>");
            out.println(
                    "<td width=100>" + rs.getString("id")
                            + "<td width=100>" + rs.getString("name") + "</td>"
                            + "<td width=100>" + rs.getString("birthday") + "</td>"
                            + "<td width=100>" + rs.getString("qq") + "</td>"
                            + "<td width=100>" + rs.getString("height") + "</td>"
                            + "<td width=100>" + rs.getString("weight") + "</td>"
            );

            out.print("</tr>");
        }
        out.print("</table>");
        rs.close();
        conn.close();
        stmt.close();

    } catch (SQLException e) {
        e.printStackTrace();
        out.println("MYsql连接失败");
    }

%>

</body>
</html>

三、实验内容中遇到的问题、解决方法和体会

猜你喜欢

转载自www.cnblogs.com/lgqrlchinese/p/9252256.html