wood burning stoves*
The moose likes Struts and the fly likes How to connect a SQL Server database with struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to connect a SQL Server database with struts" Watch "How to connect a SQL Server database with struts" New topic
Author

How to connect a SQL Server database with struts

yuqing ma
Greenhorn

Joined: Nov 10, 2004
Posts: 11
I try to connect a SQL Server database with struts,but I failed.please help me correct it.The example:

struts-config.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>

<!-- ========== Data Source Configuration =============================== -->
<data-sources />

<!-- ========== Form Bean Definitions ================================== -->
<form-beans>
<form-bean name="ownerForm" type="com.youcompany.struts.form.OwnerForm">
<form-property name="greet" type="java.lang.String" initial="Mr." />
<form-property name="address" type="java.lang.String" />
<form-property name="email" type="java.lang.String" />
<form-property name="tel" type="java.lang.String" />
<form-property name="name" type="java.lang.String" />
</form-bean>

</form-beans>

<!-- ========== Global Exception Definitions ============================== -->
<global-exceptions />

<!-- ========== Global Forward Definitions =============================== -->
<global-forwards>
<forward name="failure" path="/failure.jsp" />

</global-forwards>

<!-- ========== Action Mapping Definitions =============================== -->
<action-mappings>
<action
attribute="ownerForm"
input="/owner.jsp"
name="ownerForm"
path="/owner"
scope="request"
type="com.youcompany.struts.action.OwnerAction"
validate="false">
<forward name="success" path="/success.jsp" />
</action>

</action-mappings>

<!-- ========== Controller Configuration ================================ -->
<controller />

<!-- ========== Message Resources Definitions ============================ -->

<!-- ========== Plug Ins Configuration ================================= -->
<message-resources parameter="com.youcompany.struts.ApplicationResources" />
</struts-config>

OwnerForm.java as follows:

// Created by Xslt generator for Eclipse.
// XSL : not found (java.io.FileNotFoundException: (No such file or directory))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl

