我的博客制作(二)

  连接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页面能够实现数据的插入。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值