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 .:: | ||
Comments
21 Responses to “Spring JDBC Hello World Example, Create Table In Database”
For “Spring JDBC Hello World Example” can u tell me the jars required.
On clicking “Download Now” button, it gives Page Not Found.
@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.
@sumit
Download links updated, can now check from your end.
thank you.
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 .
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
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.
I never seen this kind of tutorial before……really helpful to evryone…wishing u all the best to java4s team…
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”.
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
Hi,
This tutorial is good for the beginners, Can you explain Spring MVC with some more examples…
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
can u explain flow of this program i am not getting where it starts?
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..!!
Do u know which Jara files needed to connect oracle
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..!!
can u explain the flow of program
Can you please explain the execution flow of the program
Hello can u plz give me jar files required for this example?
Another question I have is….Can I do spring connectivity with msaccess?
In createTable method jt.execute(); shows an error 'the method execute (string) from type jdbc template refers to the missing type data access exception
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
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.