package com.youcompany.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
* OwnerForm.java created by EasyStruts - XsltGen.
* http://easystruts.sf.net
* created on 12-23-2004
*
* XDoclet definition:
* @struts:form name="ownerForm"
*/
public class OwnerForm extends ActionForm {

// --------------------------------------------------------- Instance Variables

/** greet property */
private String greet = "Mr.";

/** address property */
private String address;

/** email property */
private String email;

/** tel property */
private String tel;

/** name property */
private String name;

// --------------------------------------------------------- Methods

/**
* Method validate
* @param ActionMapping mapping
* @param HttpServletRequest request
* @return ActionErrors
*/
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {

throw new UnsupportedOperationException("Generated method 'validate(...)' not implemented.");
}

/**
* Method reset
* @param ActionMapping mapping
* @param HttpServletRequest request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
greet = "Mr.";
address = "";
email = "";
tel = "";
name = "";

}

/**
* Returns the greet.
* @return String
*/
public String getGreet() {
return greet;
}

/**
* Set the greet.
* @param greet The greet to set
*/
public void setGreet(String greet) {
this.greet = greet;
}

/**
* Returns the address.
* @return String
*/
public String getAddress() {
return address;
}

/**
* Set the address.
* @param address The address to set
*/
public void setAddress(String address) {
this.address = address;
}

/**
* Returns the email.
* @return String
*/
public String getEmail() {
return email;
}

/**
* Set the email.
* @param email The email to set
*/
public void setEmail(String email) {
this.email = email;
}

/**
* Returns the tel.
* @return String
*/
public String getTel() {
return tel;
}

/**
* Set the tel.
* @param tel The tel to set
*/
public void setTel(String tel) {
this.tel = tel;
}

/**
* Returns the name.
* @return String
*/
public String getName() {
return name;
}

/**
* Set the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}

}

OwnerAction.java as follows:

// Created by Xslt generator for Eclipse.
// XSL : not found (java.io.FileNotFoundException: (No such file or directory))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl

package com.youcompany.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.DataSource;
import java.sql.SQLException;
import com.youcompany.struts.form.OwnerForm;

/**
* OwnerAction.java created by EasyStruts - XsltGen.
* http://easystruts.sf.net
* created on 12-23-2004
*
* XDoclet definition:
* @struts:action path="/owner" name="ownerForm" input="/owner.jsp" validate="true"
* @struts:action-forward name="/success.jsp" path="/success.jsp"
*/
public class OwnerAction extends Action {

// --------------------------------------------------------- Instance Variables

// --------------------------------------------------------- Methods

/**
* Method execute
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
OwnerForm ownerForm = (OwnerForm) form;
String greet = ownerForm.getGreet();
String name = ownerForm.getName();
request.setAttribute("name", name);
request.setAttribute("greet", greet);
String address = ownerForm.getAddress();
String email = ownerForm.getEmail();
String tel = ownerForm.getTel();

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

DataSource dataSource = (DataSource)servlet.getServletContext().getAttribute(
"org.apache.struts.action.DATA_SOURCE");
try {
conn = dataSource.getConnection();
stmt = conn.createStatement();
int id = 0;
rs = stmt.executeQuery("select max(id) as counter from owner");
while(rs.next()){
id = rs.getInt("counter");
}
id += 1;
stmt.executeUpdate("insert into owner values("+id+
", '"+greet+"', '"+name+"', '"+email+"', '"+address+"', "+tel+")");
rs.close();
stmt.close();
conn.close();
}
catch(SQLException e){
throw new SQLException("database error");
}

return (mapping.findForward("success"));

}

}
// Created by Xslt generator for Eclipse.
// XSL : not found (java.io.FileNotFoundException: (No such file or directory))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl

package com.youcompany.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.DataSource;
import java.sql.SQLException;
import com.youcompany.struts.form.OwnerForm;

/**
* OwnerAction.java created by EasyStruts - XsltGen.
* http://easystruts.sf.net
* created on 12-23-2004
*
* XDoclet definition:
* @struts:action path="/owner" name="ownerForm" input="/owner.jsp" validate="true"
* @struts:action-forward name="/success.jsp" path="/success.jsp"
*/
public class OwnerAction extends Action {

// --------------------------------------------------------- Instance Variables

// --------------------------------------------------------- Methods

/**
* Method execute
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
OwnerForm ownerForm = (OwnerForm) form;
String greet = ownerForm.getGreet();
String name = ownerForm.getName();
request.setAttribute("name", name);
request.setAttribute("greet", greet);
String address = ownerForm.getAddress();
String email = ownerForm.getEmail();
String tel = ownerForm.getTel();

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

DataSource dataSource = (DataSource)servlet.getServletContext().getAttribute(
"org.apache.struts.action.DATA_SOURCE");
try {
conn = dataSource.getConnection();
stmt = conn.createStatement();
int id = 0;
rs = stmt.executeQuery("select max(id) as counter from owner");
while(rs.next()){
id = rs.getInt("counter");
}
id += 1;
stmt.executeUpdate("insert into owner values("+id+
", '"+greet+"', '"+name+"', '"+email+"', '"+address+"', "+tel+")");
rs.close();
stmt.close();
conn.close();
}
catch(SQLException e){
throw new SQLException("database error");
}

return (mapping.findForward("success"));

}

}

HTTP Status 503 - Servlet action is currently unavailable
type Status report
message: Servlet action is currently unavailable
description The requested service (Servlet action is currently unavailable) is not currently available.
Apache Tomcat/4.1.18

I don't know where is wrong.
Please help me,everyone,thanks in advance.
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
Inside your struts-config file you need to define your datasource(well there are other ways to do it, but this one seems fairly easy)


several of the values in there are dependant on what database software your using and well userids and passwords you selected yourself.

you can also use:


Replace url, username, password with the ones specific to your database.
If your not using mysql in the Class.forName method refer to the driver for your database.

Hope that helps.

-Tad
yuqing ma
Greenhorn

Joined: Nov 10, 2004
Posts: 11
I am sorry,I pasted a wrong struts-config.xml.Really,my struts-config.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>

<!-- ========== Data Source Configuration =============================== -->
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="password" value="123456" />
<set-property property="minCount" value="1" />
<set-property property="maxCount" value="10" />
<set-property property="user" value="sa" />
<set-property property="driverClass" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<set-property property="description" value="CRM database" />
<set-property property="url" value="jdbc:microsoft:sqlserver://myq:1433;DatabaseName=crm" />
<set-property property="readOnly" value="false" />
<set-property property="autoCommit" value="true" />
<set-property property="loginTimeout" value="" />
</data-source>
</data-sources>

<!-- ========== Form Bean Definitions ================================== -->
<form-beans>
<form-bean name="ownerForm" type="com.youcompany.struts.form.OwnerForm">
<form-property name="greet" type="java.lang.String" initial="Mr." />
<form-property name="address" type="java.lang.String" />
<form-property name="email" type="java.lang.String" />
<form-property name="tel" type="java.lang.String" />
<form-property name="name" type="java.lang.String" />
</form-bean>

</form-beans>

<!-- ========== Global Exception Definitions ============================== -->
<global-exceptions />

<!-- ========== Global Forward Definitions =============================== -->
<global-forwards>
<forward name="failure" path="/failure.jsp" />

</global-forwards>

<!-- ========== Action Mapping Definitions =============================== -->
<action-mappings>
<action
attribute="ownerForm"
input="/owner.jsp"
name="ownerForm"
path="/owner"
scope="request"
type="com.youcompany.struts.action.OwnerAction"
validate="false">
<forward name="success" path="/success.jsp" />
</action>

</action-mappings>

<!-- ========== Controller Configuration ================================ -->
<controller />

<!-- ========== Message Resources Definitions ============================ -->
<message-resources parameter="com.youcompany.struts.ApplicationResources" />

<!-- ========== Plug Ins Configuration ================================= -->
</struts-config>

but I failed.

HTTP Status 503 - Servlet action is currently unavailable
type Status report
message: Servlet action is currently unavailable
description The requested service (Servlet action is currently unavailable) is not currently available.
Apache Tomcat/4.1.18
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to connect a SQL Server database with struts
 
Similar Threads
problem between Form and Action
HTTP Status 500 -
mapping between JSP n form
help help help SOS
JasperException: Cannot find bean under name contractors