Newsletter

Spring JDBC Hello World Example, Create Table In Database

Spring » on Feb 11, 2012 { 21 Comments } By Sivateja

Let us see how to create a table in database using spring JDBC.

Files Required

  • SpringJdbcCreateTable.java
  • OurLogic.java
  • spconfig.xml

Directory Structure

SpringJdbcCreateTable.java

package java4s;

import org.springframework.jdbc.core.JdbcTemplate;

public class SpringJdbcCreateTable
{
	JdbcTemplate jt;

	public void setJt(JdbcTemplate jt)
	{
		this.jt = jt;
	}

	public void createTable()
	{
		jt.execute("create table sptest(sno number(3), sname varchar(10))");
		// execute() returns void
		System.out.println("table created");
	}
}

OurLogic.java

package java4s;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class OurLogic
{
	public static void main(String args[])
	{
		Resource res = new ClassPathResource("spconfig.xml");
		BeanFactory factory = new XmlBeanFactory(res);
		SpringJdbcCreateTable st =(SpringJdbcCreateTable)factory.getBean("id3");
		st.createTable();
	}
}

spconfig.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

<bean id="id1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
  <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
  <property name="username" value="system"/>
  <property name="password" value="admin"/>
</bean>

<bean id="id2" class="org.springframework.jdbc.core.JdbcTemplate">
  <constructor-arg>
      <ref bean="id1"/>
  </constructor-arg>
</bean>

<bean id="id3" class="java4s.SpringJdbcCreateTable">
  <property name="jt">
     <ref bean="id2"/>
  </property>
</bean>

</beans>

Output:

table created

Note:  See in SpringJdbcCreateTable.java  i have used execute() method which will returns void, in fact we can use update() too but this will returns some int value.  So better execute() for creating these type of jobs.

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

21 Responses to “Spring JDBC Hello World Example, Create Table In Database”
  1. sumit says:

    For “Spring JDBC Hello World Example” can u tell me the jars required.
    On clicking “Download Now” button, it gives Page Not Found.

  2. Java4s says:

    @sumit

    Oops.. seems its the broken link, sorry sumit i will update and let you know soon. ( I will send the update to your mail id provided here )

    Thank you so much for telling this issue.

  3. Java4s says:

    @sumit

    Download links updated, can now check from your end.

    thank you.

  4. Saket says:

    Hi
    Thanks for the wonderful effort by the Java 4s team for presenting these technologies in a simpler way.
    But Please explain Spring MVC with examples .

  5. sorabh says:

    Hi,

    Great tutorial.I appreciate your work.While implementing,I have come across a mistake in your code
    jt.execute(“create table sptest(sno number(3), sname varchar(10))”);
    I modified this by
    jt.execute(“create table sptest(sno int(3), sname varchar(10))”);
    and it works fine.

    Good luck
    Sorabh

  6. chaitanya says:

    Your tutorials are very clear to understand and awesome. Any one understood very easily. Thank you very much. Please provide tutorials for spring-MVC and Spring-ORM please. Thank you very much sir.

  7. chandu says:

    I never seen this kind of tutorial before……really helpful to evryone…wishing u all the best to java4s team…

  8. nidhi says:

    can u please mention jar files required to run this programme..
    there is an error “JdbcTemplate can not resolve to a type” on “SpringJdbcCreateTable.java”.

    • Vinay says:

      This many Jar files will be needed to run the above given program

      spring-aop-4.3.10.RELEASE
      spring-aspects-4.3.10.RELEASE
      spring-beans-4.3.10.RELEASE
      spring-context-4.3.10.RELEASE
      spring-core-4.3.10.RELEASE
      spring-expression-4.3.10.RELEASE
      spring-jdbc-4.3.8.RELEASE
      spring-tx-4.3.8.RELEASE
      spring-web-4.3.10.RELEASE
      spring-webmvc-4.3.10.RELEASE-sources
      mysql-connector-java-5.1.6

  9. Hemanth says:

    Hi,
    This tutorial is good for the beginners, Can you explain Spring MVC with some more examples…

  10. aravind says:

    Hi..
    we have JdbcTemplate in spring jdbc module so why HibernateTemplate has come
    reply with brief explain.

    what is main difference between JdbcTemplate and HibernateTemplate?

    Thanks

  11. vijay says:

    can u explain flow of this program i am not getting where it starts?

  12. Pavan says:

    Hi Java4s,
    Can you please tell which are the jars we have to use??

    I have been through your all topics, all are good..

    Needed brief explanation too for all your topics..!!

  13. Tamilselvan says:

    Hi Java4s,

    I want to know how to connect mysql i trying to connect it throws error. so,please provide the code to connect mysql..!!

  14. Tapas says:

    can u explain the flow of program

  15. PullaRao says:

    Can you please explain the execution flow of the program

  16. Jeski says:

    Hello can u plz give me jar files required for this example?
    Another question I have is….Can I do spring connectivity with msaccess?

  17. Sunil says:

    In createTable method jt.execute(); shows an error 'the method execute (string) from type jdbc template refers to the missing type data access exception

  18. Jeski says:

    When I run this app…there is error like
    Could not load jdbc driver…
    I connect my program to Oracle 10g
    How to solve this problem.
    Plz help

  19. Aniket Chavan says:

    Sir can you please tell me in real-time we are using HQL or Native Or Named Query, and in Spring JDBC we are using SimpleJdbcInsert or NamedParameterJdbcTemplate or JdbcTemplate
    in a web-based application.

    Thanks in advance.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.