• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

jsp向mysql插入记录的问题

mysql 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论

mysqljspsql

通过第一个页面向第二个页面post文本框的值,然后通过第二个页面向数据库添加信息
第一个页面的代码如下 function Judge()
{if(document.form.p_name.value==””)
{window.alert(“产品名称不能为空!”);document.form本文来源gaodai$ma#com搞$$代**码网$.p_name.focus();return false;}
if(document.form.p_type.value==””)
{window.alert(“产品类型不能为空!”);document.form.p_type.focus();return false;}
}

第二个页面代码为
try {Class.forName(“com.mysql.jdbc.Driver”);}
catch(Exception e){e.printStackTrace();}
Connection conn;ResultSet rs;Statement stmt;
try
{String database=”mydb”;
String url=”jdbc:mysql://localhost:3306/”+database;
conn=DriverManager.getConnection(url,”root”,”root”);
String name=request.getParameter(“p_name”);
String type=request.getParameter(“p_type”);
String detail=request.getParameter(“p_detail”);
stmt=conn.createStatement();
String sql=”insert into product(p_name,p_type,p_detail)values(‘”+name+”‘,'”+type+”‘,'”+detail+”‘)”;
stmt.executeUpdate(sql);
int i=stmt.executeUpdate(sql);
if(i!=0){out.print(“添加成功”);response.setHeader(“refresh”, “2;URL=addproduct2.jsp”);}
else{out.print(“添加失败”);response.setHeader(“refresh”, “2;URL=addproduct2.jsp”);}
}
catch(Exception e){e.printStackTrace();
out.print(“

");<br>e.printStackTrace(new java.io.PrintWriter(out));<br>out.print("

“);}

%>

为啥输入产品123 类型123会向数据库中添加两条一模一样的记录呢,问题出在哪里

产品名称
产品类型
产品详情
产品图片

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:jsp向mysql插入记录的问题

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址