1

如何从表单中检索数据并通过 JDBC 将其发送到 MySQL 数据库?

这是我的程序:

主类:

public class Dati extends MVCPortlet {

    public static Connection con() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception ex) {
            System.out.println("Errore");
        }

        // APRI CONNESSIONE

        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost/db_alex","root","25071984");
            System.out.println("Connessione effettuata");
            return conn;
        } catch (SQLException ex) {
            System.out.println("SQlException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
            return null;
        }
    }

    public void Invio (ActionRequest actionRequest, ActionResponse actionResponse) 
    throws PortletException, IOException {
        String username = actionRequest.getParameter("username");
        String password = actionRequest.getParameter("password");

        Connection conn = null;

        try {

        Statement st = conn.createStatement();
        st.executeUpdate("INSERT INTO contacts (username,password) values ('"+username+"','"+password+"')");
        System.out.println("Inserimento avvenuto con successo");
        } catch (Exception e) {
            System.out.println("Inserimento non avvenuto");
        }
    }
}

视图.jsp

<%
/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
%>

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />

<portlet:actionURL name="Invio" var="Invio"/>

<form method="post" action="actionURL/>">

    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="Invia!">    

</form> 

我不知道我的错在哪里。我正在阅读Portlet 暂时不可用并应用该规则,但 Liferay 说我错了。

哪个是正确的存储程序?

4

2 回答 2

0

您在 view.jsp 中将 actionURL Invio 定义为 java 对象,但您错误地引用了它。您应该在 <%= %> 标记中引用它。实际上你的表单标签应该是这样的:

<form method="post" action='<%=Invio%>'>

<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Invia!">    

于 2012-11-19T17:36:54.347 回答
0

请检查表单标签的 action 属性值。

您可以通过多种方式使用。

1.  <form method="post" action="<portlet:actionURL/>">
       <input type="text" name="username">
       <input type="password" name="password">
       <input type="submit" value="Invia!">    
   </form> 

2.

    <form method="post" action="${Invio}">
       <input type="text" name="username">
       <input type="password" name="password">
       <input type="submit" value="Invia!">    
    </form> 

3.

    <form method="post" action="<%=Invio%>">
       <input type="text" name="username">
       <input type="password" name="password">
       <input type="submit" value="Invia!">    
    </form> 

第三个我不喜欢。

选择是你的......

于 2013-10-22T17:47:16.847 回答