为什么不能在mysql和tomcat上使用jdbc

时间:2018-10-21 12:22:15

标签: servlets

您好,我是新来使用Java的人,我想使用oop来创建Crud,我用类客户端创建了一个接口和一个实现类,我想为此类创建Crud数据,但是

public static Connection getConnection() {
    Connection con = null;
    try {
        // load the path of mySql Driver 
        Class.forName("com.mysql.jdbc.Driver");
        //Connect the Driver with ourServer
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/clientDB","root","");
    } catch (ClassNotFoundException | SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return con;
}

类工具

@Override
public void insert(clientcls client) {
    Connection connection = null;
    PreparedStatement prStatement = null;
    try {
        connection = conectionConfigrtion.getConnection();
        prStatement = connection.prepareStatement("INSERT INTO customer(name,password,email,contury)"
                + "VALUES(?,?,?,?)");
        prStatement.setString(1,client.getName());
        prStatement.setString(2, client.getPassword());
        prStatement.setString(3,client.getEmail());
        prStatement.setString(4, client.getCountry());
        prStatement.executeUpdate();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if(prStatement !=null) {
            try {
                prStatement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(connection !=null) {
            try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

servlet cood         受保护的void doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {         response.setContentType(“ type / html”);         PrintWriter printWriter = response.getWriter();

    clientDaoImp cImp = new clientDaoImp();

     String name = request.getParameter("name");

     String email = request.getParameter("email");

     String password = request.getParameter("password");

     String country = request.getParameter("country");

     clientcls client = new clientcls();
        client.setName(name);
        client.setEmail(email);
        client.setPassword(password);
        client.setCountry(country);

        cImp.insert(client);

}

this is my Error 

    Type Exception Report Description The server encountered an unexpected condition that prevented it from fulfilling the request
    Exception
    java lang NullPointerException
        daoImp clientDaoImp insert(clientDaoImp.java:21)
        servlets SaveClient doPost(SaveClient.java:54)
        javax.servlet httpHttpServlet service(HttpServletjava:660)
        javax.servlet http HttpServlet service(HttpServletjava:741)
        org apache tomcat websocket server WsFilter doFilter(WsFilterjava:53)
    Note The full stack trace of the root cause is available in the server logs

0 个答案:

没有答案