GithubHelp home page GithubHelp logo

tusharbihani / cloudsole-database Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 5.09 MB

Spring MVC, JPA, Jdbc, Hibernate, Spring Data, Redis, Mongo, Postgres, Oracle, REST

Home Page: http://cloudsole-database.herokuapp.com/

cloudsole-database's Introduction

CloudSole Database

Spring Data
Spring MVC
JPA
Hibernate
Redis
Oracle
REST
Quartz

#Annotation Context

<context:annotation-config />
<context:component-scan base-package="com.example.hibernate" />

<!--  <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/servicecore"/>
	<property name="username" value="root"/>
	<property name="password" value=""/>
</bean>-->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver" />
	<property name="url" value="jdbc:mysql://localhost:3306/servicecore"/>
	<property name="username" value="root"/>
	<property name="password" value=""/>
	<property name="initialSize" value="2"/>
	<property name="maxActive" value="5"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource"/>
	<property name="packagesToScan" value="com.example.model"/>
	<property name="hibernateProperties">
		<props>
			<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
		</props>
	</property>

</bean>
<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

 <context:annotation-config />
	<context:component-scan base-package="com.example.jdbc" />

	<!--  <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/servicecore"/>
		<property name="username" value="root"/>
		<property name="password" value=""/>
	</bean>-->

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/servicecore"/>
		<property name="username" value="root"/>
		<property name="password" value=""/>
		<property name="initialSize" value="2"/>
		<property name="maxActive" value="5"/>
	</bean>

</beans>

#Jdbc package com.example.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Component;

import com.example.model.Customer;

@Component(value="JdbcDaoImpl")
public class JdbcDoaImpl {

	private DataSource dataSource;
	private JdbcTemplate jdbcTemplate;
	private NamedParameterJdbcTemplate namedparmjdbtemp;
	
	public static Connection setupJdbcConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
		Connection connection = null;
	
		String driver = "com.mysql.jdbc.Driver";
		Class.forName(driver).newInstance();
		connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/servicecore","root", "");
		return connection;
	}

	public Customer getCustomer(int customerId) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
		Connection connection = dataSource.getConnection();
		java.sql.PreparedStatement ps = connection.prepareStatement("Select * from customer where id=?");
		ps.setInt(1, customerId);
	
		Customer customer = null;
		ResultSet rs = ps.executeQuery();
		if (rs.next()){
			customer = new Customer(customerId, rs.getString("first_name"), rs.getString("last_name"));
		}
		rs.close();
		ps.close();
		connection.close();
		return customer;
	}

	public int getCustomerCount(){
		//jdbcTemplate.setDataSource(getDataSource());
		return jdbcTemplate.queryForInt("Select count(*) from customer");
	}

	public String getCustomerFName(long id){
		String sql = "Select first_name from customer where id = ?";
		return jdbcTemplate.queryForObject(sql, new Object[]{id},String.class);
	}

	public Customer getCustomerForId(long id){
		String sql = "Select * from customer where id = ?";
		return jdbcTemplate.queryForObject(sql, new Object[]{id}, new CustomerMapper());
	}

	public List<Customer> getListCustomers(){
		String sql = "Select id, first_name, last_name from customer";
		return jdbcTemplate.query(sql, new CustomerMapper());
	}

	public void insertCustomer(Customer customer){
		String sql = "Insert into customer(first_name, last_name) values(?,?)";
		jdbcTemplate.update(sql, new Object[]{customer.getFirstName(), customer.getLastName()});
	}

	public void insertCustomerNP(Customer customer){
		String sql = "Insert into customer(first_name, last_name) values(:fname,:lname)";
		SqlParameterSource namedParm = new MapSqlParameterSource("fname", customer.getFirstName()).addValue("lname", customer.getLastName());
		namedparmjdbtemp.update(sql, namedParm);
	
		//jdbcTemplate.update(sql, new Object[]{customer.getFirstName(), customer.getLastName()});
	}

	public void createLeadTable(){
		String sql = "Create table lead (id integer, name varchar(255))";
		jdbcTemplate.execute(sql);
	}

	public static final class CustomerMapper implements RowMapper<Customer>{
		@Override
		public Customer mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			Customer customer = new Customer();
			customer.setId(resultSet.getLong("id"));
			customer.setFirstName(resultSet.getString("first_name"));
			customer.setLastName(resultSet.getString("last_name"));
			return customer;
		}
	}

	public List<Customer> getAllCustomers(){
		return null;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
		this.namedparmjdbtemp = new NamedParameterJdbcTemplate(dataSource);
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
}

#Main

package com.example.jdbc;

import java.sql.SQLException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.example.model.Customer;

public class MainConfig {

public static void main(String[] args) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
	/*AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    applicationContext.getEnvironment().setActiveProfiles("default");
    applicationContext.scan(JdbcDoaImpl.class.getPackage().getName());
    applicationContext.refresh();*/
	
	ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/jdbcContext.xml");
	JdbcDoaImpl jdbcdao = ctx.getBean("JdbcDaoImpl", JdbcDoaImpl.class);
	
   // JdbcDoaImpl jdbcdao = applicationContext.getBean(JdbcDoaImpl.class);
	//Customer customer = jdbcdao.getCustomer(14);
	//System.out.println(customer.getFirstName());
	//System.out.println(jdbcdao.getCustomerCount());
	//System.out.println(jdbcdao.getCustomerFName(14));
	//System.out.println(jdbcdao.getCustomerForId(14));
	//System.out.println(jdbcdao.getAllCustomers().size());
	
	jdbcdao.insertCustomerNP(new Customer("sd", "asd"));
	//jdbcdao.createLeadTable();
	
}
}

cloudsole-database's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.