我想使用可滚动的结果集,所以当我使用两行代码时:
rs.setFetchDirection(ResultSet.TYPE_SCROLL_SENSITIVE);
rs.absolute(12);
Run Code Online (Sandbox Code Playgroud)
在我的DAOimpl中,我得到例外,plz帮助解决它们,谢谢提前.
import oracle.jdbc.OracleTypes;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Component;
@Component
public class MyDAOimpl extends JdbcDaoSupport implements
MyDAO {
public List<User> getList(final String where) throws Exception {
return (List) getJdbcTemplate().execute(
"{call PKG_USER.getUser(?,?)}",
new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs)
throws SQLException {
cs.setString(1, where);
cs.registerOutParameter(2, OracleTypes.CURSOR);
cs.execute();
ResultSet rs = (ResultSet) cs.getObject(6);
rs.setFetchDirection(ResultSet.TYPE_SCROLL_SENSITIVE);
rs.absolute(12);
List<User> list = new ArrayList<User>();
while (rs.next()) {
User user = new User(
rs.getString(1),
rs.getString(2),
rs.getString(3));
list.add(user);
}
return list;
}
});
}
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用这个类,但我似乎无法弄清楚如何限制返回的行.Plain JdbcTemplate具有最大行和最大提取大小设置器.有没有办法从NamedParameterJdbcTemplate获得类似的功能?
考虑具有主键的表作为columnd"id"
必须查询一系列id为[1,2,3,4 ......]
有2个选项 -
1)
Select * from Table where id = 1;
Select * from Table where id = 2;
Select * from Table where id = 3;
Select * from Table where id = 4;
Run Code Online (Sandbox Code Playgroud)
2)
Select * from Table where id in ( 1, 2, 3, 4 );
Run Code Online (Sandbox Code Playgroud)
对于Oracle而言,这两者中的哪一个在性能方面更好,并且使用Spring JDBC模板来实现持久性.假设数据集1,2,3,4将在java数据结构的限制范围内,请从应用程序的角度考虑内存约束.
它使用数据库连接池.
我有一个类似下面的SQL stmnt,它抱怨,因为我没有传递两个绑定变量.
有没有办法可以重用一个绑定变量,因为它们是相同的?
String sqlText = "SELECT * FROM person WHERE (name = UPPER(?) OR name = LOWER(?) )";
List<obj> results = tmplt.query(sqlText, new Object[]{name}, new objExtractor());
Run Code Online (Sandbox Code Playgroud) 这是我用来创建 DAO 对象的配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@XXX.XX.XX.XXX:XXX:XXX"/>
<property name="username" value="encryptedUsername"/>
<property name="password" value="decrytedPassword"/>
</bean>
<bean id="fiBillJDBCTemplate" class="com.tfl.fi.billing.dao.FIBillingDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
通常我们可以如下创建数据库对象
ApplicationContext context =
new ClassPathXmlApplicationContext("Parent.xml");
FIBillingDAOImpl dao =
(FIBillingDAOImpl)context.getBean("fiBillJDBCTemplate");
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为存储在 xml 文件中的密码是加密的。
如何制作 DAO 对象以解密密码。
我正在将我的代码从spring-3.1.0迁移到spring-4.2.4,但在使用spring-jdbc-4.2.4时,我无法找到ParameterizedRowMapper接口.能帮忙告诉我,哪个接口替换了jdbc-4.2.4 jar中的ParameterizedRowMapper?谢谢
我想RowCallbackHandler通过 Kotlin 流式传输由 Spring JDBC 捕获的结果对象Sequence。
代码基本上是这样的:
fun findManyObjects(): Sequence<Thing> = sequence {
val rowHandler = object : RowCallbackHandler {
override fun processRow(resultSet: ResultSet) {
val thing = // create from resultSet
yield(thing) // ERROR! No coroutine scope
}
}
jdbcTemplate.query("select * from ...", rowHandler)
}
Run Code Online (Sandbox Code Playgroud)
但我收到编译错误:
挂起函数只能在协程体内调用。
然而,这个“协程体”确实应该存在,因为整个块都被包装在一个sequence构建器中。但它似乎不适用于嵌套对象。
显示它不使用嵌套对象进行编译的最小示例:
// compiles
sequence {
yield(1)
}
// doesn't compile
sequence {
object {
fun doit() {
yield(1) // Suspension functions can be called only …Run Code Online (Sandbox Code Playgroud) 当我使用 IDE 启动 spring 服务器时,我有 2 个用于运行 sql db 的文件。我不断收到此错误
Failed to execute SQL script statement #6 of URL [file:/home***/resources/main/schema.sql]: ALTER TABLE Ingredient_Ref ADD FOREIGN KEY (ingredient) REFERENCES Ingredient(id)
问题出在这两行
ALTER TABLE Taco ADD FOREIGN KEY (taco_order) REFERENCES Taco_Order(id);
ALTER TABLE Ingredient_Ref ADD FOREIGN KEY (ingredient) REFERENCES Ingredient(id);
Run Code Online (Sandbox Code Playgroud)
一旦我评论他们,一切都会顺利......但无法弄清楚他们的问题我该如何解决这个问题?
这是整个文件
create table if not exists Taco_Order (
id identity not null,
delivery_Name varchar(50) not null,
delivery_Street varchar(50) not null,
delivery_City varchar(50) not null,
delivery_State varchar(2) not null,
delivery_Zip varchar(10) not …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring JDBC 3.0.6.我还有使用普通JDBC的遗留代码.遗留代码中有一些方法需要java.sql.Connection对象.我想从我的Spring代码中调用这个方法.如何传递java.sql.Connection对象?
如果我从数据源获取连接对象,那么我需要管理此连接的返回/释放.我是否可以只获取事务中的连接对象的引用.
我正在使用基于注释的配置和基于aop的声明式事务.
我正在努力应对以下异常:
org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [update EVALUATION_SHEET set STATUS=?, LAST_EDITED=? where id=?]; SQL state [99999]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type
Run Code Online (Sandbox Code Playgroud)
这是扔在这里:
jdbcTemplate.update("update E_SHEET set STATUS=?, LAST_EDITED=? where id=?",
new Object[]{eSheet.getStatus().ordinal(), eSheet.getLastEditDate(), eSheet.getId()},
new Object[]{OracleTypes.NUMBER, OracleTypes.TIMESTAMP, OracleTypes.NUMBER});
Run Code Online (Sandbox Code Playgroud)
数据库表创建如下:
create table E_SHEET (
ID number not null unique,
ID_POSITION number not null,
STATUS number default 0 not null,
ID_EXAMINER number not null,
LAST_EDITED timestamp not null);
Run Code Online (Sandbox Code Playgroud)
我不知道是什么导致了这个问题.这个方法:
eSheet.getLastEditDate()
Run Code Online (Sandbox Code Playgroud)
返回java.util.Date对象.我使用Spring JDBC模板和Spring DB以及Oracle DB …
我得到一个spring boot应用程序,并尝试创建一个由Java配置定义的DataSource bean:
@Bean(name = "dsBD") @Primary
public DataSource dsBD() {
return DataSourceBuilder.create()
.url("jdbc:mysql://localhost:3306/??database?autoReconne??ct=true")
.username("??root")
.password("mysql")
.driverClassName("com.mysql.jdbc.Drive??r")
.build();
}
@Bean(name = "jdbcBD") @Autowired
public JdbcTemplate jdbcBD(@Qualifier("dsBD") DataSource dsBD) {
return new JdbcTemplate(dsBD);
}
Run Code Online (Sandbox Code Playgroud)
但是在启动应用程序时,我收到以下错误:
Caused by: java.lang.IllegalStateException: No supported DataSource type found
at org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder.getType(DataSourceBuilder.java:138) ~[spring-boot-autoconfigure-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder.build(DataSourceBuilder.java:69) ~[spring-boot-autoconfigure-1.5.4.RELEASE.jar:1.5.4.RELEASE]
at com.package.DatabaseConfig.dsBD(DatabaseConfig.java:29) ~[classes/:na]
at com.package.DatabaseConfig$$EnhancerBySpringCGLIB$$ac56d75a.CGLIB$dsBD$0(<generated>) ~[classes/:na]
at com.package.DatabaseConfig$$EnhancerBySpringCGLIB$$ac56d75a$$FastClassBySpringCGLIB$$159befb3.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at com.package.DatabaseConfig$$EnhancerBySpringCGLIB$$ac56d75a.dsBD(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
at …Run Code Online (Sandbox Code Playgroud) 我有这个作为我的查询。
select a.cust_id,a.cust_name,b.cust_id,b.cust_name
from acustomer a,bcustomer b
Run Code Online (Sandbox Code Playgroud)
DaoImp 使用 Spring NamedJdbcparameterTemplate
方法:
NamedJdbcparameterTemplate temp= new NJPT(datasource);
List<Map<String,Object>> out=temp.quertForList(query,parametermap);
Run Code Online (Sandbox Code Playgroud)
但问题是,每当我得到这个查询在数据库工具的输出,我得到4列,但在程序的输出,我只得到2列,即cust_id和cust_name的是越来越被覆盖b,由于在地图相同的键名。
我该如何解决这个问题,请注意每次查询都会有所不同,因为我将此方法用作程序的通用方法,并且输出将是值列表,因此无法为输出映射任何模型类。
请注意,我希望这个函数是通用函数,这意味着每次查询都会发生变化,并且输出将是不同类型的。
我想使用LocalDateTime.parse()方法使用日期格式yyyy-MM-dd HH:mm:ss解析日期,但实际得到的是格式为yyyy-MM-ddTHH:mm:ss的日期.我不需要'T'.请参阅下面的代码
LocalDateTime.parse("2016-10-31 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Run Code Online (Sandbox Code Playgroud)
我得到的结果是2016-10-31T23:59:59.看到'T'.'T'导致问题,因此我无法将其持久保存到我的数据库中.我尝试将值保存在类型列中datetime; 我收到了错误 - org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar.
查看有效的值
VALUES('US','101','test','firstname','middleName','preferedN??ame','lastName',
'198??9-01-01','M',1,'1122??1123','test@test.com??','address1','addres??s2','Bloomingdale','??IL','US','689850',
1,??1,'11111','2016-12-3??1 23:59:59')
Run Code Online (Sandbox Code Playgroud)
(T最后一个值没有)
哪个不起作用:
VALUES('US','101','test','firstname','middleName','preferedN??ame','lastName',
'198??9-01-01','M',1,'1122??1123','test@test.com??','address1','addres??s2','Bloomingdale','??IL','US','689850',
1,??1,'11111','2016-12-3??1T23:59:59')
Run Code Online (Sandbox Code Playgroud)
(带有T最后一个值).
spring-jdbc ×13
java ×8
spring ×6
jdbc ×2
kotlin ×2
oracle ×2
spring-boot ×2
sql ×2
database ×1
datetime ×1
jar ×1
jdbctemplate ×1
oracle12c ×1
resultset ×1
spring-batch ×1