mariadb数据库乱码,如何解决。
String driver ="org.mariadb.jdbc.Driver"; //从配置参数中获取数据库url
String url = "jdbc:mariadb://127.0.0.1:3306/maria"
          + "?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull"
          +"&transformedBitIsBoolean=true";
  //从配置参数中获取用户名
  String user = "root";
  //从配置参数中获取密码
  String pass = "123";

  //注册驱动
  try {
      Class.forName(driver);
      //获取数据库连接
      Connection conn = DriverManager.getConnection(url,user,pass);
      //创建Statement对象
      Statement stmt = conn.createStatement();
      //执行查询
      boolean add = stmt.execute("insert into t_exhibition_user set name = '飞',"
              +"company = 'yuan',job = 'yuan',phone = '123345678909',is_buy_equipment = 'N'");

这是我向数据量添加数据的操作,添加失败。错误如下:

java.sql.SQLDataException: (conn=15) Incorrect string value: '\x9E' for column 'name' at row 1
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:225)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.getException(ExceptionMapper.java:165)
    at org.mariadb.jdbc.MariaDbStatement.executeExceptionEpilogue(MariaDbStatement.java:238)
    at org.mariadb.jdbc.MariaDbStatement.executeInternal(MariaDbStatement.java:356)
    at org.mariadb.jdbc.MariaDbStatement.execute(MariaDbStatement.java:407)
    at com.ratta.supernote.note.MariadbAndMysql.main(MariadbAndMysql.java:30)
Caused by: java.sql.SQLException: Incorrect string value: '\x9E' for column 'name' at row 1
Query is: insert into t_exhibition_user set name = '飞',company = 'yuan',job = 'yuan',phone = '123345678909',is_buy_equipment = 'N'
java thread: main
    at org.mariadb.jdbc.internal.util.LogQueryTool.exceptionWithQuery(LogQueryTool.java:126)
    at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.executeQuery(AbstractQueryProtocol.java:222)
    at org.mariadb.jdbc.MariaDbStatement.executeInternal(MariaDbStatement.java:350)
    ... 2 more

这里的中文导致添加数据失败,而如果使用如何代码则可以添加成功

String driver ="com.mysql.jdbc.Driver"; //从配置参数中获取数据库url
String url = "jdbc:mysql://127.0.0.1:3306/maria"
          + "?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull"
          +"&transformedBitIsBoolean=true";
  //从配置参数中获取用户名
  String user = "root";
  //从配置参数中获取密码
  String pass = "123";

  //注册驱动
  try {
      Class.forName(driver);
      //获取数据库连接
      Connection conn = DriverManager.getConnection(url,user,pass);
      //创建Statement对象
      Statement stmt = conn.createStatement();
      //执行查询
      boolean add = stmt.execute("insert into t_exhibition_user set name = '飞',"
              +"company = 'yuan',job = 'yuan',phone = '123345678909',is_buy_equipment = 'N'");

这是为什么呢?数据库是mariadb,数据库字符集如图:
图片说明
图片说明
图片说明

现在不想改变数据库的字符,也不能使用MySQL的连接池驱动,如何才能将中文添成功。

useUnicode=true&characterEncoding=utf-8 这个配置常识过了,没效果。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!