web前段对数据库执行的insert插入操作

8 篇文章 0 订阅

 数据库的insert插入

一开始想使用action操作

但是后来一直报“request available”

后来直接在jsp上进行数据库的插入操作

<form action="lookFriends.jsp" class="smart-form">	

直接链接到显示的jsp上
一开始加载addnewbook.jsp上发现执行顺序为

网页-action(数据未写入执行insert-值为null)-submit-数据传入但没有执行insert操作
<!-- widget content -->
									<div class="widget-body no-padding">
				
										<form action="lookFriends.jsp" class="smart-form">										
											<%
											DB mysql = new DB();
											 String userName = mysql.returnLogin(request);
											
		%>
											
											<fieldset>										
											<!--  	<section>-->
													<label class="label">New Book Number</label>
													<label class="input">
														<input type="text" name="idbookinformation" value="" class="input-sm">
													</label>
												<!--  	</section>-->		
											<!--  	<section>-->
													<label class="label">Book ISBN(PK)</label>
													<label class="input">
														<input type="text" name="ISBNnum" value="" class="input-sm">
													</label>
											<!--  	</section>-->		
											<!--  	<section>-->
													<label class="label">Book Title</label>
													<label class="input">
														<input type="text" name="newbookname" value="" class="input-sm" maxlength="10">
													</label>
											<!--  	</section>-->
																				
									<!--  	<section>-->
													<label class="label">Author ID(FK)</label>
													<label class="input">
														<input type="text" name="author" value="" class="input-sm">
													</label>
											<!--  	</section>-->
												
										<!--  	<section>-->
													<label class="label">Publisher</label>
													<label class="input">
														<input type="text" name="publisher" value="" class="input-sm">
													</label>
												<!--  	</section>-->
												
											<!--  	<section>-->
													<label class="label">Publish Date</label>
													<label class="input">
														<input type="text" name="date" value="" class="input-sm">
													</label>
												<!--  	</section>-->
												
									<!--  	<section>-->
													<label class="label">Price</label>
													<label class="input">
														<input type="text" name="price" value="" class="input-sm">
													</label>
											<!--  	</section>-->
											
											</fieldset>
																							
											
											<td colspan= "2" align="center">
												
												<input type="submit" value="finish" size="12">
												<input type="reset" value="clear" size="12">
											</td>
											
										</form>
										<%
											String newbookname=request.getParameter("newbookname");
											String price = request.getParameter("price");
											String date = request.getParameter("date");
									        String author =request.getParameter("author");
										String publisher =request.getParameter("publisher");
											String ISBN =request.getParameter("ISBNnum");
											String Number =request.getParameter("idbookinformation");
											System.out.println(ISBN);
											//String fri = mysql.insertFri(request, userName, newbookname, price, date, author, publisher,ISBN,Number);
											%>
									</div>
结果为
null
1
null
insert
insert
所以进行了修改

<!-- widget content -->
									<div class="widget-body no-padding">
				
										<form action="lookFriends.jsp" class="smart-form">										
											<%
											//DB mysql = new DB();
											// String userName = mysql.returnLogin(request);
											
		%>
											
											<fieldset>										
											<!--  	<section>-->
													<label class="label">New Book Number</label>
													<label class="input">
														<input type="text" name="idbookinformation" value="" class="input-sm">
													</label>
												<!--  	</section>-->		
											<!--  	<section>-->
													<label class="label">Book ISBN(PK)</label>
													<label class="input">
														<input type="text" name="ISBNnum" value="" class="input-sm">
													</label>
											<!--  	</section>-->		
											<!--  	<section>-->
													<label class="label">Book Title</label>
													<label class="input">
														<input type="text" name="newbookname" value="" class="input-sm" maxlength="10">
													</label>
											<!--  	</section>-->
																				
									<!--  	<section>-->
													<label class="label">Author ID(FK)</label>
													<label class="input">
														<input type="text" name="author" value="" class="input-sm">
													</label>
											<!--  	</section>-->
												
										<!--  	<section>-->
													<label class="label">Publisher</label>
													<label class="input">
														<input type="text" name="publisher" value="" class="input-sm">
													</label>
												<!--  	</section>-->
												
											<!--  	<section>-->
													<label class="label">Publish Date</label>
													<label class="input">
														<input type="text" name="date" value="" class="input-sm">
													</label>
												<!--  	</section>-->
												
									<!--  	<section>-->
													<label class="label">Price</label>
													<label class="input">
														<input type="text" name="price" value="" class="input-sm">
													</label>
											<!--  	</section>-->
											
											</fieldset>
																							
											
											<td colspan= "2" align="center">
												
												<input type="submit" value="finish" size="12">
												<input type="reset" value="clear" size="12">
											</td>
											
										</form>
										
									</div>
									<!-- end widget content -->



