Java 和 MySql 为此类设置 unicode 字符 [英] Java and MySql set unicode characters for this class

查看:58
本文介绍了Java 和 MySql 为此类设置 unicode 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to input unicode characters (greek) to my database. However I read that in order to do so I have to set the unicode characters to active. How can I set the character set to unicode for the specific class?

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());
        }
    }
}

推荐答案

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

Pass a parameter as boolean to make the connection dynamically based on your requirements. You will need encoding at database end as well, I hope that is already there.

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

Based on the condition change the connectivity string.

改变

  "jdbc:mysql://localhost",

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

可选地,可以指定排序规则,例如:&connectionCollat​​ion=utf8_general_ci

Optionally, a collation can be specified, such as: &connectionCollation=utf8_general_ci

这篇关于Java 和 MySql 为此类设置 unicode 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