使用PreparedStatement操作mysql数据库出现中文乱码问题

背景:

    在使用PreparedStatement执行setString(4, "我是中文");后,通过debug发现中文变成了???;这个肯定是编码问题,然后通过字符串和字节码进行编码转换也没能够解决这个问题;最后在网上搜到一篇文章提到,在连接mysql数据的时候需要制定编码:?characterEncoding=utf8

解决:

   代码中连接数据库的时候需要制定编码:

public String getMysqlUrl() {
        return "jdbc:mysql://" + ip + ":" + port + "/" + sid+"?characterEncoding=utf8";
    }

   下面是整个测试代码:

package com.scu.tgm.test;

import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.*;

public class TestMysql {
    private static final String ORACLE = "oracle";
    private static final String SQL_SERVER = "mssql";
    private static final String SYBASE = "sybase";
    private static final String MYSQL = "mysql";

    private static final String MYSQL_DRIVER_CLASS = "com.mysql.jdbc.Driver";
    private static final String ORACLE_DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
    private static final String SQLSERVER_DRIVER_CLASS = "net.sourceforge.jtds.jdbc.Driver";
    private static final String SYBASE_DRIVER_CLASS = "com.sybase.jdbc3.jdbc.SybDriver";


    private String type     ="mysql";    
    private String ip       ="localhost";
    private String port     ="3306";     
    private String user     ="root";     
    private String sid      ="tgmtest";  
    private String password ="root";     

    private Connection connection;
	

    public static void main(String[] str) throws UnsupportedEncodingException{
    	TestMysql testMysql=new TestMysql();
        String sql="Insert into sms_outbox  (sismsid, extcode, destaddr, messagecontent,reqdeliveryreport,msgfmt,sendmethod,requesttime,applicationid) VALUES(?,?,?,?,?,?,?,?,?)";
        testMysql.createConnect();
        try {
        	testMysql.executeInsert(sql,"123;44","八嘎雅路");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println();
    }

    /**
     * Insert into t_user
     * (sismsid, extcode, destaddr, messagecontent,reqdeliveryreport,msgfmt,sendmethod,requesttime,applicationid)
     * VALUES(?,?,?,?,?,?,?,?,?)
     * @param sql
     * @return
     * @throws SQLException
     * @throws UnsupportedEncodingException 
     */
    public void executeInsert(String sql,String phNumbers,String messagecontent) throws SQLException, UnsupportedEncodingException {
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, UUID.randomUUID().toString());
        ps.setString(2, "");
        ps.setString(3, phNumbers);
        ps.setString(4, messagecontent);
        ps.setByte(5, (byte) 1);
        ps.setByte(6, (byte) 15);
        ps.setByte(7,(byte) 0);
        ps.setTimestamp(8, new Timestamp((new java.util.Date()).getTime()));
        ps.setString(9, "dxcs");
        ps.executeUpdate();
    }

    public void createConnect() {
        if (ORACLE.equalsIgnoreCase(type)) {

            String fornameCalss = ORACLE_DRIVER_CLASS;
            String url = getOracleUrl();
            connection= createConnection(fornameCalss, url,user,password);
        } else if (SQL_SERVER.equalsIgnoreCase(type)) {

            String fornameCalss = SQLSERVER_DRIVER_CLASS;
            String url = getSqlserverUrl();
            connection= createConnection(fornameCalss, url,user,password);
        } else if (SYBASE.equalsIgnoreCase(type)) {

            String fornameCalss = SYBASE_DRIVER_CLASS;
            String url = getSybaseUrl();
            connection= createConnection(fornameCalss, url,user,password);
        } else if (MYSQL.equalsIgnoreCase(type)) {

            String fornameCalss = MYSQL_DRIVER_CLASS;
            String url = getMysqlUrl();
            connection= createConnection(fornameCalss, url,user,password);
        }else {

        }
    }
    private Connection createConnection(String fornameCalss, String url,String user, String password) {
        Connection connection = null;
        try {
            Class.forName(fornameCalss);
            connection = DriverManager.getConnection(url, user,password);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return connection;
    }
    public String getSybaseUrl() {
        return "jdbc:sybase:Tds:" + ip + ":" + port + "/" + sid;
    }

    public String getSqlserverUrl() {
        return "jdbc:jtds:sqlserver://" + ip + ":" + port + "/" + sid;
    }

    public String getOracleUrl() {
        return "jdbc:oracle:thin:@" + ip + ":" + port + ":" + sid;
    }

    public String getMysqlUrl() {
        return "jdbc:mysql://" + ip + ":" + port + "/" + sid+"?characterEncoding=utf8";
    }



}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值