将insert操作插入到目标 lookFriends.jsp里面
<!-- widget content -->
									<div class="widget-body no-padding">

										<table class="table table-bordered">
											<thead>
												<tr>
													<th>Book Number</th>
													<th> ISBN(PK)</th>
													<th> Title</th>
													<th> Author(ID)</th>
													<th> Publisher</th>
													<th> PublishDate</th>
													<th> Price</th>
												</tr>
											</thead>
											<tbody>
											<%
											DB mysql = new DB();
											String fri;
											 String userName = mysql.returnLogin(request);
											String newbookname=request.getParameter("newbookname");
											String price = request.getParameter("Price");
											String date = request.getParameter("date");
											String author =request.getParameter("author");
											String publisher =request.getParameter("publisher");
											String ISBN =request.getParameter("ISBNnum");
											String Number =request.getParameter("idbookinformation");
											System.out.println(ISBN);
											if(newbookname != null)
												fri = mysql.insertFri(request, userName, newbookname, price, date, author, publisher,ISBN,Number);
											
											
											
											DB mysql1 = new DB();
											String userName1 = mysql.returnLogin(request);
											
											ResultSet rs = mysql.selectFriAll(request, userName);
											String fri1 = mysql.myFriends(request,userName);
											ArrayList friends= (ArrayList)session.getAttribute("friends");
											System.out.println("1");
											if(friends == null|| friends.size() == 0){
											%>
											<h1>书籍库中未有书籍</h1>
											<%
											}else{
												for(int i=friends.size()-1;i>=0;i--)
												{
													MyFriBean ff =(MyFriBean)friends.get(i);
												
											%>
											
													<tr>
													<th><%=ff.getName()%></th>
													<th><%=ff.getISBN()%></th>
													<th><%=ff.getName()%></th>
													<th><%=ff.getauthor()%></th>
													<th><%=ff.getpublisher()%></th>
													<th><%=ff.getdate()%></th>
													<th><%=ff.getprice()%></th>
												</tr>
											<%
												}
											}
											%>
											
											</tbody>
										</table>
										
										
									</div>
									<!-- end widget content -->

这样执行顺序变成
addnewbook页面显示-submit执行输入传输-显示界面执行insert

其中要注意的是要进行判断,因为直接执行显示界面会有一个null被直接加进来(没有传入)
所以进行判断
if。。。
其中进行判重和判空



insert操作
public String insertFri(HttpServletRequest request,String userName,String newBookName,String Price,String Date,String authorid,String publisher,String ISBN,String bookNumber)
	{
		System.out.println("insert");
		try{
			String sure = null;
			rs = selectFri(request,userName,newBookName);
			if(rs.next())
			{
				sure = "one";
			}
			else{
				String sql ="insert into bookinformation"+" (userName,BookName,ISBN,publisher,Date,Price,Authorid,idbookinformation)"+" values("+"'"+userName+"'"+","+"'"+newBookName+"'"+","+"'"+ISBN+"'"+","+"'"+publisher+"'"+","+"'"+Date+"'"+","+"'"+Price+"'"+","+"'"+authorid+"'"+","+"'"+bookNumber+"'"+")";
				st = getStatement();
				int row=st.executeUpdate(sql);
				if(row==1)
				{
					//调用myfriend方法,更新session中书籍信息
					String fri = myFriends(request,userName);
					if(fri.equals("ok"))
					{
						sure = "ok";
					}
					else
					{
						sure = null;
					}
				}
				else
				{
					sure = null;
				}
			}
			return sure;
		}catch(Exception e){
			e.printStackTrace();
			return null;
					}
							
	}
	


最后!数据库的标签不能带()!  author(ID)是不行的,还是要换成 authorid  不然在sql语句中会报错误

数据库标签不适用标准字符


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值