How to write an java application that takes a sql query and a number as input and fire it

121 views Asked by At

How to write an java application that takes a sql query and a number as input and fire it those many times as number establishing a database connection using jndi lookup.

2

There are 2 answers

0
Würgspaß On

Depends very much on the DB you are using. But here is an example for Oracle. In general, you will need a Connection, a Statement and a ResultSet

0
Shahid On

This the answer/piece of code I was looking for:

public void runUserQuery(final String userString, final int userInput) throws IllegalArgumentException, NamingException { System.out.println("Executing " + userString + " "+ userInput ); class NewDao extends JdbcDaoSupport {

        public NewDao() throws IllegalArgumentException, NamingException {
            JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
            bean.setJndiName("<JNDI Name>");
            bean.afterPropertiesSet();
            DataSource dataSource = (DataSource) bean.getObject();
            setDataSource(dataSource);


        }

        public void executeQueryMultiple() {
            int index = userInput;
            while (index > 0) {
                this.getJdbcTemplate().execute(userString);
                index--;
            }
        }

    }
    ;
    NewDao dao = new NewDao();

    dao.executeQueryMultiple();

}

Unfortunately I could not make you guys understand and got negative vote. :D