JDBC:不能使用servlet和Apache Tomcat

时间:2016-08-18 16:45:30

标签: java tomcat servlets jdbc

我尝试将参数从servlet发送到db,我的连接中出现错误。

我的工作步骤:

  1. 在eclipse中,我使用Tomcat服务器

  2. 创建一个Dynamic Web Project
  3. 创建一个模型(没有任何技术,如Spring,Hibernate,JSP,JSF等),以便与DML方法一起使用(清除java代码)。

  4. 我测试了创建的模块(通过使用一些主要的测试类)并且它工作得很好(连接,插入,删除..)。

  5. 之后,我可以为客户端提供一个简单的HTML文档。

  6. 逐步运行Web模块的过程:

    1. 在开始Web项目之前,我开始运行Derby DB - 工作正常。

    2. 使用Apache Tomcat启动Web项目。

    3. 将数据插入HTML并提交。 - 工作正常

    4. 在Servlet类中,数据被门控到doGet(...)方法。 - 工作正常。

    5. 当我尝试将参数发送到DB时,从Servlet(doGet(...)方法)我的conector类中出现错误。

    6. 我该如何正确设置? 感谢。

      代码:

      Servlet类:

      @WebServlet({ "/StudentServlet", "/Student" })
      public class StudentServlet extends HttpServlet {
              private static final long serialVersionUID = 1L;
      
              private StudentDbDao sf = new StudentDbDao();
              private Student student = new Student();
      
              protected void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
      
              PrintWriter writer = response.getWriter();
      
              String studentId = request.getParameter("sudentId");
              Long id = Long.parseLong(studentId);
      
              String studentFullName = request.getParameter("studentFullName");
      
              String studentGendre = request.getParameter("studentGendre");
      
              String studentGrade = request.getParameter("studentGrade");
      
              try {
                  student.setId(id);
                  student.setFullName(studentFullName);
                  student.setGender(studentGendre);
                  student.setGrade(studentGrade);
      
                  sf.createStudent(student);
                  writer.println("The student #" + id + " inserted.");
                      } catch (StudentSystemException e) {
                  e.printStackTrace();
              }
          }
      }
      

      类StudentDbDao对DML功能的响应:

      package dao.Db;
      import java.sql.Connection;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import bean.Student;
      import bean.StudentSystemException;
      import dao.StudentDao;
      import dao.connector.ConnectionPool;
      
      public class StudentDbDao implements StudentDao {
      
          private Connection conn;
      
          public StudentDbDao() {
          }
      
          @Override
          public void createStudent(Student student) throws StudentSystemException {
      
          conn = ConnectionPool.getInstance().getConnection();
          String sql = "INSERT INTO Students(ID, FULLNAME, GENDER, GRADE) VALUES(?, ?, ?, ?)";
          PreparedStatement pstmt;
      
          try {
              pstmt = conn.prepareStatement(sql);
      
              pstmt.setLong(1, student.getId());
              pstmt.setString(2, student.getFullName());
              pstmt.setString(3, student.getGender());
              pstmt.setString(4, student.getGrade());
      
              pstmt.executeUpdate();
      
              System.out.println(student.getFullName() + " created successfully");
      
          } catch (SQLException e) {
              throw new StudentSystemException("Failed!", e);
          } finally {
              ConnectionPool.getInstance().returnConnection(conn);
          }
      
          }
      
      @Override
      public void removeStudent(Student student) throws StudentSystemException {
          conn = ConnectionPool.getInstance().getConnection();
          String sql = "DELETE FROM Students WHERE ID = " + student.getId();
          PreparedStatement pstmt;
      
          try {
              pstmt = conn.prepareStatement(sql);
              pstmt.executeUpdate();
              System.out.println(student.getId() + " removed successfully.");
      
          } catch (SQLException e) {
              throw new StudentSystemException("Failed!", e);
          } finally {
              ConnectionPool.getInstance().returnConnection(conn);
          }
      }
      
      @Override
      public Student getStudentById(long id) throws StudentSystemException {
      
          conn = ConnectionPool.getInstance().getConnection();
          String sql = "SELECT * FROM Students WHERE ID = " + id;
          Student student = new Student();
          PreparedStatement pstmt;
          ResultSet rs;
      
          try {
              pstmt = conn.prepareStatement(sql);
              rs = pstmt.executeQuery();
      
              if (rs.next()) {
                  student.setId(rs.getLong(1));
                  student.setFullName(rs.getString(2));
                  student.setGender(rs.getString(3));
                  student.setGrade(rs.getString(4));
              }
          // else {
          // System.out.print("Student with PID #" + id + " not exists. ");
          // }
              return student;
      
          } catch (SQLException e) {
              throw new StudentSystemException("Failed!", e);
          } finally {
              ConnectionPool.getInstance().returnConnection(conn);
          }
      
      }
      
      public long getMaxRows() throws StudentSystemException {
      
          conn = ConnectionPool.getInstance().getConnection();
          String sql = "SELECT COUNT(*) FROM Students";
          PreparedStatement pstmt;
          int count = 0;
      
          try {
              pstmt = conn.prepareStatement(sql);
              ResultSet rs = pstmt.executeQuery();
      
              rs.next();
              count = rs.getInt(1);
          } catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } finally {
              ConnectionPool.getInstance().returnConnection(conn);
          }
      
          return count;
      }
      }
      

      类ConnectionPool,当Servlet尝试设置参数时代码落在其中:

      public class ConnectionPool {
      
      // static final int MAX_CONS = 1;
      private Connection myconn = null;
      // private Set<Connection> connections = new HashSet<Connection>();
      private static ConnectionPool instance = new ConnectionPool();
      String url = "jdbc:derby://localhost:1527/StudentDB";
      
      private ConnectionPool() {
      
          try {
              myconn = DriverManager.getConnection(url);
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
      
      public static ConnectionPool getInstance() {
          return instance;
      }
      
      public Connection getConnection() {
          // Connection conn = myconn;
          return this.myconn;
      }
      
      public void returnConnection(Connection conn) {
          this.myconn = conn;
          // myconn.add(conn);
      }
      
      public void closeAllConnections() throws StudentSystemException {
      
          Connection connection = myconn;
      
          try {
              connection.close();
          } catch (SQLException e) {
              throw new StudentSystemException("Failed to close connection: ", e);
          }
      
      }
      

      在提交之前和打印屏幕之后附上:

      之前: enter image description here 后:

      enter image description here

0 个答案:

没有答案