Link to home
Start Free TrialLog in
Avatar of TUS11
TUS11

asked on

Problem while accessing database via jdbctemplate in spring framework

We are facing problem When we try to execute any Database Command, NULL is returned in Exception.
Issue with the way calling DAO method in Servlet class.

We are using Spring 3.0 and oracle. Please check the attached file as we have pasted all the code as per the structure.

Thanks
Rohan.
JAVA-Problem.docx
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please check the attached file as we have pasted all the code as per the structure.
You're not doing yourself any favours there. Not only are you making it less likely that people will help in the first place, but some won't have software installed that can open that format
You'd be better off posting the log (.txt file)
Avatar of TUS11
TUS11

ASKER

Hi

i have attached the file in .txt and .doc which can open with older versions of word and .docx is already uploaded with original question.

any immediate help will be appreciated.
java-problem.txt
JAVA-Problem.doc
There's no log file there
Avatar of TUS11

ASKER

please find the error log attached.

thanks
Error.doc
Your getStudentCount method wouldn't even compile, so i don't know what code you're running.

	   	String str = "";	
		      String SQL = "Select COUNT(*) from ERP_PRODUCT;";
		      int student = this.jdbcTemplate.queryForInt(SQL);
		     str = student; // <<<< wouldn't even compile

Open in new window

Whatever code you are running isn't populating the student count
Avatar of TUS11

ASKER

hmm,

You are right, actually it's str += student;
I didn't change that in the files attached.
Now Plz. tell me is it possible to inject bean into servlet.

As while I tried to debug & searched on net, found that the setDataSource() method runs file while compiling, but I couldn't find the correct way to inject the bean into Servlet.

When I make object of DAO class that actually creaates new instance & the values set by bean were lost.

Thanks.
Now Plz. tell me is it possible to inject bean into servlet.
I'm not a Spring expert i'm afraid, but isn't that the whole point? So the answer is probably yes.

You are right, actually it's str += student;
I didn't change that in the files attached.
Please post the exact code you're running - it just wastes time otherwise. (And not as an attachment)
Avatar of TUS11

ASKER

Problem : When we try to execute any Database Command, NULL is returned in Exception.
Issue with the way calling DAO method in Servlet class.
Following are the files with structure:

Files on Loaction ( WebContent\WEB-INF) :

1)      Bean.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	
	
	
		
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
		<property name="url" value="jdbc:oracle:thin:@ hostName:1521:xe"/>
		<property name="username" value="hostName"/>
		<property name="password" value="Password"/>
	</bean>
	
	<!-- DAOS -->
	<bean id="productDAO" class="br.edeploy.voive.dao.ProductDAO">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	
</beans>

Open in new window


2)      web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>VoiceIntegrationWS</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/beans.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
    <servlet>
    <servlet-name>ProdutosIn</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ProdutosIn</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
</web-app>

Open in new window


3)      ProdutosIn-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


  	 <bean name="/NewFile.htm" class="br.edeploy.voice.service.ProdutosIn"/> 

</beans>

Open in new window


Files on Loaction ( WebContent) :

1)      NewFile.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


<form method="GET" action='ProdutosIn' name="delete">
This is a demo for XML writing.<br /><br />

<input type="submit" value="Add Temp Record in Database">
<a href="NewFile2.htm">Go to New File</a>
</form>

</body>
</html>

Open in new window


Files on Loaction (\src\ service) :

1) ProdutosIn.java (This is used as Servlet)

package br.edeploy.voice.service;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.servlet.ModelAndView;
import br.edeploy.voice.bean.Product;
import br.edeploy.voive.dao.ProductDAO;

@WebServlet("/ProdutosIn")
public class ProdutosIn extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    public ProdutosIn() {
        // TODO Auto-generated constructor stub
    }
    public void init() throws ServletException {
	    
	  }
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		

		String msg = "";
		msg += "1 ";
				
				
		try
		{
			 
		ProductDAO objP=new ProductDAO();
		//ProductDAO objP = (ProductDAO) context.getBean("ProductDAO");
	    msg += "2 ";
		
	    // Inject the datasource into the dao
		//objP.SetDatasource(dataSource);
	    		
	    String i = objP. getStudentCount ();
	    msg += "  Total-Products : "+i;
		}
		catch(Exception ex)
		{
			msg += "Error : "+ex.getMessage();
		}
	    
	    
        
	    response.setContentType("text/plain");
	    PrintWriter out = response.getWriter();
	    out.println("The Record Status : "+msg);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}

Open in new window


Files on Loaction (\src\ dao) :

1)      ProductDAO.java (DAO class)

package br.edeploy.voive.dao;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
public class ProductDAO {
	@Autowired
	private DataSource dataSource;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	   
	public ProductDAO() {
		
	}

	   public void setDataSource(DataSource dataSource) {
	      this.dataSource = dataSource;
	      this.jdbcTemplate = new JdbcTemplate(dataSource);
	      int i = this.jdbcTemplate.queryForInt("select count(*) from ERP_Product");
	      System.out.print("Count is "+i);
	   }

	
	   public String getStudentCount() 
	   {
	   	String str = “”;	
		      String SQL = "Select COUNT(*) from ERP_PRODUCT;";
		      int student = this.jdbcTemplate.queryForInt(SQL);
		     str += student;
	      return str;
	   }

	
	
}

Open in new window

Now you've posted your code visibly, someone who knows Spring can at least see. It's possibly something to do with the way the datasource is wired - the dao is getting created but not populated
Your code is all mixed up between Spring and non-Spring stuff. You should read some of the "Spring 3 mvc hello world examples" (Google search) for the correct general structure. This will also give you a working start point from where you can make small changes at a time to move toward what you want. I realise that these don't deal with DAO's and datasources, etc but they are things that you would add once you have a simple page working.
Avatar of TUS11

ASKER

Hi

we have developed hello world and infact the forms and registration pages done. we are facing problem when we are interacting with oracle database via spring using jdbctempate.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial