jsp向mysql插入记录的问题

WBOY
リリース: 2016-06-06 09:43:35
オリジナル
904 人が閲覧しました

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("
ログイン後にコピー
");}

%>

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

产品名称
产品类型
产品详情
产品图片
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!