2017-05-05 64 views
1

爲什麼不將數據存儲到jsp中的數據庫中?在下面的代碼中找到錯誤。我想從表單字段獲取數據並從jsp頁面獲取數據並存儲到數據庫中。如何從jsp中的表中將數據插入數據庫中

數據庫名稱=測試表名=信息列名=名字,姓氏

<%@page import="java.io.*,java.util.*,java.sql.*" %> 
<%@page import="javax.servlet.*,javax.servlet.http.*" %> 
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>connection with database</title> 
    </head> 
    <body> 
     String f_name = request.getParameter("f"); 
     String l_name = request.getParameter("l"); 
     <sql:setDataSource var="sqldata" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/test" user="root" password="" /> 
     <sql:update dataSource="${sqldata}" var="result1" > 
      INSERT INTO info(first_name,last_name)VALUES('f_name','l_name'); 
     </sql:update>--%> 


      <input type="text" name="f" /><br /> 
      <input type="text" name="l" /><br /> 
      <input type='submit' /> 
    </body> 
</html> 
+0

你的連接字符串說MySQL的,所以我變成它從SQL Server –

回答

1

都沒有碰過JSP很長一段時間。如果我的記憶是正確的,它不應該是這樣嗎?

<%@page import="java.io.*,java.util.*,java.sql.*" %> 
<%@page import="javax.servlet.*,javax.servlet.http.*" %> 
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%   
     String f_name = request.getParameter("f");   
     String l_name = request.getParameter("l"); 
     pageContext.setAttribute("f_name", f_name); 
     pageContext.setAttribute("l_name", l_name); 
%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>connection with database</title> 
    </head> 
    <body> 
     <c:if test="${f_name != null}"> 
     <sql:setDataSource var="sqldata" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/test" user="root" password="" /> 
     <sql:update dataSource="${sqldata}" var="result1"> 
      INSERT INTO info (first_name,last_name) VALUES (?,?); 
      <sql:param value="${f_name}" /> 
      <sql:param value="${l_name}" /> 
     </sql:update> 
     </c:if> 

      <input type="text" name="f" /><br /> 
      <input type="text" name="l" /><br /> 
      <input type='submit' /> 
    </body> 
</html> 
+0

org.apache.jasper.JasperException:javax.servlet.ServletException:javax.servlet.jsp.JspException: INSERT INTO Infor公司(FIRST_NAME, last_name)VALUES(?,?); :柱 '如first_name' 不能爲空 \t org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:601) \t org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466 ) \t org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) \t org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) – muneeb

+0

參見更新的代碼。 – wannadream

+0

非常感謝主席先生。這是工作本身。上帝保佑你和你的家人 – muneeb

相關問題