mysql中利用jdbc插入中文数据出现乱码!
我的程序中当把一些中文字符插入数据库中,数据库中的纪录为乱码,甚至一些中文字符程序执行时就出现数组越界异常。
import java.io.*;
import java.sql.*;
import java.util.*;
public class test{
public static void main(String[] args){
Connection con=null;
try {
Class.forName("org.gjt.mm.mysql.Driver");//
con=DriverManager.getConnection("jdbc:mysql://localhost/performance_monitor");
}
catch(Exception e){e.printStackTrace();}
try
{
String fileRecord="打";//当fileRecord是“打”时程序正常,为“打到”时程序就不能执行了
int getID=12;
String sqlCommand;
sqlCommand="insert into softlog(id,softname)"+"values(?,?)";
PreparedStatement preparedStatement=con.prepareStatement(sqlCommand);
preparedStatement.setInt(1,getID);
// fileRecord=new String(fileRecord.getBytes("gb2312"),"ISO-8859-1");
preparedStatement.setString(2,fileRecord);
preparedStatement.executeUpdate();
con.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}