• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Getting null pointer exception when connecting to JDBC using spring

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is my Configuration xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.webdev.controller" />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="feedbackDAO" class="com.webdev.dao.impl.FeedBackDAOImpl">
<property name="datasource" ref="datasource"/>
</bean>

<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/WebDev"/>
<property name="username" value="root"/>
<property name="password" value="root"/>

</bean>
<mvc:resources mapping="/resources/**" location="/resources/"
cache-period="31556926"/>

   <mvc:annotation-driven />
</beans>


This is my DAO Impl Class


package com.webdev.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;

import com.webdev.Entities.Feedback;

public class FeedBackDAOImpl implements FeedbackDAO {
@Autowired
private DataSource datasource;

/*public DataSource getDatasource() {
return datasource;
}*/


public void setDatasource(DataSource datasource) {
this.datasource = datasource;
}

Connection connection = null;
PreparedStatement statement=null;

public void addFeedback(Feedback feedback) {
String sql = "insert into feedback(name,email,message)"+" values (?,?,?)";
try{
connection = datasource.getConnection();
statement = connection.prepareStatement(sql);
statement.setString(1, feedback.getName());
statement.setString(2, feedback.getEmail());
statement.setString(3, feedback.getMessage());
statement.executeQuery();
statement.close();
}catch(SQLException e){
e.printStackTrace();
}

}

}



This is  my UI page

<%@ 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>
<div id="Form">
<form action="addFeedback" method="post">
<label>Name</label> <input type="text" name="Name" required>
<label>Email</label> <input type="text" name="Email" required>
<label>Message</label> <input type="text" name="Message" required>
<input type="submit">

</form>
</div>
</body>
</html>
 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the error you get
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic