码迷,mamicode.com
首页 > 数据库 > 详细

java中使用db2读取脚本文件的坑

时间:2021-06-03 17:48:45      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:runner   bean   public   char   ESS   rollback   inpu   成功   and   

@Bean
	public DataSource getDataSource() throws SQLException {
		DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
		dataSourceBuilder.driverClassName("org.h2.Driver");
		dataSourceBuilder.url("jdbc:h2:mem:test;database_to_upper=false;");
		dataSourceBuilder.username("sa");
		dataSourceBuilder.password("").build();
		DataSource dataSource = dataSourceBuilder.build();
		Connection connection = null;
		try {
			ClassPathResource classPathResource = new ClassPathResource("test.sql");
			connection = dataSource.getConnection();
			ScriptRunner runner = new ScriptRunner(connection);
		    runner.setErrorLogWriter(null);
            runner.setLogWriter(null);
         // 执行SQL脚本
            runner.runScript(new InputStreamReader(classPathResource.getInputStream(), StandardCharsets.UTF_8));
PreparedStatement preparedStatement2 = connection.prepareStatement("select * from `p_event`");
            ResultSet resultSet2 = preparedStatement2.executeQuery();
            while (resultSet2.next()) {
                int id = resultSet2.getInt("id");
                System.out.println("id:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+id );
            }
            PreparedStatement preparedStatement = connection.prepareStatement("select * from `p_event_record_detail`");
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                System.out.println("id:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+id );
            }
            // 关闭连接
            connection.close();
            // 若成功,打印提示信息
            System.out.println("====== SUCCESS ======");
		} catch (Exception e) {
			log.error(e.getMessage());
		} finally {
			if (connection != null) {
				connection.close();
			}
		}
		return dataSource;
	}

  

如上代码:

  读取完脚本之后,先查询了`p_event`表,查询结果正常。但是在查询`p_event_record_detail`表时,总是抛出异常,异常信息为找不到该表。

 

最终排查结果: 

    db2的sql脚本语法中:注释(comment)的内容中不能包含英文字符分号(;)。

正确写法:

`fail_rollback` tinyint(5)             NOT NULL DEFAULT 1 COMMENT ‘同步事件回滚标记:0:回滚;1:不回滚‘

  

错误写法:(注意sql中的分号,此处为英文符号)

`fail_rollback` tinyint(5)             NOT NULL DEFAULT 1 COMMENT ‘同步事件回滚标记:0:回滚;1:不回滚‘

  

 

java中使用db2读取脚本文件的坑

标签:runner   bean   public   char   ESS   rollback   inpu   成功   and   

原文地址:https://www.cnblogs.com/zhlblogs/p/14843467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!