刚才看了一下mysql数据库里,显示的也是乱码,不是中文英文和数字可以正常显示

解决方案 »

  1.   

    /*
     * 创建日期 2004-3-14
     *
     * 更改所生成文件模板为
     * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
     */
    import java.sql.*;
    import java.util.*;
    import java.io.*;
    /**
     * @author Administrator
     *
     * 更改所生成类型注释的模板为
     * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
     */
    public class MysqlTest { static String sqlstmt1 = "insert into Greetings values ('hello,world,山西用友')";
    private static byte[] dbByte;
    public static void main(String[] args) {
    try
    {

    Connection conn = getConnection();
    Statement stat = conn.createStatement();

    // stat.execute("use pubs");
    stat.execute("drop table greetings");
    stat.execute("create table Greetings (name char(30))");
    dbByte = sqlstmt1.getBytes();
    String sqlstmt = new String (dbByte,"ISO-8859-1");
    // stat.execute("insert into Greetings values ('hello,world,"+sqlstmt+"')");
    stat.execute(sqlstmt);
    ResultSet result = stat.executeQuery("select * from Greetings");
    result.next();
    String dbstr2 =new String ();
    dbstr2 = result.getString(1);
    //String dbByte = new String(); 
    dbByte = dbstr2.getBytes("ISO-8859-1");
    String dbStr1 = new String(dbByte);
    System.out.println(dbStr1);
    result.close();

    stat.close();
    conn.close();
    }
    catch (SQLException ex)
    {
    while (ex != null)
    {
    ex.printStackTrace();
    ex = ex.getNextException();
    }
    }
    catch (IOException ex)
    {
    ex.printStackTrace();
    }


    }
    public static Connection getConnection() throws SQLException , IOException
    {
    Properties props = new Properties();
    FileInputStream in = new FileInputStream("database.properties");
    props.load(in);
    in.close();

    String drivers = props.getProperty("jdbc.drivers");
    if (drivers != null)
    System.setProperty("jdbc.drivers",drivers);
    String url = props.getProperty("jdbc.url");
    String username = props.getProperty("jdbc.username");
    String password = props.getProperty("jdbc.password");
    return DriverManager.getConnection(url,username,password);
    }
    }
      

  2.   

    你插入数据库时,先把要插入的汉字字符串经过如下tran方法转码:public String tran(String code)
        {
            String tmp= new String(code.getBytes(),"ISO-8859-1");
            return tmp;
        }
    把tmp再插入数据库
      

  3.   

    "jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=GBK";
      

  4.   

    "jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=GBK";爱笛生的这个方法不行呢不过还是谢谢你啊我的问题依然有待解决啊
      

  5.   

    SD_wxb()的方法你试试吧,应该可以解决你的问题,
    乱码是你在插入的时候没有转换字符集导致的。