使用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";
    }



}

猜你喜欢

转载自blog.csdn.net/CristianTang/article/details/83144866