Spring framework hosting

Spring framework hosting Хостинг

Define book class

The class is quite generic and consists of getter/setter methods for private variables and a few constructors. Different costructors are used depending on what data has been provided in the CRUD form. Contents of Book.java follows:

package com.jvmhost;

public class Book {
 private int id;
 private String title;
 private String author;

 // constructors

 public Book() {}

 public Book(int id) {
 super();
 this.id = id;
 }

 public Book(String author, String title) {
 super();
 this.title = title;
 this.author = author;
 }

 public Book(int id, String author, String title) {
 super();
 this.id = id;
 this.title = title;
 this.author = author;
 }

// getter setter methods

 public int getId() { return id; }
 public void setId(int id) { this.id = id; }
 public String getTitle() { return title; }
 public void setTitle(String title) { this.title = title; }
 public String getAuthor() { return author; }
 public void setAuthor(String author) { this.author = author; }
}

Define dao (data access objects) for book class

In the example, read (search/select) operation is only coded for ID field but you can easily extend readBook method so that it recognizes if there are other attributes set and constructs proper query. Alternativley you can create dedicated methods for each attribute combination for example readBookByAuthorAndTitle and add code that determines what method to call depending on what search criteria have been provided in the form.

package com.jvmhost;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import java.util.List;
import java.util.ArrayList;

public class BookDao {
 private JdbcTemplate jdbcTemplate;

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

 public int createBook(Book b){
 String query="insert into book (author, title) values ('" b.getAuthor() "','" b.getTitle() "')";
 return jdbcTemplate.update(query);
 }

 public int updateBook(Book b){
 String query="update book set author='" b.getAuthor() "',title='" b.getTitle() "' where id=" b.getId() " ";
 return jdbcTemplate.update(query);
 }

 public int deleteBook(Book b){
 String query="delete from book where id=" b.getId() " ";
 return jdbcTemplate.update(query);
 }

 @SuppressWarnings("unchecked")
 public List<Book> readAllBooks(){
 String query = "SELECT * FROM book";
 List<Book> books = jdbcTemplate.query(query, new BeanPropertyRowMapper(Book.class));
 return books;
 }

 @SuppressWarnings("unchecked")
 public List<Book> readBook(Book b) {
 String query = "select * from book where ";
 List<Book> books = new ArrayList<Book>();
 if (b.getId() >0) {
  query = query   "id = ?";
  books = jdbcTemplate.query(query, new Object[] { b.getId() }, new BeanPropertyRowMapper(Book.class));
  } else {
   query = "SELECT * FROM book";
   books = jdbcTemplate.query(query, new BeanPropertyRowMapper(Book.class));
  }
 return books;
 }
}

Required spring 3, apache commons and h2 jars

For the example to compile you will need to download the following jars to WEB-INF/lib of your Java web application:

commons-logging-1.1.3.jar
h2-1.3.174.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-jdbc-3.2.4.RELEASE.jar
spring-tx-3.2.4.RELEASE.jar

As you can see in Spring 3 classes have been split to subsets in separate jars. For Spring 2 the jar set will be different.

Читайте также:  Скачать MCPEHosting - Create your own MCPE Server Мод APK (ограниченные деньги) для Android.

Spring and jdbctemplate

Aside from well-known classic JDBC usage scheme Spring includes JdbcTemplate class that facilitates database related tasks. Below we will show how to build simple web application (book library) that implements CRUD opeartions (Create, Read, Update, Delete) with the help of JdbcTemplate.

To make this project fully portable H2 embedded database will be used. It is easy to change it to HSQL, Derby or any standalone database and add connection pooling like C3P0 by only editing applicationContext.xml. Already built WAR file can also be downloaded and deployed on your application server. It works out of the box. Java code is contained in 3 files: Book.java, BookDao.java and index.jsp.

Spring beans in applicationcontex.xml

applicationContext.xml (located in WEB-INF/classes) stores beans used by the application. Beans reference each other except ds that is a base bean. The problem that exists when storing JDBC URL in .xml file or .properties file is that usually variables cannot be used there and so we need to have the URL hardcoded.

The index.jsp and crud form logic

Here we import needed classes, define the form and load beans from applicationContext.xml. Then we determine real path for our web application. As the bundled example database resides directly in web application’s db sudirectory we only append its name db/book when dynamically constructing JDBC URL.

Web hosting for spring and hibernate application

You may use a cloud provider for that. There are a lot. Amazon web services is the leader on the market but it’s moving fast (and you have a free micro instance for one year to try). Oracle just entered the ring for example. Rakspace is a big one too and there are a lot of others.

Читайте также:  Поиск VPS/VDS серверов |

Then you may decide between IAAS and PAAS. Basically with PAAS you don’t install tomcat or mysql yourself. There you have solutions like Red Hat Openshift, Vmware Cloudfoundry, Amazon beanstalkC cloudbees, Microsoft Azure.
With IAAS, you have a virtual machine. Maybe you can start by this to move up the stack and try PAAS later.

Cloud is the future of hosting. Renting a physical machine will disappear shortly. PAAS is believed to be the future of cloud. So you should try the cloud. There are tons of tutorials on this.

Отзывы о хостинге shhost.ru, обзор провайдера spring.hosting

Описание

Работаем официально, как российский ИП, возможно заключение договоров, оплата по безналу.

Кратко о виртуальном хостинге:
Находится в Страсбурге, Франция, датацентр OVH.
Cloudlinux 7, возможность выбора версии PHP и прочие особенности Cloudlinux. Защита от DDoS.
3 тарифа — от самого простого на 1 сайт/БД за 0.33$ (22 рубля), до максимального на 10 сайтов/БД за 1$ (65 рублей) и возможностью увеличить лимиты дополнениями.

VDS:
Виртуализация KVM.
Локации: Франция, Нидерланды, Латвия, Болгария, Украина, Канада, США.
Тарифы рассчитываются исходя из ОЗУ, от 512 МБ до 8 ГБ. Цены от 4$ (260 рублей).

Выделенные сервера:
Локации: Франция, Нидерланды, Латвия, Болгария, США, Украина. Цены от 25$ (1650 рублей).

Пользовались этим хостингом? Тогда нажмите « 1», где согласны, или напишите отзыв. Проголосовали 168 человек за последние пять лет.

Оцените статью
Хостинги