用户登录页面显示效果图:

使用前后端分离查找图书管理系统数据库数据-LMLPHP

前段js+JQuery代码显示:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="jquery.js"></script>
    <style>
        body{
            background-image: url(t01f6feb7e1ef3f0e74.jpg);
            background-size: cover;
        }
        div{
            width: 500px;
            height: 296px;
            margin: 300px auto;
        }
        td,input{
            font-size: 25px;
            color: bisque;
        }
        caption{
            font-size: 25px;
        }

    </style>
</head>
<body>
<div>
<table>
    <caption>图书管理登录页面</caption>
    <tr>
    <td>用户名:</td>
    <td><input type="text" name="name"></td>
</tr>
    <tr>
        <td>密码:</td>
        <td><input type="password" name="pwd"></td>
    </tr>
    <tr>
        <td>性别:</td>
        <td><input type="text" name="sex"></td>
    </tr>
    <tr>
        <td>年龄:</td>
        <td><input type="text" name="age"></td>
    </tr>
    <tr>
        <td>手机号:</td>
        <td><input type="text" name="phone"></td>
    </tr>
    <tr id="qq">
        <td colspan="2" align="center"><input type="button" name="denglu" value="登录" style="background-color: red"></td>
    </tr>
</table>
    </div>
<script>
    $(function(){
        $.ajax({
            url:"userServlet",
            type:"post",
            data:{

            },
            dateType:"json",
            success:function(data){
                $.each(data,function(i){
                    var Stringstr="";
                    Stringstr = "<tr><td>"+data[i].name+"</td><td>"+data[i].pwd+"</td><td>"+data[i].sex+"</td><td>"+data[i].age+"</td><td>"+data[i].phone+"</td></tr>";
                $("tboby").appeng(Stringstr);
                });
            }
        });
    });


</script>
</body>
</html>

mvc后台框架显示:

使用前后端分离查找图书管理系统数据库数据-LMLPHP

图书类Book代码:

package com.hnpi.bean;
public class Book {
    private int id;
    private String bookName;
    private String bookAuthor;
    private String bookIsbn;
    private String bookPublish;
    private int classifyId;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getBookAuthor() {
        return bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public String getBookIsbn() {
        return bookIsbn;
    }

    public void setBookIsbn(String bookIsbn) {
        this.bookIsbn = bookIsbn;
    }

    public String getBookPublish() {
        return bookPublish;
    }

    public void setBookPublish(String bookPublish) {
        this.bookPublish = bookPublish;
    }

    public int getClassifyId() {
        return classifyId;
    }

    public void setClassifyId(int classifyId) {
        this.classifyId = classifyId;
    }

    public Book() {
        super();

    }
    public Book(Integer id, String bookName, String bookAuthor, String bookIsbn, String bookPublish, Integer classifyId) {
        super();
        this.id = id;
        this.bookName = bookName;
        this.bookAuthor = bookAuthor;
        this.bookIsbn = bookIsbn;
        this.bookPublish = bookPublish;
        this.classifyId = classifyId;

    }
}

接口BookService代码显示:

package com.hnpi.service.impl;

import com.hnpi.bean.Book;

import java.util.List;

public interface BookService {

    List<Book>selectAll();
    boolean delBook(Book book);
    boolean addBook(Book book);
    boolean updateBook(Book book);
}

实现接口BookServiceImpl代码显示:

package com.hnpi.service.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hnpi.bean.Book;

import com.hnpi.util.DBUtil;
public class BookServiceImpl implements BookService{

    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    public List<Book> selectAll(){
        conn = DBUtil.getConn();
        String sql = "select * from book";
        List<Book> bookList = new ArrayList<Book>();
        try{
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                Book book = new Book();
                book.setId(rs.getInt(1));
                book.setBookName(rs.getString(2));
                book.setBookAuthor(rs.getString(3));
                book.setBookIsbn(rs.getString(4));
                book.setBookPublish(rs.getString(5));
                book.setClassifyId(rs.getInt(6));
                bookList.add(book);
            }

        }catch(SQLException E){
            E.printStackTrace();
        }finally{
            DBUtil.closeConn(conn, ps, rs);
        }
        return bookList;
    }

