1. 程式人生 > >JSP學習之---運用useBean和jdbc操作。實現簡答前臺操作資料庫。

JSP學習之---運用useBean和jdbc操作。實現簡答前臺操作資料庫。

JSP學習之—運用useBean和jdbc操作。實現簡答前臺操作資料庫。

功能描述

1 . 在”student”表中查詢所有大於特定年齡的學生資訊,此年齡由使用者指定(提示,在網頁上面新增一個文字框用於使用者輸入年齡,然後根據使用者輸入的年齡建立sql語句,下面加一個按鈕,單擊按鈕將查詢結果顯示在網頁上。
2 . 向”student”表中填入若干資料記錄,要求資料由網頁輸入,並在下一頁面顯示插入後的結果。


資料庫表設計

在mysql中按照下表的結構建立”student”表

欄位名 資料型別
name 文字
address 文字
age 數字

實現效果展示

1 .查詢
這裡寫圖片描述

2 . 插入
這裡寫圖片描述


實現程式碼

1 . DbHandle.java

package com.shiyan5_buchong;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public
class DbHandle { Connection conn; Statement sta = null; ResultSet rst = null; public DbHandle(){ try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection("jdbc:mysql://www.malikcheng.xin:3306/test"
,"***","***"); } catch (SQLException e) { e.printStackTrace(); } //System.out.println("success!"); } public ResultSet select( String age){ String sql ="select * from students where age >"+age; try { sta = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } try { rst = sta.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } // System.out.println(sql); return rst; } public ResultSet selectAll( ){ try { sta = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } try { rst = sta.executeQuery("select id,name,address,age from students"); } catch (SQLException e) { e.printStackTrace(); } // System.out.println(sql); return rst; } public void insert (String id,String name,String age,String address ) { String sql = "insert into students (id,name,age,address) values ("+id+",'"+name+"',"+age+",'"+address+"'"+")"; try { sta = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } try { sta.execute(sql); } catch (SQLException e) { e.printStackTrace(); } // System.out.println(sql); } public void close () throws Exception{ sta.close(); conn.close(); } public static void main(String[] args) throws Exception { DbHandle d = new DbHandle(); d.insert("33", "qq", "88","qq"); ResultSet rst =d.selectAll(); while (rst.next()){ System.out.print("\n"+rst.getString(1)+" "); System.out.print(" "+rst.getString(2)+" "); System.out.print(" "+rst.getString(3)+"\n"); } } }

2 . 查詢頁面

<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<jsp:useBean id="dbhandle" class="com.shiyan5_buchong.DbHandle"></jsp:useBean>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <table bgcolor="greed" border="1" cellspacing="0" cellpadding="0"
        align="center" width="500">
        <%
            String age = request.getParameter("age");
        out.println("<tr>");
        out.println("<td>" + ("id") + "</td>");
        out.println("<td>" + ("name") + "</td>");
        out.println("<td>" + ("address") + "</td>");
        out.println("<td>" + ("age") + "</td>");
        out.println("</tr>");
            if (age != null) {
                ResultSet rst = dbhandle.select(age);
                while (rst.next()) {
                    out.println("<tr>");
                    out.println("<td>" + rst.getString("id") + "</td>");
                    out.println("<td>" + rst.getString("name") + "</td>");
                    out.println("<td>" + rst.getString("address") + "</td>");
                    out.println("<td>" + rst.getString("age") + "</td>");
                    out.println("</tr>");
                }
                rst.close();
                dbhandle.close();
            }
            else{
                ResultSet rst = dbhandle.selectAll();
                while (rst.next()) {
                    out.println("<tr>");
                    out.println("<td>" + rst.getString("id") + "</td>");
                    out.println("<td>" + rst.getString("name") + "</td>");
                    out.println("<td>" + rst.getString("address") + "</td>");
                    out.println("<td>" + rst.getString("age") + "</td>");
                    out.println("</tr>");
                }
                rst.close();
                dbhandle.close();
            }
        %>
    </table>
    <div align="center">

        <form method="post" action="">
            <span>輸入要查詢的大於特定年齡的數值:</span> <input type="text" name="age">
            <input type="submit" value="提交">
        </form>
    </div>
</body>
</html>

3 . 插入頁面

<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
 <jsp:useBean id="dbhandle" class="com.shiyan5_buchong.DbHandle"></jsp:useBean> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <table bgcolor="greed" border="1" cellspacing="0" cellpadding="0"
        align="center" width="500">
        <%
        String age = request.getParameter("age");
        String id = request.getParameter("id");
        String address  = request.getParameter("address");
        String name= request.getParameter("name");
        out.println("<tr>");
        out.println("<td>" + ("id") + "</td>");
        out.println("<td>" + ("name") + "</td>");
        out.println("<td>" + ("address") + "</td>");
        out.println("<td>" + ("age") + "</td>");
        out.println("</tr>");
            if (id != null) {
             dbhandle.insert(id, name, age,address);                

            }
                ResultSet rst = dbhandle.selectAll();
                while (rst.next()) {
                    out.println("<tr>");
                    out.println("<td>" + rst.getString("id") + "</td>");
                    out.println("<td>" + rst.getString("name") + "</td>");
                    out.println("<td>" + rst.getString("address") + "</td>");
                    out.println("<td>" + rst.getString("age") + "</td>");
                    out.println("</tr>");
                }
                rst.close();
                dbhandle.close();

        %>
    </table>
    <form method="post" action="">
    <table align="center">
  <tr>
    <td>id:</td>
    <td><input type="text" name="id" required="required"></td>
  </tr>
  <tr>
    <td>name:</td>
    <td><input type="text" name="name"></td>
  </tr>
  <tr>
    <td>address:</td>
    <td><input type="text" name="address"></td>
  </tr>
  <tr>
    <td>age:</td>
    <td><input type="text" name="age"></td>
  </tr>
  <tr>
    <td colspan="2" align="center">
    <input type="submit" name="" value="提交">
    <input type="reset" name="" value ="重設">
    </td>

  </tr>
</table>

    </form>
</body>
</html>

3 . 注意不要忘記把mysql驅動拷到WebContent\WEB-INF\lib 下
4 。 還有些bug沒有解決,只是簡單的實現。比如只能提交一次、提交的資料格式不正確時等等。