当前位置: 代码迷 >> Java Web开发 >> jsp 数据库问题
  详细解决方案

jsp 数据库问题

热度:169   发布时间:2009-11-05 21:09:11.0
jsp 数据库问题
怎么将jsp表单里的数据按“提交”就自动保存进MySQL数据库????高手举例讲讲呗
搜索更多相关主题的帖子: jsp  数据库  

----------------解决方案--------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>

<%

request.setCharacterEncoding("gbk");//解决乱码问题
String action = request.getParameter("action");//接受参数
if(action != null && action.equals("post")) {
    String title = request.getParameter("title");
    String cont = request.getParameter("cont");
   
    cont = cont.replaceAll("\n" , "<br>");
   
    Class.forName("com.mysql.jdbc.Driver");//连接MySQL数据库
    String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
    Connection conn = DriverManager.getConnection(url);
   
    conn.setAutoCommit(false);
   
    String sql = "insert into article values (null, 0, ?, ?, ?, now(), 0)";//SQL语句,插入数据
    PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
    Statement stmt = conn.createStatement();
   
    pstmt.setInt(1, -1);//将-1插入到PreparedStatement的第一个?
    pstmt.setString(2, title);//将title插入到PreparedStatement的第二个?
    pstmt.setString(3, cont););//将cont插入到PreparedStatement的第三个?
    pstmt.executeUpdate();
   
    ResultSet rsKey = pstmt.getGeneratedKeys();
    rsKey.next();
    int key = rsKey.getInt(1);
    rsKey.close();
    stmt.executeUpdate("update article set rootid = " + key + " where id = " + key);
   
    conn.commit();
    conn.setAutoCommit(true);
   
    stmt.close();
    pstmt.close();
    conn.close();
   
    response.sendRedirect("index.jsp");跳转回首页
}
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<form action="Post.jsp" method="post">
    <input type="hidden" name="action" value="post">
    <table border="1">
        <tr>
            <td>
                <input type="text" name="title" size="80">
            </td>
        </tr>
        <tr>
            <td>
                <textarea cols="80" rows="12" name="cont"></textarea>
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="提交">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

[ 本帖最后由 lampeter123 于 2009-11-6 10:27 编辑 ]
----------------解决方案--------------------------------------------------------
哇塞,lampeter123好厉害,把代码全打出来了~~~~
----------------解决方案--------------------------------------------------------
不懂
----------------解决方案--------------------------------------------------------
不就是写一个insert方法将数据插入到数据库嘛。jdbc操作啊。。
----------------解决方案--------------------------------------------------------
楼主还是先去看了JDBC的东西再做项目吧,这是基础啊
----------------解决方案--------------------------------------------------------
楼上的,哪有这样编码的,将业务逻辑与前台代码全混在一起,应采用MVC模式进行开发。

----------------解决方案--------------------------------------------------------
以下是引用zouguozhu在2009-11-18 16:31:33的发言:

楼上的,哪有这样编码的,将业务逻辑与前台代码全混在一起,应采用MVC模式进行开发。


其实这个例子也就是帮助楼主解决下问题,没有别的意思了。不用说那么仔细了。

----------------解决方案--------------------------------------------------------
  相关解决方案