實驗九 在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>

 

三、實驗內容中遇到的問題、解決方法和體會


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM