如何在jsp页面里,点击一个按钮后向数据库插入数据

如何在jsp页面里,点击一个按钮后向数据库插入数据,第1张

首先在后台定义一个类和方法:

import org.directwebremoting.annotations.RemoteMethod

import org.directwebremoting.annotations.RemoteProxy

import com.core.manager.UserMng

/**用户管理DWR*/

@RemoteProxy(name="userDwr")

public class UserDwr {

@Autowired

private UserMng userMng

/**

* 插入用户记录

* @param user 用户对象

* @return String

* */

@RemoteMethod

public String addUser(User user) {

user = userMng.save(user)

if (null != user.getId()) {

return "插入用户数据成功!"

}

return " *** 作失败!"

}

}

然后在jsp写一个function:

function addUser() {

var user = {userName:"zhangsan",password:"zhangsan",realName:"张三",sex:"男"}

userDwr.addUser(user, function(result) {alert(result)})

}

最后在你的按钮中调用这个function:

<input type="button" value="保存" class="button" onclick="addUser()" />

经过这几步后,你会很惊奇的发现,数据库已经多了一条记录。

获取表单中的信息,然后插入到Mysql中 

<%@ page language="java" contentType="text/html charset=gbk"

    pageEncoding="gbk"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%

int id = Integer.parseInt(request.getParameter("id"))

int rootid = Integer.parseInt(request.getParameter("rootid"))

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html charset=gbk">

<title>Replay</title>

</head>

<body>

<form method="post" action="ReplayOK.jsp">

 <input type="hidden" name="id" value="<%=id %>">

 <input type="hidden" name="rootid" value="<%=rootid %>">

<table align="center">

 <tr>

  <td>

   <input type="text" name="title" size="80">

  </td>

 </tr>

 <tr>

  <td>

   <textarea cols="80" rows="20" name="cont"></textarea>

  </td>

 </tr>

 <tr>

  <td>

   <input type="submit" value="提交">

  </td>

 </tr>

</table>

</form>

</body>

</html>

---------------------------------------------------------------

下面接收上面表单中传过来的信息,并插入到mysql中

<%@ page language="java" contentType="text/html charset=gbk"

    pageEncoding="gbk"%>

<%@ page import="java.sql.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%

request.setCharacterEncoding("GBK")

int id = Integer.parseInt(request.getParameter("id"))

int rootid = Integer.parseInt(request.getParameter("rootid"))

String title = request.getParameter("title")

String cont = request.getParameter("cont").replaceAll("\n","<br/>")

Connection conn = null

Statement st = null

Class.forName("com.mysql.jdbc.Driver")

conn = DriverManager.getConnection("jdbc:mysql://localhost/bbs?user=root&password=690115399")

st = conn.createStatement()

conn.setAutoCommit(false)

String sql = "insert into article values(null,?,?,?,?,now(),0)"

PreparedStatement pstmt = conn.prepareStatement(sql)

pstmt.setInt(1,id)

pstmt.setInt(2,rootid)

pstmt.setString(3,title)

pstmt.setString(4,cont)

pstmt.executeUpdate()

st.executeUpdate("update article set isleaf = 1 where id = " + id)

conn.commit()

conn.setAutoCommit(true)

st.close()

pstmt.close()

conn.close()

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html charset=gbk">

<title>Insert title here</title>

</head>

<body>

<%response.sendRedirect("ShowArticleTree.jsp") %>

</body>

</html>

当然最好的方法还是应该用jsp + JavaBean方式。

你的问题我知道了,你想往数据库里插入数据,单纯从jsp页面插入没有现实意义,可以考虑到再编写一个表单页面提交表单数据,在jsp页面用统配符向数据库插入数据。

我大致一个小例子你看看。

zhuce.html

<html>

<body>

<form name="form1" method="post" action="register.jsp">

<p align="center">用户名:

<input type="text" name="name">

</p>

<p align="center">密码:

<input type="password" name="password">

</p>

<p align="center">

<input type="submit" name="Submit" value=" 注册">

</p>

</form>

</body>

</html>

register.jsp

<%@ page contentType="text/htmlcharset=gb2312" language="java" import="java.sql.*" errorPage="" %>

<html>

<body>

<%

request.setCharacterEncoding("GBK")

String name=request.getParameter("name")//内置对象应该会吧

String password=request.getParameter("password")

try{

Class.forName("org.gjt.mm.mysql.Driver")//驱动程序你自己的,我的是com.mysql.jdbc.Driver

String url="jdbc:mysql://localhost:3306/tian"//你自己设置数据库名称

Connection con=DriverManager.getConnection(url,"root","")//如果你mysql中root的密码是空的话最好写成""代替null

String sql="insert into txt (name,password) values ('"+name+"','"+password+"')"//你使用的表是txt,sql建表自己看着办吧

Statement stmt=con.createStatement()

if{

stmt.executeUpdate(sql)

response.sendRedirect("success.html")//根据结果定向成功页面

}else{

response.sendRedirect("f.html")//失败页面

}

}catch(Exception e){

e.printStackTrace()

System.out.println(e)

}

%>

</body>

</html>

至于success.jsp和f.jsp比较简单自己写下吧。

不会了可以上网查资料,或许再提问吧


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9994230.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存