当前位置:编程学习 > JAVA >>

怎样解决向mysql中输入数据是乱码的问题

源代码如下:
package mytest.message;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: AddMessageServlet
 *
 */

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class AddMessageServlet extends HttpServlet {
    private Connection con;
    public void doGet(HttpServletRequest request ,HttpServletResponse response)
         throws IOException,ServletException{
     response.setContentType("text/html;charset=gbk");
         request.setCharacterEncoding("gbk");
        //String name1=new String(request.getParameter("name1").getBytes("ISO-8859-1"),"gbk");
        String name1=request.getParameter("name1");
        String num=new String(request.getParameter("num").getBytes("ISO-8859-1"),"gbk");
        String title=new String(request.getParameter("title").getBytes("ISO-8859-1"),"gbk");
        String editor1 = new String(request.getParameter("editor1").getBytes("ISO-8859-1"),"gbk");
    
        if(name1==null)
            name1 = "";
        if(title==null)
            title = "";
        if(editor1==null)
         editor1 = "";
        if(num==null)
         num="";
        try
        {
            PreparedStatement stm=con.prepareStatement("insert into liuyan values(?,?,?,?,?)");
            stm.setString(1,name1);
            
            stm.setString(2,num);
            stm.setString(3,title);
            stm.setString(4,editor1);
            stm.setDate(5,new java.sql.Date(new java.util.Date().getTime()));
            try
            {
                stm.executeUpdate();
            }catch(Exception e){}
            //关闭数据库库连接
                //con.close();
            RequestDispatcher dispatcher =
     request.getRequestDispatcher("/ViewMessageServlet");
            dispatcher.forward(request, response);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
    }
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException ,ServletException{
     doGet(request,response);
}
     public AddMessageServlet()
     {
      String url="jdbc:mysql://localhost:3306/myproject?useUnicode=true&characterEncoding=gb2312";
String userName = "root";
String password = "1234";
String sql = null;
con = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
System.out.println("加载驱动器类时出现异常");
}
try {
con = DriverManager.getConnection(url, userName, password);
} catch(SQLException e) {
System.out.println("出现SQLException异常");
}
     }
   } 

--------------------编程问答-------------------- 1、创建数据库时 ,指定编码:

GBK: create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; 

UTF8: CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci 


2、

my.ini中配置默认编码
default-character-set=utf8

连接数据库设置编码
jdbc:mysql://192.168.0.5:3306/test?characterEncoding=utf8


java中的常用编码UTF-8;GBK;GB2312;ISO-8859-1;
对应mysql数据库中的编码utf8;gbk;gb2312;latin1



---------------------------
数据库连接串中指定字符集URL=jdbc:mysql://yourIP/college?user=root&password=yourPassword&useUnicode=true&characterEncoding=gbk
--------------------编程问答-------------------- mysql指定utf-8 --------------------编程问答-------------------- my.ini中配置默认编码 --------------------编程问答-------------------- mysql编码修改为GBK这样直接查询数据库也不会乱码 --------------------编程问答-------------------- 你数据库的编码格式是什么呢? --------------------编程问答-------------------- 可以修改一下mysql目录下的my.ini文件


The following options will be read by MySQL client applications.
# Note that only client applications shipped by MySQL are guaranteed
# to read this section. If you want your own MySQL client program to
# honor these values, you need to specify it as an option during the
# MySQL client library initialization.
#
[client]

port=3306

[mysql]

default-character-set=gbk(修改)


# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this 
# file.
#
[mysqld]

# The TCP/IP Port the MySQL Server will listen on
port=3306


#Path to installation directory. All paths are usually resolved relative to this.
basedir="C:/Program Files/MySQL/MySQL Server 5.0/"

#Path to the database root
datadir="C:/Program Files/MySQL/MySQL Server 5.0/Data/"

# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=utf8(修改)


然后你的数据库的编码换成utf8 --------------------编程问答-------------------- Mysql的安装很简单
但是在安装的过程中有一个设置编码的弹窗
选择 自定义 选择 GBK
这样 数据库里就是中文。

把他卸载了在装一下 设置编码 就OK了 
听我的没错 我一直用的都是这玩意 --------------------编程问答-------------------- 是不是要在数据库当中设置编码方式啊~
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,