|
笔者在使用 mysql-connector时,正常的Query 可以查出中文
程序如下
import java.sql.*;
public class mysqlqvb{
Connection con;
Statement st;
ResultSet rs;
public mysqlqvb(){
try{
String name="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/qvb?user=root&password=xxx&useUnicode=true&characterEncoding=GBK";
Class.forName(name);
con=DriverManager.getConnection(url);
System.out.println("连接成功!"+url);
Statement st=con.createStatement();
String sql="select * from teacher";
ResultSet rs=st.executeQuery(sql);
System.out.println("查询成功!"+sql);
System.out.println("Id Name address year");
while(rs.next())
{
String id=rs.getString("id");
id=new String(id.getBytes("ISO-8859-1"),"GB2312");
System.out.print(id+" ");
String na=rs.getString("name");
na=new String(na.getBytes("ISO-8859-1"),"UTF-8");
System.out.print(na+" ");
String addr=rs.getString("address");
addr=new String(addr.getBytes("ISO-8859-1"),"UTF-8");
System.out.print(addr+" ");
String ye=rs.getString("year");
System.out.print(ye);
System.out.print("\n");
}
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
mysqlqvb cms=new mysqlqvb();
}
}
但在使用插入数据的时候
import java.sql.*;
public class imy{
Connection con;
Statement st;
ResultSet rs;
public imy(){
try{
String name="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/qvb?user=root&password=xxx&useUnicode=true&characterEncoding=GBK";
Class.forName(name);
con=DriverManager.getConnection(url);
System.out.println("连接成功!"+url);
Statement st=con.createStatement();
String sql="insert into teacher (name,address,year) values ('邹衍','吉林长岭','1980')";
sql=new String(sql.getBytes("ISO-8859-1"),"UTF-8");
int rs=st.executeUpdate(sql);
System.out.println("查询成功!"+sql);
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
imy cms=new imy();
}
}
所插入的中文全是????
其中id 为数字int
其它字段均为字符,长度也够
请教高手!!! |
|