我的Java數據庫中的java程序前面記錄的數據有問題。我檢查了mysql和Netbeans,編碼是utf-8,但我仍然有這種問題。 任何提示??從Mysql獲取數據時的字符問題
我在Mac上使用NetBeans 8.2
我的應用程序顯示這樣的數據:
MySQL的顯示沒有問題的數據:
我的Java數據庫中的java程序前面記錄的數據有問題。我檢查了mysql和Netbeans,編碼是utf-8,但我仍然有這種問題。 任何提示??從Mysql獲取數據時的字符問題
我在Mac上使用NetBeans 8.2
我的應用程序顯示這樣的數據:
MySQL的顯示沒有問題的數據:
這個問題不夠精確,因此可能會嘗試一些觀點。
添加這些語句在Java前端應用程序,數據庫連接後,可以插入任何數據之前:
SET character_set_connection="utf8"
SET character_set_client="utf8"
SET character_set_database="utf8"
SET character_set_results="utf8"
SET character_set_server="utf8"
SET character_set_system="utf8"
也許你不會需要他們所有;隨時嘗試哪些技巧可以做到。
您也可以登錄到MySQL控制檯,並通過發出命令查看實際設置:
mysql> show variables like '%character_set%';
更新...在我的測試過程中,我發現這個問題只有在JTextArea事實上JTextField正確接收數據。所以我很確定問題是組件 – Magobin
我認爲這個問題是Blob內容,我試圖獲取字節並在Utf-8和ISO-8859-1中進行轉換,但沒有任何更改....我也嘗試更改Longtext中的Blob並更改數據在「è」中,我有一個mysql語法錯誤.... – Magobin
解決:問題是Blob內容;使用TEXT或MEDIUM TEXT我解決了所有問題....對於有特殊字符問題的用戶,在Mysql中存儲字符串的最佳方法是Prepared Statement – Magobin
這是代碼,一個生成的JTextArea,所有數據
private void popolaPianificazione(){
String tipo="Pianificazione";
String sql = "SELECT * FROM DomandePianificazione";
ResultSet res = null;
try {
res = MysqlStuff.richiediDatiSQL(sql);
if(res != null){
res.last();
if(res.getRow() != 0){
res.beforeFirst();
while(res.next()){
final String contatore = res.getString("id");
int conta = Integer.parseInt(contatore);
JPanel temp = new javax.swing.JPanel(new MigLayout("fill","grow"));
temp.setBorder(javax.swing.BorderFactory.createTitledBorder("DOMANDA "+"["+conta+"]"));
String domande = res.getString("Domanda");
domande.replace("è", "p");
javax.swing.border.Border border = BorderFactory.createEtchedBorder();
JTextArea domanda = new javax.swing.JTextArea(domande,2,2);
domanda.setBorder(border);
domanda.setBackground(colore);
domanda.setSize(400, 100);
domanda.setFont(font);
domanda.setMinimumSize(new Dimension(400,100));
domanda.setLineWrap(true);
domanda.setWrapStyleWord(true);
domanda.setOpaque(false);
domanda.setEditable(false);
JCheckBox rispostaC = new javax.swing.JCheckBox("Si/No");
JCheckBox rispostaCom = new javax.swing.JCheckBox("A completamento");
String rispostaCheck = res.getString("rispostaCheck");
String rispostaCompleta = res.getString("rispostaCompleta");
if (!"no".equals(rispostaCheck)){
rispostaC.setSelected(true);
}
else{
rispostaCom.setSelected(true);
}
JButton edit = new javax.swing.JButton("Modifica la domanda");
ButtonGroup buttonGroup1 = new javax.swing.ButtonGroup();
buttonGroup1.add(rispostaC);
buttonGroup1.add(rispostaCom);
rispostaC.setEnabled(false);
rispostaC.setRolloverEnabled(false);
rispostaCom.setEnabled(false);
rispostaCom.setRolloverEnabled(false);
temp.add(edit,"wrap");
edit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if ("Salva le modifiche".equals(edit.getLabel())){
System.out.println("Sto salvando...");
String pannello = "DomandePianificazione";
try {
SalvaDomanda(tipo,contatore,domanda,rispostaC,rispostaCom,pannello);
PanelPianificazione.revalidate();
PanelPianificazione.repaint();
} catch (SQLException ex) {
Logger.getLogger(ManageQuestionario.class.getName()).log(Level.SEVERE, null, ex);
}
SKIP
,這是發送數據到mysql的代碼:
public static void inviaDatiSQL(String sql,String stat) throws SQLException, ClassNotFoundException{
UP = connetti();
System.out.println("INVIO dati a DB: \n"+ sql);
Statement stmt = null;
PreparedStatement test = UP.prepareStatement(sql);
test.setString(1, stat);
test.executeUpdate();
System.out.println("Finito !");
}
替換隻是一個小測試.... – Magobin
好的,我解決了它。 基本上這是一個關於Mysql中的列數據的問題,它是BLOB ...我已經嘗試在LONGTEXT中進行更改,但即使所有數據庫都是UTF-8,如果我僅更改了內容類型,它仍然不夠! 我不得不改變兩種排序方式,數據庫和列表。
感謝您的支持!
Alex
您還沒有顯示任何代碼,它們很難幫助您。 –
如果它是在一個blob中,你應該把它看作二進制文件,並將其轉換爲你需要的類型 – Yaman