程序:
public static void main(String[] args)
{
Connection connection = null;
Statement statement;
ResultSet resultSet;
// 加载驱动程序以连接数据库
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//这个驱动不能是其他的..
connection = DriverManager.getConnection("jdbc:odbc:user","root","123456"); //user是data Source ,root是用户名,123456是进入mysql的密码
System.out.println("open easy");
String query = "Select * from userrole";
statement = connection.createStatement();
resultSet = statement.executeQuery( query );
if(resultSet!=null){
while(resultSet.next()){
String s1=resultSet.getString(1);
String s2 = new String(s1.getBytes("ISO-8859-1"),"GBK");
System.out.println(s1+"..........."+s2);
}
}
connection.close();
System.out.println("close easy"); }
// 捕获加载驱动程序异常
catch ( ClassNotFoundException cnfex )
{
System.err.println(
"装载 JDBC/ODBC 驱动程序失败。" );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
// 捕获连接数据库异常
catch ( SQLException sqlex )
{
System.err.println( "无法连接数据库" );
sqlex.printStackTrace();
System.exit( 1 ); // terminate program
}
catch (Exception e)
{
System.out.println(e.toString());
}
}//上面的程序引用自:http://blog.csdn.net/cctt_1/archive/2006/08/13/1057361.aspx运行后的结果:
open easy??...........????...........????...........??tommy...........tommyclose easy
----------------------------------------------------------------------
而Mysql表userrole中的数据为:mysql> select * from userrole;
+-------+
| name |
+-------+
| 张三 |
| 李四 |
| 王五 |
| tommy |
+-------+
4 rows in set (0.00 sec)
表编码字符为:
mysql> show create table userrole;
+----------+---------------------------
-----------------------------+
| Table | Create Table
|
+----------+---------------------------
-----------------------------+
| userrole | CREATE TABLE `userrole` (
`name` varchar(20) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+----------+---------------------------
-----------------------------+
1 row in set (0.00 sec)
====================================================================请问:出现乱码??的原因是什么?应该怎么修改可以使读取的数据不为乱码?谢谢!
public static void main(String[] args)
{
Connection connection = null;
Statement statement;
ResultSet resultSet;
// 加载驱动程序以连接数据库
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//这个驱动不能是其他的..
connection = DriverManager.getConnection("jdbc:odbc:user","root","123456"); //user是data Source ,root是用户名,123456是进入mysql的密码
System.out.println("open easy");
String query = "Select * from userrole";
statement = connection.createStatement();
resultSet = statement.executeQuery( query );
if(resultSet!=null){
while(resultSet.next()){
String s1=resultSet.getString(1);
String s2 = new String(s1.getBytes("ISO-8859-1"),"GBK");
System.out.println(s1+"..........."+s2);
}
}
connection.close();
System.out.println("close easy"); }
// 捕获加载驱动程序异常
catch ( ClassNotFoundException cnfex )
{
System.err.println(
"装载 JDBC/ODBC 驱动程序失败。" );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
// 捕获连接数据库异常
catch ( SQLException sqlex )
{
System.err.println( "无法连接数据库" );
sqlex.printStackTrace();
System.exit( 1 ); // terminate program
}
catch (Exception e)
{
System.out.println(e.toString());
}
}//上面的程序引用自:http://blog.csdn.net/cctt_1/archive/2006/08/13/1057361.aspx运行后的结果:
open easy??...........????...........????...........??tommy...........tommyclose easy
----------------------------------------------------------------------
而Mysql表userrole中的数据为:mysql> select * from userrole;
+-------+
| name |
+-------+
| 张三 |
| 李四 |
| 王五 |
| tommy |
+-------+
4 rows in set (0.00 sec)
表编码字符为:
mysql> show create table userrole;
+----------+---------------------------
-----------------------------+
| Table | Create Table
|
+----------+---------------------------
-----------------------------+
| userrole | CREATE TABLE `userrole` (
`name` varchar(20) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+----------+---------------------------
-----------------------------+
1 row in set (0.00 sec)
====================================================================请问:出现乱码??的原因是什么?应该怎么修改可以使读取的数据不为乱码?谢谢!
解决方案 »
- 权值搜索的问题
- SHOW VARIABLES LIKE 'BASE%'
- 请教有没有好的工具或者方法用于观察和比较数据库变化
- 一篇文章可以属于多个专题,我在文章表里添加一个字段nvarchar(4000) 然后吧文章所属的专题的ID 用豆号分割? 这样写法好不好?
- 请问MySql的存储过程可以返回一个数据集吗?高手指教,在线等待!
- 难!!!!!从.xls导入到SQL数据库,为什么有的表提示:空值无法插入!!!!????
- 如何大批量修改某个字段部分的值?
- Mysql二进制备份文件还原缓慢
- 查询一行中为NULL或为空的字段数量
- mysql 查询慢的问题(很有挑战)
- 请大家帮忙实现一个功能-关于MYSQL统计数字功能
- 复杂查询 结果重复 请求帮助!谢谢!
试试
填入后,
D:\temp>java Test
open easy
张三...........??
李四...........??
王五...........??
tommy...........tommy
close easy。