数据库连接代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %><%@ page import="java.sql.*" %><%@ page import="java.util.Date" %><%request.setCharacterEncoding("utf-8");//本语句用于解决从表单获取数据中文乱码问题String sid = request.getParameter("id");int id = Integer.parseInt(sid);String articleTitle=request.getParameter("articleTitle");String articleContent=request.getParameter("articleContent");request.setCharacterEncoding("utf-8");Class.forName("com.mysql.jdbc.Driver");String connection = "jdbc:mysql://localhost:3306/myblog";Connection conn = DriverManager.getConnection(connection,"root","root");//3-业务逻辑操作String sql=null;PreparedStatement pstmt=null;if(id>0){//修改日志标题信息 sql="update article set article_title=?,article_content=? where id=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, articleTitle); pstmt.setString(2, articleContent); pstmt.setInt(3, id);}else{//添加新标题 sql="insert into article(article_title , article_content)values(?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, articleTitle); pstmt.setString(2, articleContent); }pstmt.executeUpdate();//4-关闭释放资源pstmt.close();conn.close();//页面转到日志列表页面 response.sendRedirect("classList.jsp");%>