    public boolean delBook(Book book){
        conn = DBUtil.getConn();
        String sql = "delete from book where id = '"+book.getId()+"'";
        int count = 0;
        try{
            ps = conn.prepareStatement(sql);
            count = ps.executeUpdate();

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DBUtil.closeConn(conn, ps, null);
        }
        if(count>0)
            return true;
        else
            return false;
    }
    public boolean addBook(Book book){
        conn = DBUtil.getConn();
        String sql = "insert into book(id,book_name,book_author,book_isbn,"+"book_publish,classify_id)values(?,?,?,?,?,?";
        int count = 0;
        try{
            ps = conn.prepareStatement(sql);
            ps.setInt(1,book.getId());
            ps.setString(2,book.getBookName());
            ps.setString(3,book.getBookAuthor());
            ps.setString(4,book.getBookIsbn());
            ps.setString(5,book.getBookPublish());
            ps.setInt(6,book.getClassifyId());
            count = ps.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DBUtil.closeConn(conn, ps, null);

        }if(count>0)
            return true;
        else
            return false;
    }

    public boolean updateBook(Book book){
        try{
            conn = DBUtil.getConn();
            String sql = "update book set book_name=?,book_author=?,"+"book_isbn=?,book_publish=?,classify_id=?where id = '"+book.getId()+"'";
            ps = conn.prepareStatement(sql);

            ps.setString(1,book.getBookName());
            ps.setString(2,book.getBookAuthor());
            ps.setString(3,book.getBookIsbn());
            ps.setString(4,book.getBookPublish());
            ps.setInt(5,book.getClassifyId());
            int result = ps.executeUpdate();
            if(result>0){
                return true;

            }else{
                return false;
            }
        }catch(SQLException sqlException){
            sqlException.printStackTrace();
            return false;
        }finally{
            DBUtil.closeConn(conn, ps, null);
        }
    }
}

LoginServlet界面,调用select()方法,获取数组中的数据,把数据放到JSON中,通过json把数据返回到HTML界面展示出来(这个是不完整的):

package com.hnpi.servlet;

import com.hnpi.bean.Book;
import com.hnpi.service.impl.BookService;
import com.hnpi.service.impl.BookServiceImpl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class LoginServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
       //允许跨域
        response.setHeader("Accsee-Control-Allow-Origin","*");

        PrintWriter out = response.getWriter();
        //实例化BookService接口
        BookService bookService = new BookServiceImpl();
      //获取接口里的select()方法
        List<Book> list = bookService.selectAll();
      //把查询到的数据放到JSON中
        String bookStr = JSON.toJSONString(list);

        out.println(bookStr);
        out.flush();
        out.close();

    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
     doPost(request,response);
    }
}

DBUtil代码与数据库连接:

package com.hnpi.util;

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


public class DBUtil {
    public static Connection getConn(){
        String url = "jdbc:sqlserver://localhost:1433;databaseName=MyDB";
        String user = "sa";
        String pwd = "1";
        Connection conn = null;
        try{
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            conn = DriverManager.getConnection(url,user,pwd);

        }catch(SQLException e){
            e.printStackTrace();
        }
        return conn;
    }
    public static void closeConn(Connection conn,PreparedStatement ps,ResultSet rs){
        try{
            if(conn!=null){
                conn.close();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        try{
            if(ps!=null){
                ps.close();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        try{
            if(rs!=null){
                rs.close();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
    }

目前这个demo是不完整的 还有很多转换没有实现 servlet其中还有增删改查没有写 数据库暂时不上传 整理过后会发表完整的项目代码及效果展示 谢谢!

10-30 13:17