Home > Database > Mysql Tutorial > body text

jsp向mysql插入记录的问题

WBOY
Release: 2016-06-06 09:43:35
Original
938 people have browsed it

mysqljspsql

通过第一个页面向第二个页面post文本框的值,然后通过第二个页面向数据库添加信息
第一个页面的代码如下 function Judge()
{if(document.form.p_name.value=="")
{window.alert("产品名称不能为空!");document.form.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("
Copy after login
");}

%>

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

产品名称
产品类型
产品详情
产品图片
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template