MyBatis与JDBC连接数据库所使用的url之间的差异
在Windows7 系统上安装了MySQL 8.0,然后创建Maven工程,配置pom.xml文件,添加了如下依赖:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
(1)在mybatis-config.xml中,配置数据库连接环境:
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" >
<property name="" value="" />
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="password@password" />
</dataSource>
</environment>
</environments>
运行测试函数进行连接测试,出现如下错误提示:
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 102; 对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾。
上网搜索解决方案,才知道 mybatis 的配置文件中,这里 url 中的 '&' 符号应当写成 '&' ,修改后的 url 如下:
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC" />
这样就可以正常连接了。
(2)鉴于1中的错误,尝试使用JDBC连接MySQL时,如下进行连接:
@Test public void testJDBC() { Connection conn = null; try { String userName = "root"; String passWord = "password@password"; String jdbcUrl = "jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC"; Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection(jdbcUrl, userName, passWord); String sql = "select id,countryname,countrycode from country"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String countryname = rs.getString("countryname"); String countrycode = rs.getString("countrycode"); System.out.println(id + "\t" + countryname + "\t" + countrycode); } pstmt.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { System.out.println("Game Over!"); } }
运行测试函数,结果出现如下错误:
java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ';characterEncoding=utf-8&useSSL=false&serverTimezone=UTC'.
尝试修改 url ,将 '&' 改成 '&' , 结果就运行正常了,说明 JDBC 方式连接 MySQL 不需要对 '&' 进行转义。
特此记之。
- 各种数据库使用JDBC连接的URL地址
- 使用JDBC-ODBC方式连接数据库时,如何在代码中指定ODBC数据源信息
- 各种数据库的JDBC驱动下载及连接字符串URL写法
- 各种数据库的JDBC驱动下载及连接字符串URL写法
- java使用JDBC连接数据库
- 使用JDBC连接ODBC数据库的Java代码
- 使用 JDBC 连接不同版本 DB2 数据库的兼容性问题
- JDBC的各种数据库的驱动下载及连接字符串URL写法
- 各种数据库使用JDBC连接的方式
- JDBC连接数据库的URL关键代码
- 在java中使用jdbc的数据库连接大全
- 第一次使用jdbc连接postgre数据库碰到的问题及解决方法
- 使用JDBC建立各种数据库的连接方式集萃
- 使用MyEclipse 6.0连接SQL 2000和2005数据库(JDBC)
- 使用 JDBC 连接不同版本 DB2 数据库的兼容性问题
- 使用 JDBC 连接不同版本 DB2 数据库的兼容性问题
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- 各种数据库的JDBC驱动下载及连接字符串URL写法
- 使用 JDBC 连接不同版本 DB2 数据库的兼容性问题
- 使用 JDBC 连接不同版本 DB2 数据库的兼容性问题