【问题标题】:Java and MySql set unicode characters for this classJava 和 MySql 为这个类设置了 unicode 字符
【发布时间】:2017-01-15 06:26:17
【问题描述】:

我正在尝试将 unicode 字符(希腊语)输入到我的数据库中。但是我读到了,为了这样做,我必须将 unicode 字符设置为活动。如何将特定类的字符集设置为 unicode?​​p>

public class database {

    public static database busDatabase = null;

    protected String connection_url = "";
    protected String driver_name = "";
    protected String name = "";
    protected String user = "";
    protected String password = "";
    protected Class driver_class = null;
    protected static Connection connection = null;
    protected ResultSet results = null;
    protected String current_table = "";
    protected Boolean error = false;

    public database(String name, String user, String password) {
               this(name, user, password, "jdbc:mysql://localhost:3306", "com.mysql.jdbc.Driver");
  }

   //public void run() {
  //   con =  (Connection)DriverManager.getConnection(connection_url, user, password);
 //}

  public static Boolean getCon() throws SQLException
  {
      return  connection.isValid(0); 
  }

  public database(String name, String user, String password, String connection_url, String driver_name)
  {
    this.name           = name;
    this.user           = user;
    this.password       = password;
    this.connection_url = connection_url;
    this.driver_name    = driver_name;
  }

  public static void openBusDatabase()
  {
    try
    {
     busDatabase = new database("appointments", "root",
              "27121993petros", "jdbc:mysql://localhost",
              "com.mysql.jdbc.Driver");
            busDatabase.open();
            System.out.println("dbopened");
        } catch (Exception ex) {
            throw new InvalidQueryException("Unable to open database " + ex.getMessage());
        }
    }
}

【问题讨论】:

标签: java mysql unicode


【解决方案1】:

将参数作为布尔值传递,以根据您的要求动态建立连接。您还需要在数据库端进行编码,我希望已经存在。

根据条件更改连接字符串。

改变

  "jdbc:mysql://localhost",

  "jdbc:mysql://localhost?useUnicode=true&characterSetResults=utf8&characterEncodi‌​ng=UTF-8",

可以选择指定排序规则,例如:&connectionCollation=utf8_general_ci

【讨论】:

  • 我的笔记说你需要所有这些参数:...?useUnicode=true&characterSetResults=utf8&characterEncoding=UTF-8。 (可选)可以指定排序规则:&connectionCollation=utf8_general_ci
  • @RickJames 请编辑答案并添加这些行,如果你觉得舒服的话......谢谢
猜你喜欢
  • 1970-01-01
  • 2010-09-25
  • 2012-09-14
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多