连接mysql,代码都没错误,驱动很到位。并且在连接数据库成功率很高的情况下,老是报驱动找不到的错,快吐血了都。最后解决的竟然是方法是:全部重启,代码重新写一遍,驱动重新载入一遍。
连数据库只是前菜,后面的插入数据才是重头戏。
代码如下:
public class BlogServlet_1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String title = request.getParameter("title");
String category = request.getParameter("category");
String content = request.getParameter("content");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/blog?user=root&password=123");
String sql = "insert into blog (blo_id,title,content,create_time) value (?,?,?,now())";
java.sql.PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1, Integer.parseInt(category));
pst.setString(2, title);
pst.setString(3, content);
int result = pst.executeUpdate();
System.out.println(result);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
连着前面的jsp页面能够实现数据的插入。