请问一下大家,mysql数据库如何才能更好的解决乱码问题,我写了一个连mysql的测试类,在里面插入中文,在控制台显示是好的,没有乱码,可是在数据库里查询到的却是乱码,不知道如何解决,而且我在程序里面也已经指定了字符编码的,还是没有效果.测试类:package demo;import java.sql.*;
import demo.NoticeBean;public class JDBCTest
{
public static void main(String[] args)
{
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try
{
String driverName = "org.gjt.mm.mysql.Driver";
Class.forName(driverName).newInstance();
String url = "jdbc:mysql://localhost:3306/JavaWeb?useUnicode=true&characterEncoding=gb2312";
connection = DriverManager.getConnection(url,"root","root");
System.out.println("连接数据库成功");
//用到的各个变量
statement = null;
resultSet = null;
String strSql = null;
NoticeBean bean = null;
String title = null;
String content= null;
try
{
title = "标题";
content = "内容";
strSql = "INSERT INTO notice(title,content) values('"+title+"','"+content+"')";
statement = connection.createStatement();
statement.executeUpdate(strSql);
System.out.println("插入语句执行成功:" + strSql);
}
catch(SQLException ex1)
{
System.err.println("插入失败");
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}}控制台显示正常,打印如下:连接数据库成功
插入语句执行成功:INSERT INTO notice(title,content) values('标题','内容')可是进入mysql数据库里面看却是乱码,还有mysql里面后缀名为frm的是什么文件,是数据文件吗?怎么打开啊!oracle创建的数据文件好像就可以直接打开啊!
import demo.NoticeBean;public class JDBCTest
{
public static void main(String[] args)
{
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try
{
String driverName = "org.gjt.mm.mysql.Driver";
Class.forName(driverName).newInstance();
String url = "jdbc:mysql://localhost:3306/JavaWeb?useUnicode=true&characterEncoding=gb2312";
connection = DriverManager.getConnection(url,"root","root");
System.out.println("连接数据库成功");
//用到的各个变量
statement = null;
resultSet = null;
String strSql = null;
NoticeBean bean = null;
String title = null;
String content= null;
try
{
title = "标题";
content = "内容";
strSql = "INSERT INTO notice(title,content) values('"+title+"','"+content+"')";
statement = connection.createStatement();
statement.executeUpdate(strSql);
System.out.println("插入语句执行成功:" + strSql);
}
catch(SQLException ex1)
{
System.err.println("插入失败");
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}}控制台显示正常,打印如下:连接数据库成功
插入语句执行成功:INSERT INTO notice(title,content) values('标题','内容')可是进入mysql数据库里面看却是乱码,还有mysql里面后缀名为frm的是什么文件,是数据文件吗?怎么打开啊!oracle创建的数据文件好像就可以直接打开啊!
解决方案 »
- 【修改datetime字段值报错】如果把datetime字段修改成大于2039-01-08则报错,小于它则OK,请各位大师帮忙分析一下,谢谢!
- temptable类型的视图问题
- 有一个字段xm,我想实现横向排列,如何做呢?
- 大家帮我分析一下mysql的问题
- C语言连接MYSQL,编译通过,运行崩溃,并且还不理解啥意思
- (高分请高人入内)Mysql动态生成N个TXT文件(N为记录条数)
- 在网上找了一个脚本备份mysql,每天自动备份的,出错,不知啥原因
- mysql中VarChar类型的 National Sorting 修饰符的意思是什么?(马上解决,马上结帖!在线等。)
- mySql中的语言问题
- 双master ,双mysqlproxy 问题
- 如何用一个表中的字段替换另一个表中的字段???
- MySQL 问题待解,高手来
控制台打印也是证明你程序没有错误,不能证明到了数据库还是对的啊!
把连接字符串
jdbc:mysql://localhost:3306/JavaWeb?useUnicode=true&characterEncoding=yourDBType"; characterEncoding部分改成和你的数据库一样的字符集!编码不统一,肯定会出乱码的!
{
System.err.println("插入失败");
} 这样的东东尽量写到专门的文件里面,然后从里面提取变量,不要直接写在程序里。