如何从表单中检索数据并通过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");
}
}
}
view.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 is temporarily unavailable并应用该规则,但Liferay说我错了。
存储的正确程序是什么?
答案 0 :(得分:0)
你将actionURL Invio定义为view.jsp中的java对象,但你引用它是错误的。 你应该在&lt;%=%&gt;中引用它标签。实际上你的表格标签应如下所示:
<form method="post" action='<%=Invio%>'>
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Invia!">
答案 1 :(得分:0)
请检查表单标记的操作属性值。
您可以通过多种方式使用。
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>
第三个我不喜欢。
选择是你的......