spring dao

Spring Dao - a Spring JDBC update example

Spring JDBC FAQ: Can you provide an example of a SQL UPDATE query used with Spring JDBC, i.e., Spring Dao class and methods?

Here's some example Java code from a Spring Dao class where I use Spring JDBC and the JDBCTemplate update method to run a SQL UPDATE statement. In short, if you can get access to the Spring JDBCTemplate, this Spring JDBC code shows a simple example of how to execute a SQL UPDATE statement.

Spring JDBC update method

Here's the source code for my example Spring JDBC update method:

Spring Dao - JDBC SELECT statement examples

Spring JDBC/Dao FAQ: Can you share some Spring JDBC examples, specifically SELECT query examples using Spring Dao objects?

Sure. I've done a lot of work with Spring lately, and I love the Spring Dao approach, so this page is a collection of Spring JDBC SELECT query examples (Spring DAO examples) from a real-world Java project I've been working on. In each example I try to show each type of SQL SELECT query that I've used in that project, with a brief explanation before each SQL query.

Spring Dao/JDBC tip - a great way to test your Spring DAO code

The Spring Framework gives you a great way to test your Spring JDBC (Spring DAO) code. Just use a special Spring JDBC class with a very long name, and you can test your Spring JDBC code against a real database.

Some people don't like this approach, but I think it's invaluable for (a) testing your SQL syntax when you first create it, and (b) making sure your SQL syntax is still valid after database changes occur.

Spring application context example - using Spring in a standalone Java application

Spring application context FAQ: Can you provide an example of using a Spring application context file in a standalone Java application?

Spring Dao - Spring JDBC DELETE examples

Spring JDBC DELETE FAQ: Can you provide some examples of using SQL DELETE queries using Spring JDBC (Spring Dao classes and methods)?

Here are a few Spring Dao examples, specifically JDBC/SQL DELETE examples.

Spring JDBC delete example #1: Delete a record by the primary key

In this first Spring JDBC DELETE example, I pass in an id parameter, which represents the primary key in my hosts database table. This JDBC/SQL statement deletes the record from that table that matches that id field.

A Spring JDBC SELECT and INSERT example

A Spring JDBC SELECT and INSERT example: Here's the source code for a complete Spring DAO class from a project that I'm currently working on (a Java-based web interface to the open source Nagios project) that shows how to use a few Spring JDBC methods, including both a SELECT example and a simple INSERT example.

I'll add more examples to this site later (and with more introduction/description), but for now I'm just going to drop this Java class sample out here and hope that it helps you "Learn Spring JDBC by example".

Spring Dao - a Spring JDBC DELETE example

Spring JDBC Example/Question: How do I perform a SQL DELETE statement using JDBC and the Spring Framework?

Here's a simple Java method that I created to perform a SQL DELETE using Spring JDBC techniques:

private void deleteById(String sql, int id)
{
  getSimpleJdbcTemplate().update(sql, id);
}

To use this method you just need to call it with the SQL DELETE statement you want to run, also passing in the id of the record you want to delete, like this:

Syndicate content