---
# System prepended metadata

title: 用prepared statement的欄位需要新增多國語言時的解決方法
tags: [Java]

---

# 用prepared statement的欄位需要新增多國語言時的解決方法

用setNString來把欄位設成Unicode
```java=
String sqlSms = "INSERT INTO Sms (SmsSeq, SmsSysId,Content,OfficeHourSend,Status,LastUpdateTime,MaintainHost,MaintainProgramId,MainUserId,CreateDate,CreateProgramId,CreateTime,CreateUserId,DeleteFlag,Mobile,Reserved,BatchMark,InstantFlag,ExpectDeliverTime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

PreparedStatement stmtSms = null;

try {
    if(smsList != null && !smsList.isEmpty()) {
        long start = System.currentTimeMillis();
        connection.setAutoCommit(false);
        //通過JDBC API執行用於批量更新的SQL語句

        stmtSms = connection.prepareStatement(sqlSms);

        for(Sms bean:smsList) {
            ...
            stmtSms.setNString(3, bean.getContent());
            ...
            stmtSms.addBatch();
        }
        stmtSms.executeBatch();

        long end = System.currentTimeMillis();

        System.out.println(String.format("Total time: %d 毫秒", (end - start)));
        logger.info(String.format("Total time: %d 毫秒", (end - start)));
        connection.commit();
    }
}catch(SQLException e) {
    try {
        connection.rollback();
    } catch (SQLException sqlException) {
        sqlException.printStackTrace();
    }
}finally {
    if (stmtSms != null) {
        try {
            stmtSms.close();
        } catch (SQLException e) {
            logger.error("preparedStatement.close() fail.", e);
            System.out.println("preparedStatement.close() fail."+e);
        }
    }

    if (connection != null) {
        try {
            connection.setAutoCommit(true);
            connection.close();
        } catch (SQLException e) {
            logger.error("connection.close() fail.", e);
            System.out.println("connection.close() fail."+ e);
        }
    }
}
```


###### tags: `Java`