Announcement Announcement Module
Collapse
No announcement yet.
How to select schema with JdbcTemplate? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to select schema with JdbcTemplate?

    Hi

    I have created an EmployeeDAO to read from employee table from postgreSQL. This table is in schema abc. This table can be present in different schemas and i want t o set it as a property in DAO. Is it possible to pass the schema dynamically? One way that i know for doing it is to use JdbcInsert but i cant get that from JdbcDaoSupport. Is there any better way of doing this?

    Code:
    package pkg.spring3;
    
    
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    
    public class EmployeeDAO extends JdbcDaoSupport{
    	public void createEmployee(Employee e){
    		getJdbcTemplate().update("insert into abc.employee(emp_id, name, manager_id) values(?,?,?)", e.getEmployeeId(), e.getName(), e.getManagerId());
    	}
    }

  • #2
    Hello amitkhanna

    This table can be present in different schemas and i want t o set it as a property in DAO.
    Can you expand your idea? is confuse

    but i cant get that from JdbcDaoSupport.
    Why?, Do you receive some error?
    If yes, copy here the complete error stack trace

    Code:
    getJdbcTemplate().update("insert into abc.employee(emp_id, name, manager_id) values(?,?,?)", e.getEmployeeId(), e.getName(), e.getManagerId());
    your Query is simple, I mean, is not very specific to some SQL provider

    You could consider Hibernate/JPA too

    Comment


    • #3
      Thanks for your reply. Actually, my idea is to create table named employee (and other related tables) in different schema. There will be a separate PostgreSQL schema for each company each having same tables. This design separates data for different companies. So if i maintain data for company say abc and xyz, i will have two tables abc.employee and xyz.employee.

      JdbcInserter has a method withSchema to set schema for insertion, but JdbcTemplate doesn't. So what i am interested in knowing is that, is there any why i can set the schema name with JdbcTemplate. I don't want to build my query string at runtime by prefixing the selected schema name to the table name.

      What could be a better way of doing this?

      Thanks
      Amit Khanna

      Comment

      Working...
      X