标签:jdbc


ORA-00911: 无效字符


一般情况下是由于JDBC要执行的SQL语句尾部多结束符造成的,即“;”

Spring提供的JdbcTemplate简单使用


所需maven配置

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jdbc</artifactId>
 <version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-tx</artifactId>
 <version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>6.0.6</version>
</dependency>
// JDBC模板依赖于连接池来获得数据的连接,所以必须先要构造连接池
         DriverManagerDataSource dataSource = new DriverManagerDataSource();
         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
         dataSource.setUrl("jdbc:mysql://localhost:3306/spring");
         dataSource.setUsername("root");
         dataSource.setPassword("123456");
 
         // 创建JDBC模板
         JdbcTemplate jdbcTemplate = new JdbcTemplate();
         // 这里也可以使用构造方法
         jdbcTemplate.setDataSource(dataSource);
 
         // sql语句
         String sql = "select count(*)  from user";
         Long num = (long) jdbcTemplate.queryForObject(sql, Long.class);
 
         System.out.println(num);