gpt4 book ai didi

java - JDBC - 如何在 MySQL 数据库中插入和检索泰米尔语值

转载 作者:搜寻专家 更新时间:2023-10-30 23:35:25 24 4
gpt4 key购买 nike

我正在尝试插入和检索泰米尔语(语言)值。但我越来越像 ??????

像这样插入查询,

"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

最佳答案

您必须在连接中使用 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 ;

关于java - JDBC - 如何在 MySQL 数据库中插入和检索泰米尔语值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43424728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com