我的博客製作(二)

  連接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頁面能夠實現數據的插入。


發佈了15 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章