我用java访问MySQL数据库时,显示出来的中文全是乱码了,求求各位大虾帮我指点一下了,谢谢了,小弟我是新手,初次接触MySQL,希望能帮帮我!!现在我没有分了,以后给你们补上!!
我程序如下:
import java.sql.*;
public class UseExecuteQuery {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3307/sql_test";
String userName = "root";
String password = "root";
String sql = null;
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
System.out.println("加载驱动器类时出现异常");
}

try {
conn = DriverManager.getConnection(url, userName, password);

//创建Statement语句
stmt = conn.createStatement();
sql = "SELECT * FROM student";

//使用executeQuery执行SQL查询语句
ResultSet rs = stmt.executeQuery(sql);

//显示返回的结果集
while (rs.next()) {
String id      = rs.getString(1);
String name    = rs.getString(2);
String address = rs.getString(3);
System.out.println(id + "   " + name + "   " + address);
}

rs.close();
stmt.close();

} catch(SQLException e) {
System.out.println("出现SQLException异常");
} finally {
//关闭语句和数据库连接
try {
if (conn != null) conn.close();
} catch(SQLException e) {
System.out.println("关闭数据库连接时出现异常");
}
}

}
}