Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从web应用程序添加到数据库?_Java_Database_Spring_Spring Mvc - Fatal编程技术网

Java 如何从web应用程序添加到数据库?

Java 如何从web应用程序添加到数据库?,java,database,spring,spring-mvc,Java,Database,Spring,Spring Mvc,我正在尝试从SpringWeb应用程序添加到数据库中。web应用程序有一个简单的jsp页面,用户可以在其中输入详细信息,然后点击提交按钮。单击按钮后,结果应显示在刷新的数据库中。我已经编写了基本的java类,比如包含插入特定数据库的SQL语句的类。我只是不知道如何将submit按钮链接到添加到数据库中的特定java类 WebController.java @RequestMapping(value = "/addQuestion", method = RequestMethod.POST)

我正在尝试从SpringWeb应用程序添加到数据库中。web应用程序有一个简单的jsp页面,用户可以在其中输入详细信息,然后点击提交按钮。单击按钮后,结果应显示在刷新的数据库中。我已经编写了基本的java类,比如包含插入特定数据库的SQL语句的类。我只是不知道如何将submit按钮链接到添加到数据库中的特定java类

WebController.java

 @RequestMapping(value = "/addQuestion", method = RequestMethod.POST)
        public String addQuestion(@RequestParam(value="question", required = true)  String theQuestion , @RequestParam(value=" questionId", required = true)  Integer questionId, @RequestParam(value="category", required = true)   String category) throws SQLException{
                ViewController viewController = new ViewController();
                viewController.createQuestion(questionId, theQuestion, category, 
                return "qFour";
        }
public Question createQuestion(int questionId, String theQuestion, String category, String correctAnswer) throws SQLException{
        Question question = questionController.addQuestion(questionId, theQuestion, category, correctAnswer);
        return question;
    }
public Question addQuestion(int questionId, String theQuestion, String category, String correctAnswer)throws SQLException{

       Question question = questionFactory.createQuestion();
       question.setQuestionId(questionId);
       question.setTheQuestion(theQuestion);
       question.setCategory(category);
       question.setCorrectAnswer(correctAnswer);

       qdao.addQuestion(questionId, theQuestion, category, correctAnswer);


       return question;
   }
ViewController.java

 @RequestMapping(value = "/addQuestion", method = RequestMethod.POST)
        public String addQuestion(@RequestParam(value="question", required = true)  String theQuestion , @RequestParam(value=" questionId", required = true)  Integer questionId, @RequestParam(value="category", required = true)   String category) throws SQLException{
                ViewController viewController = new ViewController();
                viewController.createQuestion(questionId, theQuestion, category, 
                return "qFour";
        }
public Question createQuestion(int questionId, String theQuestion, String category, String correctAnswer) throws SQLException{
        Question question = questionController.addQuestion(questionId, theQuestion, category, correctAnswer);
        return question;
    }
public Question addQuestion(int questionId, String theQuestion, String category, String correctAnswer)throws SQLException{

       Question question = questionFactory.createQuestion();
       question.setQuestionId(questionId);
       question.setTheQuestion(theQuestion);
       question.setCategory(category);
       question.setCorrectAnswer(correctAnswer);

       qdao.addQuestion(questionId, theQuestion, category, correctAnswer);


       return question;
   }
QuestionController.java

 @RequestMapping(value = "/addQuestion", method = RequestMethod.POST)
        public String addQuestion(@RequestParam(value="question", required = true)  String theQuestion , @RequestParam(value=" questionId", required = true)  Integer questionId, @RequestParam(value="category", required = true)   String category) throws SQLException{
                ViewController viewController = new ViewController();
                viewController.createQuestion(questionId, theQuestion, category, 
                return "qFour";
        }
public Question createQuestion(int questionId, String theQuestion, String category, String correctAnswer) throws SQLException{
        Question question = questionController.addQuestion(questionId, theQuestion, category, correctAnswer);
        return question;
    }
public Question addQuestion(int questionId, String theQuestion, String category, String correctAnswer)throws SQLException{

       Question question = questionFactory.createQuestion();
       question.setQuestionId(questionId);
       question.setTheQuestion(theQuestion);
       question.setCategory(category);
       question.setCorrectAnswer(correctAnswer);

       qdao.addQuestion(questionId, theQuestion, category, correctAnswer);


       return question;
   }
QuestionsDAO.java

public void addQuestion(int questionID, String question, String category, String correctAnswer)throws SQLException{

        Connection connection = connFactory.getConnection();
        String query = "INSERT INTO es_rm_questions (questionID, question, category, correctAnswer) VALUES (?, ?, ?, ?)";
        PreparedStatement prepState = connection.prepareStatement(query);
        prepState.setInt(1, questionID);
        prepState.setString(2, question);
        prepState.setString(3, category);
        prepState.setString(4, correctAnswer);

        int numberOfRowsUpdated = prepState.executeUpdate();
        prepState.close();
        connection.close();
    }
qOne.jsp

<form:form method="GET" action="addQuestion">

    <input type="text" name="questionId" value="">Enter Id<br>
    <input type="text" name="theQuestion" value="">Enter Q <br>
    <input type="text" name="category" value="">Enter Category<br>
    <input type="text" name="correctAnswer" value="">Enter correct answer<br>
             <input type="submit" value="Next" >
</form:form>
web.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
       <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

下一行将POST请求绑定到路径YOUAPPCONTEXT/addQuestion

@RequestMapping(value = "/addQuestion", method = RequestMethod.POST)
因此,您只需将表单提交到指定的路径, 但是您的方法中没有参数。例如,如果表单具有名为question、category、questionId的输入字段,则应按如下方式定义方法

@RequestMapping(value = "/addQuestion", method = RequestMethod.POST)
        public String addQuestion(@RequestParam(value="question", required = true)  String theQuestion , @RequestParam(value=" questionId", required = true)  Integer questionId, @RequestParam(value="category", required = true)   String category) throws SQLException{
                ViewController viewController = new ViewController();
                viewController.createQuestion(questionId, theQuestion, category, 
                return "qFour";
        }

向我们展示您尝试过的代码。@TomJonckheere已添加到问题中。您已在注释中编辑了我的代码以匹配此代码,运行该代码但得到404。有什么想法吗?或者需要查看更多代码?你有form:form-method=GET,但你应该有form:form-method=post。我已经更改了,但是我仍然在浏览器中获得404。我已经完全按照上面的指示完成了,但是发生的是,当我单击submit时,它不会返回qFour,而是返回addQuestion,即它当前所在的页面,因此它会清除所有当前输入。你知道为什么吗?或者我做错了什么?