Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Struts
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Struts
Struts JDBC inserting data
ronnie villareal
Greenhorn
Posts: 6
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Im Inserting data to the database, their is no error but when im inserting the data in database theirs no data inserted
my
struts
config
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="UserForm" type="com.myapp.struts.UserForm"/> </form-beans> <global-exceptions> </global-exceptions> <global-forwards> <forward name="welcome" path="/Welcome.do"/> </global-forwards> <action-mappings> <action input="/index.jsp" name="UserForm" path="/User" scope="request" type="com.myapp.struts.UserAction"> <forward name="success" path="/success.jsp"/> </action> </action-mappings>
my index.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html:html xhtml="true"> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <title>Add new user</title> </head> <body> <html:form action="/User" method="POST"> User ID : <input type="text" name="userid" readonly="readonly" maxlength="16" /> First Name : <html:text property="firstName" size="16" maxlength="16"/> <html:errors property="firstname"/> Last Name : <input <html:text property="lastName" size="16" maxlength="16"/> DOB : <input <html:text property="dob" size="16" maxlength="16"/> Email : <html:text property="email" size="16" maxlength="16"/> <input type="submit" value="Submit" /> </html:form> </body> </html:html>
success.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <html> <head> <title>Welcome Registered successfully</title> </head> <body> <h1>Welcome</h1> </body> </html>
Model
package com.daniel.model; public class User { private int userid; private String firstName; private String lastName; private String dob; private String email; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getDob() { return dob; } public void setDob(String dob) { this.dob = dob; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User [userid=" + userid + ", firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + ", email=" + email + "]"; } }
UserDao
package com.strutsexample.dao; import com.strutsexamplel.connection.ConnectionFactory; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.daniel.model.User; import com.myapp.struts.UserForm; public class UserDao { private Connection connection; public UserDao() throws ClassNotFoundException, SQLException { connection = ConnectionFactory.getConnection(); } public void addUser(User user) { try { PreparedStatement preparedStatement = connection .prepareStatement("insert into users(firstname,lastname,dob,email) values (?, ?, ?, ? )"); // Parameters start with 1 preparedStatement.setString(1, user.getFirstName()); preparedStatement.setString(2, user.getLastName()); preparedStatement.setString(3, user.getDob()); preparedStatement.setString(4, user.getEmail()); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } }
ConnectionFactory
package com.strutsexamplel.connection; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.sql.*; import javax.naming.*; import javax.sql.*; /** * * @author villareal_ronnie */ public class ConnectionFactory { public static Connection getConnection() throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/user"; Connection conn = DriverManager.getConnection(url, "root", "secret"); return conn; } }
UserForm
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.myapp.struts; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; /** * * @author ron */ public class UserForm extends org.apache.struts.action.ActionForm { private int userid; private String firstName; private String lastName; private String dob; private String email; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getDob() { return dob; } public void setDob(String dob) { this.dob = dob; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User [userid=" + userid + ", firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + ", email=" + email + "]"; } /** * */ public UserForm() { super(); // TODO Auto-generated constructor stub } /** * This is the action called from the Struts framework. * @param mapping The ActionMapping used to select this instance. * @param request The HTTP Request we are processing. * @return */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if (firstName == null || firstName.trim().equals("")) { errors.add("firstName", new ActionMessage("error.cust.firstname.empty")); } return errors; } }
UserAction
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.myapp.struts; import com.daniel.model.User; import com.strutsexample.dao.UserDao; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * * @author ron */ public class UserAction extends org.apache.struts.action.Action { /* forward name="success" path="" */ private static final String SUCCESS = "success"; /** * This is the action called from the Struts framework. * @param mapping The ActionMapping used to select this instance. * @param form The optional ActionForm bean for this request. * @param request The HTTP Request we are processing. * @param response The HTTP Response we are processing. * @throws java.lang.Exception * @return */ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UserForm myUserForm = (UserForm) form; UserDao dao = new UserDao(); User user = new User(); if (myUserForm != null) { user.setFirstName(myUserForm.getFirstName()); user.setLastName(myUserForm.getLastName()); user.setDob(myUserForm.getDob()); user.setEmail(myUserForm.getEmail()); dao.addUser(user); return mapping.findForward(SUCCESS); } return null; } }
needs help thanks in advance
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Struts Validatons are not working
javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items"
No getter method for property
Actionforms and Actions
org.apache.jasper.JasperException: java.lang.NullPointerException: Module 'null' not found.
More...