我试图在数据库中插入值动态值输入数据库但是因为null不了解卡在哪里也附加图像以便更好地理解以前的值没有插入但是在更改html页面值插入但是因为无法理解为什么值插入null或问题是request.parameter values?
这是我的HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
function getinput()
{
var employee_id="<br>Employee_id : <input type='text' name='txt_employeeid'>";
var Project_name=" Project_name : <input type='text' name='txt_project_name'>";
var Header=" Header : <input type='text' name='txt_Header'>";
var Department=" Department : <input type='text' name='txt_Department'>";
var Description=" Department : <input type='text' name='txt_description'>";
$("#inputs").append(employee_id,Project_name,Header,Department,Description);
}
</script>
</head>
<body>
<div id="inputs" align="left">
<button onclick="getinput()">Add</button><br>
</div>
<input type="hidden" name="txtemployeeid" value="employee_id"><br>
<form action="EmployeeVendorValidation.jsp" method="post">
<input type="submit" name="btnSubmit" value="Submit"/><br><br>
</form>
</body>
</html>
My jsp where i tried to enter details in database:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String sbm=request.getParameter("btnSubmit");
String employee_id=request.getParameter("employee_id");
String txt_project_name=request.getParameter("Project_name");
String get_header=request.getParameter("Header");
String get_department=request.getParameter("Department");
String get_description=request.getParameter("Description");
if(sbm!=null)
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@173.18.114.213:1821:godb","xe","Spacess");
PreparedStatement ps=con.prepareStatement("insert into Employee_task values(?,?,?,?,?)");
ps.setString(1, employee_id);
ps.setString(2, txt_project_name);
ps.setString(3, get_header);
ps.setString(4, get_department);
ps.setString(5, get_description);
ps.executeUpdate();
out.print("data inserted");
}
%>
</body>
</html>
[![enter image description here][1]][1]
答案 0 :(得分:1)
原因也与您之前的问题类似。
在您的html代码中,参数名称都以text
开头:
var employee_id="<br>Employee_id : <input type='text' name='txt_employeeid'>";
var Project_name=" Project_name : <input type='text' name='txt_project_name'>";
var Header=" Header : <input type='text' name='txt_Header'>";
var Department=" Department : <input type='text' name='txt_Department'>";
var Description=" Department : <input type='text' name='txt_description'>";
但是在获取参数的jsp代码中,参数名称不能保持不变:
String employee_id=request.getParameter("employee_id");
String txt_project_name=request.getParameter("Project_name");
String get_header=request.getParameter("Header");
String get_department=request.getParameter("Department");
String get_description=request.getParameter("Description");
为了让它工作,您需要保持参数名称相同,将它们更改为:
String employee_id=request.getParameter("txt_employeeid");
String txt_project_name=request.getParameter("txt_project_name");
String get_header=request.getParameter("txt_Header");
String get_department=request.getParameter("txt_Department");
String get_description=request.getParameter("txt_description");
您还需要更改HTML代码设计
$("#inputs").append(employee_id,Project_name,Header,Department,Description);
上面的代码是向输入添加元素,但在form
内部,因此值始终为null。
<div id="inputs" align="left"> <!-- add element to this div will not inside the form -->
<button onclick="getinput()">Add</button><br>
</div>
<input type="hidden" name="txtemployeeid" value="employee_id"><br>
<form action="EmployeeVendorValidation.jsp" method="post">
<input type="submit" name="btnSubmit" value="Submit"/><br><br>
</form>
由于你不知道怎么做,你可以用下面的东西,现在应该可以工作:
将append
更改为html
:
$("#inputs").html(employee_id,Project_name,Header,Department,Description);
重新设计您的html页面如下:
<form action="EmployeeVendorValidation.jsp" method="post">
<div id="inputs" align="left"></div>
<input type="hidden" name="txtemployeeid" value="employee_id"><br>
<button onclick="getinput()">Add</button><br>
<input type="submit" name="btnSubmit" value="Submit"/><br><br>
</form>