spring-jdbc

Spring Jdbc query execution

两盒软妹~` 提交于 2020-01-06 03:18:22
问题 Does anyone know how what Spring Jdbc template method I could use to execute this 'upsert' or an alternative approach that would also perform the operations in one database call? UPDATE jasper_report SET Uri = 'update' WHERE ReportId = 99; IF @@ROWCOUNT = 0 AND Exists(Select 1 FROM report Where Id = 99) BEGIN INSERT INTO jasper_report (ReportId, Uri) VALUES (99, 'insert') END; 回答1: Turns out I was close but forget a step. I had to change the query itself to: BEGIN UPDATE jasper_report SET Uri

Passing the JNDI name dynamically

。_饼干妹妹 提交于 2020-01-05 09:51:06
问题 I have a lot of Websphere servers with different JNDI name of my DB connection so I have to build many ears files for each server. I'd like to do something like this: <bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${SPECIFIC_JNDI_NAME}"/> </bean> How can I create this SPECIFIC_JNDI_NAME variable? 回答1: The proper Java EE way to do it, is using resource references in your code like: java:comp/env/jdbc/myDSRef , then this resource

Spring jdbcTemplate Junit

天涯浪子 提交于 2020-01-05 04:07:11
问题 I have one application with DAO as follows. I want to junit this class. My Class looks like this. public class DaoImpl implements Dao{ @Override public User getUserInfo(String userid) { return getTemplate().queryForObject(QUERY, new Object[] { userid }, new BeanPropertyRowMapper<User>(User.class)); } } My junit class looks like this @RunWith(SpringJUnit4ClassRunner.class) public class DaoImplTests{ @Autowired private Dao dao; @Mock JdbcTemplate jdbcTemplate; @Test public void testUsingMockito

Aspect for Spring JDBC

梦想的初衷 提交于 2020-01-04 04:40:57
问题 Is that possible to define Spring AOP aspect to Spring JDBC ? To be specific, I am trying to setup a logger for NamedParameterJdbcTemplate to log SQL queries. Below is my XML configuration. <aop:config> <aop:aspect id="aspect4" ref="sqlLoggingInterceptor"> <aop:pointcut expression="execution(* org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate..*(..))" id="pointcut3" /> <aop:around pointcut-ref="pointcut3" method="profile" /> </aop:aspect> </aop:config> And below is my

Getting error code 4220 with null SQL State

为君一笑 提交于 2020-01-03 18:52:18
问题 I'm getting error code -4220 with null SQL State .the SP "XXXXX" contain VARGRAPHICS data type. Below is complete exception stack trace. SP is executing properly, the error is coming while reading output parameter i.e. execute method called successfully but getString method is giving error. Even if I'm trying to access any method to get output parameter value, error is coming. Just FYI, I tried with latest version of db2 driver but still error persist. com.ibm.db2.jcc.am.SqlException: [jcc]

spring ArrayIndexOutOfBoundsException with SimpleJdbcCall

我的未来我决定 提交于 2020-01-03 02:26:15
问题 This is the Oracle procedure I'm trying to call: PROCEDURE GetCoreReportExtras ( pnAssignment IN NUMBER, pnUserRole in NUMBER, psAreaMenu in VARCHAR2, pnAreaLevel in NUMBER, curReportList OUT outcur, psLDO in VARCHAR2 default 'none', pnAcisNumber in NUMBER default 0); Here's my Java code: private class GetStandardReportExtrasSPV2{ int nAreaLevel; int nAssignment; int nUserRole; int nAcisNum = 0; String strAreaMenu; String strLDO = null; private SimpleJdbcTemplate simpleJdbcTemplate; private

Can we fetch the results using one RowMapper object instead of creating objects everytime?

被刻印的时光 ゝ 提交于 2020-01-01 11:57:10
问题 When fetching results from db through springJdbcTemplate, everywhere I have seen that they are passing the new object of RowMapper everytime. Is this required? Or can we just use one object and pass it again and again. Student student = jdbcTemplateObject.queryForObject(SQL, new Object[] { id }, new StudentRowMapper()); I know this object will be garbage collected later on, but I didn't wanted to create the same object over and over again. Can I use Student student = jdbcTemplateObject

What is proper way to use PreparedStatementCreator of Spring JDBC?

为君一笑 提交于 2020-01-01 04:40:31
问题 As per my understanding the use of PreparedStatement in Java is we can use it multiple times. But I have some confusion using PreparedStatementCreator of Spring JDBC. For example consider following code, public class SpringTest { JdbcTemplate jdbcTemplate; PreparedStatementCreator preparedStatementCreator; ResultSetExtractor<String> resultSetExtractor; public SpringTest() throws SQLException { jdbcTemplate = new JdbcTemplate(OracleUtil.getDataSource()); preparedStatementCreator = new

Overriding BeanPropertyRowMapper to Support JodaTime DateTime

ε祈祈猫儿з 提交于 2019-12-31 21:35:13
问题 My Domain object has couple of Joda-Time DateTime fields. When I'm reading database values using SimpleJdbcTemplate: Patient patient = jdbc.queryForObject(sql, new BeanPropertyRowMapper(Patient.class), patientId); It just fails and surprisingly, no errors were logged. I guess it's because of the timestamp parsing to DateTime is not working with Jdbc. If it's possible to inherit and override BeanPropertyRowMapper and instruct to convert all java.sql.Timestamp and java.sql.Date to DateTime , it

Why this statement rs=st.executeQuery(query); is not excuting? How can I select only a table depend on input type=radio from mysql from two tables?

五迷三道 提交于 2019-12-31 06:42:26
问题 Why is this query rs=st.executeQuery(query) ; not executed to select a table from database? String gender = request.getParameter("gender"); if (gender != null) { String table = gender.equals("teacher") ? "teacher2" : "student"; String query ="select username,password from " +table+ " where username='"+name+"' AND password='"+abc+"'"; rs=st.executeQuery(query); //Why This statement having error } Mysql Query for this table enter image description herei think this query is wrong "select