JAVA Web出现这种问题

本人是初学者,没有JAVA Web的基础,为了满足教学任务
编写了一个图书信息系统,提交图书信息后报错

img

img


下面是Dao程序的代码

package Dao;

import com.beans.BookInfo;

import java.sql.*;
import java.util.ArrayList;

public class BookDao {
    private Connection conn ;
    private Statement stmt ;
    private PreparedStatement prepStmt ;
    private ResultSet rs;

    // 构造函数
    public BookDao() {
        // 进行数据库连接
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/book";
            String username = "root";
            String password = "cyk2003";
            conn = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 保存图书信息,插入到表 bookinfo
    public void saveBook(BookInfo book) {
        try {
            String sql = "INSERT INTO bookinfo VALUES (?, ?, ?, ?, ?);";
            prepStmt = conn.prepareStatement(sql);
            prepStmt.setString(1, book.getBno());
            prepStmt.setString(2, book.getBname());
            prepStmt.setString(3, book.getBauthor());
            prepStmt.setString(4, book.getBpush());
            prepStmt.setFloat(5, book.getBprice());
            prepStmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }
    }

    // 根据编号删除图书信息
    public void deleteBook(String bno) {
        try {
            String sql = "DELETE FROM bookinfo WHERE bno = ?;";
            prepStmt = conn.prepareStatement(sql);
            prepStmt.setString(1, bno);
            prepStmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }
    }

    // 修改图书信息
    public void updateBook(BookInfo book) {
        try {
            String sql = "UPDATE bookinfo SET bname = ?, bauthor = ?, bpush = ?, bprice = ? WHERE bno = ?;";
            prepStmt = conn.prepareStatement(sql);
            prepStmt.setString(1, book.getBname());
            prepStmt.setString(2, book.getBauthor());
            prepStmt.setString(3, book.getBpush());
            prepStmt.setFloat(4, book.getBprice());
            prepStmt.setString(5, book.getBno());
            prepStmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }
    }
    // 根据编号查找图书信息
    public BookInfo findBookInfo(String bno) {
        try {
            stmt = conn.createStatement();
            String sql = "SELECT * FROM bookinfo WHERE bno = '" + bno + "';";
            rs = stmt.executeQuery(sql);
            if (rs.next()) {
                BookInfo book = new BookInfo();
                book.setBno(rs.getString("bno"));
                book.setBname(rs.getString("bname"));
                book.setBauthor(rs.getString("bauthor"));
                book.setBpush(rs.getString("bpush"));
                book.setBprice(rs.getFloat("bprice"));
                return book;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }
        return null;
    }

    // 查询所有书籍信息
    public ArrayList<BookInfo> findAllBookInfo() {
        ArrayList<BookInfo> books = new ArrayList<BookInfo>();
        try {
            stmt = conn.createStatement();
            String sql = "SELECT * FROM bookinfo;";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                BookInfo book = new BookInfo();
                book.setBno(rs.getString("bno"));
                book.setBname(rs.getString("bname"));
                book.setBauthor(rs.getString("bauthor"));
                book.setBpush(rs.getString("bpush"));
                book.setBprice(rs.getFloat("bprice"));
                books.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }
        return books;
    }

    // 关闭数据库连接
    private void closeConnection() {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

没有连上数据库,检查下数据库连接的用户密码是否正确,其次如果你使用的mysql8,class_forname那个可以不设置,或者修改为:
class.forName("com.mysql.cj.jdbc.Driver");

this.conn 看下为什么是 null,是不是没有new初始化,还是没有打开

是不是数据库环境没安装?

这里为空了,你构造函数里边写的是连接数据库的代码,调用是怎么调用,是通过new么?或者就在上边 conn = DriverManager.getConnection(url, username, password)加个断点看是否连接成功

img

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这篇文章:JAVA WEB 使用dao方式实现增加雇员、修改雇员信息,显示雇员图片等功能 也许有你想要的答案,你可以看看
  • 除此之外, 这篇博客: java web 期末考试 简单的网页开发 进行增删改查操作中的 1.dao层 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    PhoneDao:

    package cn.dao;
    
    import java.sql.SQLException;
    import java.util.List;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanHandler;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    
    import cn.entity.Phone;
    import cn.util.C3p0Utils;
    
    public class PhoneDao {
    	QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource());
    
    	// 获取所有数据
    	public List<Phone> findPhone() throws SQLException {
    		String sql = "select * from phone";
    		List<Phone> phones = runner.query(sql, new BeanListHandler<Phone>(Phone.class));
    		return phones;
    	}
    
    	// 增加数据
    	public int adddataPhone(Phone phone) throws SQLException {
    		String sql = "insert into phone (id,sjname,sjprice) values(?,?,?)";
    		int row = runner.update(sql, phone.getId(), phone.getSjname(), phone.getSjprice());
    		return row;
    	}
    
    	// 删除数据,通过id
    	public int deletePhoneById(int id) throws SQLException {
    		String sql = "delete from phone where id = ?";
    		int row = runner.update(sql, id);
    		return row;
    	}
    
    	// 修改数据,通过id
    	public int updatePhone(Phone phone) throws SQLException {
    		String sql = "update phone set sjname = ?,sjprice = ? where id=?";
    		int row = runner.update(sql, phone.getSjname(), phone.getSjprice(), phone.getId());
    		return row;
    	}
    
    	// 查询商品,通过id
    	public Phone findPhoneById(int id) throws SQLException {
    		String sql = "select * from phone where id = ?";
    		Phone phone = runner.query(sql, new BeanHandler<Phone>(Phone.class), id);
    		return phone;
    	}
    }
    
    

    UserDao

    package cn.dao;
    
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanHandler;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    
    import cn.util.C3p0Utils;
    import cn.dao.*;
    import cn.entity.User;
    
    public class UserDao {
    
    	public User findUandUpsw(String username, String password) throws SQLException {
    		QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource());
    		String sql = "select * from user where username = ? and password = ?";
    		User user = runner.query(sql, new BeanHandler<User>(User.class), username, password);
    		return user;
    	}
    }
    
    

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^