编写了一个html文件,是以post请求输入一个整数,在Servlet的dopost方法里面,向一个数据库插入数据,插入不进去,一直报错。
```java
public class AddServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("ID");
int i = Integer.parseInt(id);
try {
TestInsert.Insert(i);
} catch (SQLException throwable) {
throwable.printStackTrace();
}
}
}
//插入数据的所定义的方法
public class TestInsert {
//用于插入数据
public static void Insert(int n) throws SQLException {
//Jdbc是一个工具类,用于获取连接和关闭连接
Connection connection = JdbcUtils.getConnection();
String sql = "insert into number values(?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,n);
preparedStatement.executeUpdate();
JdbcUtils.close(null,preparedStatement,connection);
}
}
```
那你看报错原因,
报错粘出来看看嘞