本文将分享在过去2天检索到的Java1.7.0下运行的SQL-Server的详细内容,并且还将对MSSQL-JDBC3.0中的日期列进行详尽解释,此外,我们还将为大家带来关于ApacheKafka中的
本文将分享在过去2天检索到的Java 1.7.0下运行的SQL-Server的详细内容,并且还将对MSSQL-JDBC 3.0中的日期列进行详尽解释,此外,我们还将为大家带来关于Apache Kafka 中的 IDENTITY_INSERT 问题 - JDBC - MSSQL、com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别、Could not load JDBC driver class [com.mysql.jdbc.Driver]、Could not resolve placeholder ''jdbc.driverClassName'' in string value "${jdbc.driverClassName} 错误的相关知识,希望对你有所帮助。
本文目录一览:- 在过去2天检索到的Java 1.7.0下运行的SQL-Server(MSSQL-JDBC 3.0)中的日期列
- Apache Kafka 中的 IDENTITY_INSERT 问题 - JDBC - MSSQL
- com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别
- Could not load JDBC driver class [com.mysql.jdbc.Driver]
- Could not resolve placeholder ''jdbc.driverClassName'' in string value "${jdbc.driverClassName} 错误
在过去2天检索到的Java 1.7.0下运行的SQL-Server(MSSQL-JDBC 3.0)中的日期列
在正式的Oracle JDK 1.7.0下运行时,使用Microsoft JDBC-Driver
3.0版从SQLServer2008检索DATE类型的列时,会产生奇怪的效果。主机操作系统是Windows
Server 2003。
所有日期栏,为检索 两个 天过去相对于实际存储在列中的值。
我准备了一个最小的代码示例,将其测试出来(测试表和数据):
CREATE TABLE Java7DateTest (
dateColumn DATE
);
INSERT INTO Java7DateTest VALUES('2011-10-10');
码:
public class Java7SQLDateTest {
public static void main(final String[] argv) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(
"jdbc:sqlserver://192.168.0.1:1433;databaseName=dbNameHere","user","password");
PreparedStatement statement = connection.prepareStatement("SELECT * FROM Java7DateTest");
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
final java.sql.Date date = resultSet.getDate("dateColumn");
final String str = resultSet.getString("dateColumn");
System.out.println(date + " (raw: " + str + ")");
}
resultSet.close();
statement.close();
connection.close();
} catch (final Throwable t) {
throw new RuntimeException(t.getMessage(),t);
}
}
}
在上述配置上运行此代码将显示:“ 2011-10-08(原始:2011-10-08)”。在JRE 1.6.0_27下打印:“
2011-10-10(原始:2011-10-10)”
我找不到与我的Google问题有关的任何内容,因此我假设它要么是我忽略的愚蠢行为,要么是没人在使用Java7。
有人可以确认这个问题吗?如果我仍然想使用Java7,还有哪些选择?
编辑:即使使用-Xint运行,也会出现问题,因此它不是由Hotspot错误引起的。
Edit2:旧的驱动程序(Microsoft
1.28)可以与JDK1.7.0一起正常工作(我认为直到两年前我们才使用该驱动程序)。jTDS在该示例中也可以很好地工作。我正在考虑切换到jTDS,但我不愿意这样做,因为我没有最模糊的想法,这可能会对我们的生产环境造成什么影响。理想情况下,它应该可以正常工作,但是那也是我将开发箱切换到Java7时所相信的。生产环境中有一个漂亮的胖数据库,它太大了,无法创建一个副本进行测试(或者我们的服务器只剩下很少的磁盘)。因此,为该应用程序设置测试环境并非难事,我将不得不为此缩减一个缩小的数据库。
Edit3:jTDS附带了自己的一组捕获。我发现行为上的差异破坏了我们的应用程序之一。ResultSet.getObject()根据驱动程序(Short与Integer)为SmallInt列返回不同的对象类型。另外,jTDS不实现JDBC4
Connection接口,不支持Connect.isValid()。
Edit4:我上周注意到,在我更新到JDK1.6.0_29之后,MSSQL-JDBC
3.0拒绝连接到任何数据库。现在是jTDS了……我们昨天切换了生产服务器(我固定了两个应用程序依赖于驱动程序特性的地方),到目前为止,我们还没有遇到任何问题。
Apache Kafka 中的 IDENTITY_INSERT 问题 - JDBC - MSSQL
如何解决Apache Kafka 中的 IDENTITY_INSERT 问题 - JDBC - MSSQL
使用 Apache Kafka 接收器连接器将数据从 sql Server 插入/更新到 sql Server 得到以下错误
java.sql.BatchUpdateException:无法为表“table_name”中的标识列插入显式值 当 IDENTITY_INSERT 设置为 OFF 时。 在 com.microsoft.sqlserver.jdbc.sqlServerPreparedStatement.executeBatch(sqlServerPreparedStatement.java:2075)
源配置
name=jdbc-mssql-prod-5
connector.class=io.confluent.connect.jdbc.JdbcSourceConnector
connection.url=jdbc:sqlserver:
connection.user=
connection.password=
topic.prefix= source_topic.
mode=timestamp
table.whitelist=A,B,C
timestamp.column.name=ModifiedDateTime
connection.backoff.ms=60000
connection.attempts=300
validate.non.null= false
# enter timestamp in milliseconds
timestamp.initial= -1
接收器配置
name=MysqL-sink-prod-5
connector.class=io.confluent.connect.jdbc.JdbcSinkConnector
tasks.max=1
topics= sink_topic_a,sink_topic_b
connection.url=jdbc:sqlserver:
connection.user=
connection.password=
insert.mode=upsert
delete.enabled=true
pk.mode=record_key
errors.log.enable= true
errors.log.include.messages=true
在该表中,主键列和标识列相同。
com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别
com.mysql.jdbc.Driver
是 mysql-connector-java 5中的,
com.mysql.cj.jdbc.Driver
是 mysql-connector-java 6中的
Could not load JDBC driver class [com.mysql.jdbc.Driver]
我使用 Spring JDBC 编程时,遇到一个错误消息:Could not load JDBC driver class [com.mysql.jdbc.Driver]
完整的错误为:
Jul 27, 2020 12:22:26 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b80350b: startup date [Mon Jul 27 12:22:26 CST 2020]; root of context hierarchy
Jul 27, 2020 12:22:26 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Jul 27, 2020 12:22:26 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''dataSource'' defined in class path resource [Beans.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property ''driverClassName'' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''dataSource'' defined in class path resource [Beans.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property ''driverClassName'' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1650)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1357)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:758)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.sap.MainApp.main(MainApp.java:10)
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property ''driverClassName'' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:123)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1646)
... 13 more
解决方法:
下载 MySQL Connector/J (the official JDBC driver for MySQL).
https://dev.mysql.com/downloads/connector/j/
将下载的 mysql-connector-java.jar 添加到 Java build path 后问题解决:
使用 mySQL client 命令行可以看到之前用 Java JDBC 插入的数据库条目:
要获取更多 Jerry 的原创文章,请关注公众号 "汪子熙":
本文分享 CSDN - 汪子熙。
如有侵权,请联系 support@oschina.cn 删除。
本文参与 “OSC 源创计划”,欢迎正在阅读的你也加入,一起分享。
Could not resolve placeholder ''jdbc.driverClassName'' in string value "${jdbc.driverClassName} 错误
今天在 MyEclipse 搭建 spring 框架,通过自动生成 spring 框架,布好 DAO 层还有 Service 层后,还导入了单元测试,没想到在测试时出现了
Could not resolve placeholder ''jdbc.driverClassName'' in string value "${jdbc.driverClassName} 错误
具体代码如下:
test 类
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import Service.AccountService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Resource(name="accountService")
private AccountService accountService;
@Test
public void demo1(){
accountService.transfer("aaa", "bbb", 200d);
}
}
service 层
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="accountService" class="Service.AccountServiceImpl"></bean>
DAO 层
<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="accountDao" class="Dao.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
数据库
jdbc.properties 文件已导入,设置好数据库的键值对、
然后在测试时出现该错误,思考一下,可能是该数据库文件的键名称与 DAO 层的 dataSource 不对应,或者是名称打错
开始调试 bug-----------------------------------
我先删除了 DAO 层的第一行
<property name="driverClass" value="${jdbc.driverClass}"/>
然后错误信息就变了
Could not resolve placeholder ''jdbc.driverClassName'' in string value "${jdbc.url} 错误
证明不是名称打错,应该是文件 jdbc.properties 导入失败 -------------------------
再次寻找原因
网上说修改一下
<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true" />
在其后面加上
ignore-unresolvable="true" 属性,测试后还是同样的错误 ---------------------------------------
再次寻找原因
在网上找到
可能是 Spring 容器的配置问题
Spring 容器采用反射扫描的发现机制,在探测到 Spring 容器中有一个 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 的 Bean 就会停止对剩余 PropertyPlaceholderConfigurer 的扫描(Spring 3.1 已经使用 PropertySourcesPlaceholderConfigurer 替代 PropertyPlaceholderConfigurer 了)。
而这个基于命名空间的配置,其实内部就是创建一个 PropertyPlaceholderConfigurer Bean 而已。换句话说,即 Spring 容器仅允许最多定义一个 PropertyPlaceholderConfigurer (或),其余的会被 Spring 忽略掉(其实 Spring 如果提供一个警告就好了)。
PropertyPlaceholderConfigurer 配置,我将其全部删除,然后测试,没有错误,成功了,成功修改数据库数据!
今天关于在过去2天检索到的Java 1.7.0下运行的SQL-Server和MSSQL-JDBC 3.0中的日期列的分享就到这里,希望大家有所收获,若想了解更多关于Apache Kafka 中的 IDENTITY_INSERT 问题 - JDBC - MSSQL、com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别、Could not load JDBC driver class [com.mysql.jdbc.Driver]、Could not resolve placeholder ''jdbc.driverClassName'' in string value "${jdbc.driverClassName} 错误等相关知识,可以在本站进行查询。
本文标签: