JSP如何修改MYSQL的数据?(内详)

JSP如何修改MYSQL的数据?(内详),第1张

用executeUpdate(SQL语句),

有DELETE,EDIT,DROP什么的,具体见SQL命令大全:

http://edu.codepub.com/2010/0128/20192.php

你的问题我知道了,你想往数据库里插入数据,单纯从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比较简单自己写下吧。

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

知道有一种形式:

之前得封装下2个文件,页面引用:

<%@ taglib uri="/WEB-INF/database.tld" prefix="database"%>

<%@ taglib uri="/WEB-INF/beans.tld" prefix="beans"%>

<beans:tagHandle id="tagID">

<database:prepareStatement id="<%=tagID%>" scope="page" connStr="gongda">

添加或删除sql

</database:prepareStatement>

<database:executePreparedStatement id="<%=tagID%>" scope="page" update="true" variables=""/>

<database:rows id="list" query="<%=tagID%>">

</database:rows>

<database:release query="<%=tagID%>"/>

</beans:tagHandle>


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

原文地址:https://54852.com/bake/11668134.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存