2017-04-15 54 views
1

我试图插入和检索泰米尔(语言)values.But我越来越像??????JDBC - 如何在MySQL数据库中插入和检索泰米尔值

插入查询一样,

"INSERT INTO category VALUES (19, 'இந்தியா நாட்டின்','A');" 

我的代码一样,

public static void main(String[] args) { 

     System.out.println(""); 

     System.out.println("MySQL Connect Example."); 
      Connection conn = null; 
      String url = "jdbc:mysql://localhost:3306/"; 
      String dbName = "test"; 
      String driver = "com.mysql.jdbc.Driver"; 
      String userName = "root"; 
      String password = "root"; 
      try { 
      Class.forName(driver).newInstance(); 
      conn = (Connection) DriverManager.getConnection(url+dbName,userName,password); 
      System.out.println("Connected to the database"); 
      try{ 
       Statement st = (Statement) conn.createStatement(); 
       st.executeQuery("select categoryname from category"); 
       ResultSet rs = st.getResultSet(); 
       int count = 0 ; 
       while(rs.next()){ 
        String name = rs.getString("categoryname"); 
        System.out.println("Vlalue "+ name); 
        ++count; 
       } 
      } 
       catch (SQLException s){ 
       System.out.println("SQL statement is not executed!"); 
       } 
      conn.close(); 
      System.out.println("Disconnected from database"); 
      } catch (Exception e) { 
      e.printStackTrace(); 
      } 

    } 

注:

我的配置,

OS : windows 7 (32 bit), 
DataBase : MySQL 5.1, 
JAVA : 1.7 
+0

您的连接看起来怎么样,你可以分享你的代码吗? –

+0

@YCF_L,我加了我的基本示例代码.. –

回答

2

您的连接使用useUnicode=true&characterEncoding=utf-8

Connection connection = DriverManager.getConnection(
"jdbc:mysql://db_name?useUnicode=true&characterEncoding=utf-8", "root", "pass"); 

我认为你的数据库是这样创建的:

CREATE TABLE table_name 
(
    ...  
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ; 

这里有一个例子我的结果看起来怎么样:

enter image description here

编辑

代码

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

public class ConnectionJSon { 

    static String driver = "com.mysql.jdbc.Driver"; 
    static String DB_username = "root"; 
    static String DB_password = "admin"; 
    static String DB_URL = 
      "jdbc:mysql://localhost:3306/d3?useUnicode=true&characterEncoding=utf-8"; 

    public static void main(String[] args) throws Exception { 
     try { 

      Connection connection = DriverManager.getConnection(DB_URL, 
        DB_username, DB_password); 
      String q = "INSERT INTO table1 VALUES (?)"; 
      PreparedStatement preparedStatement = connection.prepareStatement(q); 
      preparedStatement.setString(1, "இந்தியா நாட்டின்"); 
      int exc = preparedStatement.executeUpdate(); 
      System.out.println(exc); 
      q = "SELECT * FROM table1"; 
      preparedStatement = connection.prepareStatement(q); 
      ResultSet rs = preparedStatement.executeQuery(); 
      while (rs.next()) { 
       System.out.println(rs.getString(1)); 
      } 
      System.out.println("Success"); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

SQL

create table table1(
    col varchar(30) 
)ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ; 
+0

感谢您的努力。但它不会工作。我有同样的价值,像?????? –

+0

@sathishanish它对我很好,检查我的结果在截图 –

+0

请显示您的导入语句..因为我的导入语句是混合sql和mySql .. –

0

的字符集/归类,你的表/数据库是我们的ing不支持泰米尔语字符。最有可能的是,你想让你的整个数据库支持泰米尔语。这样,无论何时您创建一个新表格,它都将支持泰米尔语。

CREATE DATABASE yourDatabase 
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; 

如果这行不通,那么也许你的MySQL版本不支持utf8mb4字符集。在这种情况下,请尝试仅使用utf8

+0

感谢您的努力。我得到了这个例外。未知字符集:'utf8mb4'。现在我做了什么? –

+0

您使用的是哪个版本的MySQL? –

+0

MySQL版本是5.1 –

相关问题