zoukankan      html  css  js  c++  java
  • Java: JDBC连接MySQL数据库插入中文内容出现乱码

    如上图, 向MySQL数据库中插入中文内容时, 插入的信息变成了问号。

    解决办法如下:

    1. 设置jsp页面的编码格式。

    <%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <%response.setCharacterEncoding("UTF-8");%>

    我的项目中是这样的:

    <!--index.jsp作为程序中的主页,用于放置添加图书信息的表单。此表单提交到AddBook.jsp页面处理-->
    <%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <%response.setCharacterEncoding("UTF-8");%>
    <form action="AddBook.jsp" method="post" onsubmit="return check(this);">
        <h2>添加图书信息</h2>
        图书名称: <input type="text" name="title"><br>
        图书价格: <input type="text" name="price"><br>
        图书数量: <input type="text" name="amount"><br>
        图书作者: <input type="text" name="author">
        <br>
        <br>
        <input align="right" type="submit" value="提交" name="submit">
    </form>

    2. 设置数据库的编码方式:

    <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
    <%request.setCharacterEncoding("UTF-8");%>

    我的项目中是这样的:

    <!--本AddBook.jsp页面用于处理添加图书信息的请求。此页面通过JDBC所提交的图书信息数据写入数据库中-->
    <%@ page import="java.sql.Connection" %>
    <%@ page import="java.sql.DriverManager" %>
    <%@ page import="java.sql.PreparedStatement" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
    <%request.setCharacterEncoding("UTF-8");%>
    <jsp:useBean id="book" class="cth.Book"/>
    <jsp:setProperty name="book" property="*"/>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <%
        Connection connection = null;//Connection连接
        Class.forName("com.mysql.jdbc.Driver");//加载数据驱动,注册到驱动管理器
        String url = "jdbc:mysql://localhost:3306/mysql?characterEncoding=utf-8";//数据库连接字符串
        String username = "Chintsai";//数据库用户名
        String password = "1234";//数据库密码
        connection = DriverManager.getConnection(url, username, password);
        String sql = "insert into purchase_book(title,price,amount,author) values (?,?,?,?)";//添加图书信息的SQL语句
        PreparedStatement preparedStatement = null;//获取PreparedStatement
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, book.getTitle());//对SQL语句中的第1个参数赋值
            preparedStatement.setDouble(2, book.getPrice());//对SQL语句中的第2个参数赋值
            preparedStatement.setInt(3, book.getBookCount());//对SQL语句中的第3个参数赋值
            preparedStatement.setString(4, book.getAuthor());//对SQL语句中的第4个参数赋值
            int row = preparedStatement.executeUpdate();//执行更新操作,返回所影响的行数
            if (row > 0)//判断是否更新成功
                out.println("成功添加了" + row + " 条信息");//更新成功输出信息
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {//关闭PreparedStatement,释放资源
                if (preparedStatement != null)
                    preparedStatement.close();
                preparedStatement = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {//关闭Connection,释放资源
                if (connection != null)
                    connection.close();
                connection = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    %>
    <br>
    <a href="index.jsp">返回</a>
    </body>
    </html>
    

    3. 设置JDBC连接的编码方式:

    即上图中的那一行代码:

     String url = "jdbc:mysql://localhost:3306/mysql?characterEncoding=utf-8";//数据库连接字符串

    这一步很多人容易疏漏,因而产生乱码,要特别注意哟 (^U^)ノ~YO

    最后,贴上完整的项目代码:Github

    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    【BZOJ1930】[Shoi2003]pacman 吃豆豆 最大费用最大流
    【BZOJ3444】最后的晚餐 乱搞
    C语言的 32个关键之和9个控制语言之关键字
    10进制如何转二进制
    (转)这些开源项目,你都知道吗?(持续更新中...)[原创]
    如何将一个HTML页面嵌套在另一个页面中
    SNMP学习笔记之SNMPWALK 命令
    Linux学习笔记之Centos7设置Linux静态IP
    Linux学习笔记之Linux通过yum安装桌面
    Linux学习笔记之Linux启动级别
  • 原文地址:https://www.cnblogs.com/chintsai/p/10117043.html
Copyright © 2011-2022 走看看