急,写了一个JDBC程序出乱码,问题在哪?
import java.sql.*;
public class ConnMysql { public static void main(String[] args) throws Exception{
// 1.加载驱动,使用反射的知识,现在记住这么写。
Class.forName("com.mysql.jdbc.Driver");
//2.使用DriverManager获取数据库连接,
//其中返回的Connection就代表了Java程序和数据库的连接
//不同数据库的URL写法需要查驱动文档知道,用户名、密码由DBA分配
Connection conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/select_test" ,
"root" , "456abc,,"); //3.使用Connection来创建一个Statment对象
Statement stmt = conn.createStatement();
//4.执行SQL语句。
/*
Statement有三种执行sql语句的方法:
1 execute 可执行任何SQL语句。- 返回一个boolean值,
  如果执行后第一个结果是ResultSet,则返回true,否则返回false
2 executeQuery 执行Select语句 - 返回查询到的结果集
3 executeUpdate 用于执行DML语句。- 返回一个整数,代表被SQL语句影响的记录条数
*/
ResultSet rs = stmt.executeQuery("select s.* , teacher_name from student_table s , "
+ "teacher_table t where t.teacher_id = s.java_teacher");
//ResultSet有系列的getXxx(列索引 | 列名),用于获取记录指针指向行、特定列的值
//不断地使用next将记录指针下移一行,如果依然指向有效行,则指针指向行的记录
while(rs.next())
{
System.out.println(rs.getInt(1) + "\t"
+ rs.getString(2) + "\t"
+ rs.getString(3) + "\t"
+ rs.getString(4));
}
if (rs != null)
{
rs.close();
}
if (stmt != null)
{
stmt.close();
}
if (conn != null)
{
conn.close();
} }
}
运行后显示:
1 ???? 1 Yeeku
2 ???? 1 Yeeku
3 ???? 1 Yeeku
4 ???? 2 Sharfly
5 _???? 2 Sharfly
6 null 2 Sharfly
另MYSQL里的数据如下:
drop database if exists select_test;
create database select_test;
use select_test;
-- 为了保证从表参照的主表存在,通常应该先建主表。
create table teacher_table
(
-- auto_increment:实际上代表所有数据库的自动编号策略,通常用作数据表的逻辑主键。
teacher_id int auto_increment,
teacher_name varchar(255),
primary key(teacher_id)
);
create table student_table
(
-- 为本表建立主键约束
student_id int auto_increment primary key,
student_name varchar(255),
-- 指定java_teacher参照到teacher_table的teacher_id列
java_teacher int,
foreign key(java_teacher) references teacher_table(teacher_id)
);
insert into teacher_table
values
(null , 'Yeeku');
insert into teacher_table
values
(null , 'Sharfly');
insert into teacher_table
values
(null , 'Martine');
insert into student_table
values
(null , '张三' , 1);
insert into student_table
values
(null , '张三' , 1);
insert into student_table
values
(null , '李四' , 1);
insert into student_table
values
(null , '王五' , 2);
insert into student_table
values
(null , '_王五' , 2);insert into student_table
values
(null , null , 2);
insert into student_table
values
(null , '赵六' , null);

解决方案 »

  1.   

    应该是你的Mysql的编码有问题。你去数据库里查查看,看到的应该也是乱码。
      

  2.   

    Connection conn = DriverManager.getConnection(
    "jdbc:mysql://127.0.0.1:3306/select_test" ,
    "root" , "456abc,,");
    jdbc:mysql://127.0.0.1:3306/select_test后面加上你的编码试试
      

  3.   

    在my.ini里面设置你MySQL的默认字符集为utf-8或者gb2312
      

  4.   

    如果是数据库乱码,那末在数据库中set names gbk试一下
      

  5.   

    修改你的MYSQL字符编码  或转换字符
    new String(temp.getBytes("ISO8859-1"))  从数据库读数据到网页
    new String(title1.getBytes("GB2312"),"ISO8859-1")从网页向数据库写数据
      

  6.   

    mysql中的my.ini第一个default-character-set=GBK第二个default-character-set=utf8就不会出现乱码了!
      

  7.   

    Connection conn = DriverManager.getConnection( 
    "jdbc:mysql://127.0.0.1:3306/select_test?useUnicode=true&characterEncoding=utf-8" , 
    "root" , "456abc,,"); 
      

  8.   

    Connection conn = DriverManager.getConnection( 
    "jdbc:mysql://127.0.0.1:3306/select_test?useUnicode=true&characterEncoding=gb2312" , 
    "root" , "456abc,,");