mysql链接时出现乱码,该如何指定全部的编码方式呢

package jdbcdemo;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;


public class jdbcdemo {


private static final String url = "jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8";

private static final String user = "root";

private static final String password = "doce";

public static void main(String[] args) throws Exception {

//1、加载mysql驱动

Class.forName("com.mysql.jdbc.Driver");

  

//2、获得数据库连接

Connection connection=DriverManager.getConnection(url ,user , password);

//3、通过连接操作数据库,实现增删改查

Statement statement=connection.createStatement();

//4、执行sql语句


ResultSet re = statement.executeQuery("select user_name,age from imooc_goddess;");

while(re.next()){

 

System.out.println(re.getString("user_name")+","+re.getInt("age"));

}

}}


qq_微笑王子_0
浏览 2082回答 1
1回答

AScorpio

java不是很清楚,我在php中会用mysql_query("set names utf8"); //告诉数据库,发送和接受数据用utf8;试试看,在java中执行下,set names utf8 这句SQL语句
打开App,查看更多内容
随时随地看视频慕课网APP