jsp遍历mysql数据库

jsp遍历数据库是通过jsp嵌入java代码的方式实现遍历数据库功能,话不多说上代码。

这里不用servlet直接在jsp中调用dao层中的遍历方法实现一个数据的传输。

首先是javabean层

BookInformation.java(存放数据的类)

package com.Bean;

public class BookInformation {
 
    private String number;
    private String bookname;
    private String writer;
    private String quantity;
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public String getBookname() {
        return bookname;
    }
    public void setBookname(String bookname) {
        this.bookname = bookname;
    }
    public String getWriter() {
        return writer;
    }
    public void setWriter(String writer) {
        this.writer = writer;
    }
    public String getQuantity() {
        return quantity;
    }
    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }
    
}

 Utils.java(这个里面封装了连接数据库的接口和一些方法)

package com.Util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.Bean.BookInformation;

public class Utils {
    
    //定义数据库参数
    private static String URL="jdbc:mysql://localhost:3306/db3";
    private static String USER="root";
    private static String PASSWORD="1767737316.";
    
    //创建连接对象,载体,结果集对象
    private static Connection conn=null;
    private static PreparedStatement ps=null;
    private static ResultSet rs=null;
    
    //创建连接对象,可以直接在实例化前加载,使用静态方法
    static{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn=DriverManager.getConnection(URL,USER,PASSWORD);
            System.out.println(conn.isClosed()==false? "数据库连接成功...":"数据库连接失败...");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 增删改方法update
     * @param sql 需要执行的sql语句
     * @param objects    封装了数据的数组,需要和sql语句中的占位符一一对应
     * @return 返回影响条数,反之为0
     */
    public  int update(String sql,Object[] objects){
        int a=0;
        try {
            //创建sql载体
            ps=conn.prepareStatement(sql);
            //给占位符赋值
            for(int i=0;i<objects.length;i++){
                ps.setObject(i+1, objects[i]);
            }
            //操作SQL语句
            a=ps.executeUpdate(); 
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return a;
    }
    public  int delite(String sql,Object[] objects){
        int a=0;
        try {
            //创建sql载体
            ps=conn.prepareStatement(sql);
            //给占位符赋值
            for(int i=0;i<objects.length;i++){
                ps.setObject(i+1, objects[i]);
            }
            //操作SQL语句
            a=ps.executeUpdate(); 
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return a;
    }
    public  void select(String sql,ArrayList<BookInformation> booklist,String x){
        try {
            //创建sql载体
            ps=conn.prepareStatement(sql);
            ps.setString(1,x);
            ResultSet rs  = ps.executeQuery();
            while(rs.next()) {
            //ArrayList <BookInformation> A = new ArrayList<BookInformation>();
            BookInformation B = new BookInformation();
            B.setBookname(rs.getString(2));
            B.setNumber(rs.getString(1));
            B.setWriter(rs.getString(3));
            B.setQuantity(rs.getString(4));
            booklist.add(B);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    public  void showall(String sql,ArrayList<BookInformation> booklist){
        try {
            //创建sql载体

            ps=conn.prepareStatement(sql);
            ResultSet rs  = ps.executeQuery();
            while(rs.next()) {
            //ArrayList <BookInformation> A = new ArrayList<BookInformation>();
            BookInformation B = new BookInformation();
            B.setBookname(rs.getString(2));
            B.setNumber(rs.getString(1));
            B.setWriter(rs.getString(3));
            B.setQuantity(rs.getString(4));
            booklist.add(B);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    /**
     * 
     * @param sql 需要执行的sql语句
     * @param objects    封装了数据的数组,需要和sql语句中的占位符一一对应
     * @return 封装了数据的结果集,失败返回null
     */
    public ResultSet query(String sql,Object[] objects){
        
        try {
            ps=conn.prepareStatement(sql);
            //给占位符赋值
            
            for(int i=0;i<objects.length;i++){
                ps.setObject(i+1, objects[i]);
            }
            rs=ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    public  void close(){    //关闭数据库
        try {
            
            if(rs!=null){
                rs.close();
            }
            if(ps!=null){
                ps.close();
            }
            if(conn!=null)
            {
                conn.close();
                System.out.println("数据库已关闭....");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

showall.jsp

<%@page import="com.Util.Utils"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.Bean.BookInformation"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示数据</title>
</head>
<body>
    <table border="1">
        <tr>
            <th>图书编号</th>
            <th>图书名称</th>
            <th>作者</th>
            <th>可借阅数量</th>
        </tr>
        <%
        ArrayList <BookInformation> booklist = new ArrayList<BookInformation>();//将这个集合传进方法里,然后为这个集合赋值
        Utils to=new Utils();
            String sql1="select * from tushu";
            to.showall(sql1,booklist);//这里一个方法
            //先把数据取出来,通过request对象(内置对象,不需要我们去创建,其实是httpservletRequest对象的实例)
        
            for (int i = 0; i < booklist.size(); i++) {
                BookInformation book = booklist.get(i);//book是个变量
        %>
        <tr>
            <td><%=book.getNumber()%></td>
            <td><%=book.getBookname()%></td>
            <td><%=book.getWriter()%></td>
            <td><%=book.getQuantity()%></td>
        </tr>
 
        <%
            }
        %>
    </table>
</body>
</html>

 

上一篇:题解 P2572序列操作


下一篇:重新排列句子中的单词