数据库连接池常见错误集合

JDBC常见错误集合

1.格式错误导致添加异常

例如:
/*[Error Code 1366: Incorrect string value: '\xE6\xAF\x9B\xE6\xB3\xBD...' for column 'username' at row 1]*/

原因:表格的字符格式不是UTF-8,所以需要更改表格的字符格式。

ALTER TABLE jdbc_user DEFAULT CHARACTER SET utf8;

修改执行:insert命令发现依旧报错

错误类型不变,此时使用change语法,将整列的字符格式更改为UTF-8,此时成功解决,发现因为之前知识username这一列字段的格式出现问题,问题原因是因为之前在添加数据时,没有添加中文数据,而数据库的默认字符格式不是UTF-8。

ALTER TABLE jdbc_user CHANGE username username VARCHAR(50) CHARACTER SET utf8 NOT NULL;

C3P0中关于配置文件c3p0-config.xml的文件位置不对导致的异常

在idea中使用C3P0数据库连接池,由于使用JDBC时,我们需要先写一个JDBC工具类,工具类中需要设置DriverName驱动包常量,url地址常量,用户名常量等,对于Java编程来说,这是不符合我们三大特性之一的封装性,如果需要更改我们需要修改源代码。所以为了防止这样的情况出现,我们可以通过配置文件来修改,达到我们的目的。

所以使用C3P0数据库连接池时,我们应该先对其配置文件进行规范。C3P0配置文件有两种格式:

  1. c3p0-config.xml,名字固定不可更改。
  2. c3p0.properties,名字不固定。

此处只做xml文件展示。

1.c3p0-config.xml创建xml文件

<c3p0-config>
  
  <!--默认配置-->
    <default-config>  
		<!-- initialPoolSize:初始化时获取三个连接,
			  取值应在minPoolSize与maxPoolSize之间。 --> 
        <property name="initialPoolSize">3</property>  
		
		<!-- maxIdleTime:最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。-->
        <property name="maxIdleTime">60</property>  
		
		<!-- maxPoolSize:连接池中保留的最大连接数 -->
        <property name="maxPoolSize">100</property>  
		<!-- minPoolSize: 连接池中保留的最小连接数 -->
        <property name="minPoolSize">10</property>  
		
    </default-config>  
  
   <!--配置连接池mysql-->

    <named-config name="mysql">
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/table?characterEncoding=UTF-8</property>
        <property name="user">root</property>
        <property name="password">password</property>
        <property name="initialPoolSize">10</property>
        <property name="maxIdleTime">30</property>
        <property name="maxPoolSize">100</property>
        <property name="minPoolSize">10</property>
    </named-config>
    <!--配置连接池2,可以配置多个-->

</c3p0-config>

要注意的是:

  1. jdbcUrl的内容一定要书写正确。
  2. 书写后,xml如果位置正确,idea会自动导入。

错误示范
1.与源代码文件夹分开在这里插入图片描述

2.放在源代码下一级文件目录中
在这里插入图片描述
造成错误提醒:
在这里插入图片描述
named-config with name ‘mysql’ does not exist. Using default-config.会显示mysql这个数据库接口不存在,

**原因:**idea在运行时只会自动在源代码这一层级搜索xml文件。

正确的做法:
在这里插入图片描述
运行结果:在这里插入图片描述
此时没有报错信息。

**总结:**c3p0-config.xml配置文件需要放在源目录src里。

关于Druid连接池的配置文件Properties问题

在这里插入图片描述
此问题出现的数字格式转换异常,加上后面空指针异常。

查看源代码:

 while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                int digit = Character.digit(s.charAt(i++), radix);
                if (digit < 0 || result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
            }
if (s == null) {
            throw new NumberFormatException("null");
        }

在转换过程中有两个点:

  1. 不能为空
  2. 字符串后面不能有空格,否则会导致result < multmin

通过检查发现,在书写
Druid.properties文件时

driverClassName = com.mysql.jdbc.Driver
url = jdbc:mysql://127.0.0.1:3306/db2?characterEncoding=UTF-8
username = root
password = 'password'
initialSize = 5
maxActive = 10 
maxWait = 3000

maxActive = 10 后面有一个多余的空格。导致出现了问题。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值