[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[jfriends:00216] mysql の日本語表示について



お世話になります。横山と申します。
先月はアプリの文字化けについて
質問させてもらった者です。
今回は別の件で日本語の表示がされず
投稿致しました。
FAQのような気もするのですが
ネット検索でこれといえるものが
なく投稿に至りました。

環境はvine2.6  LANG=ja_JP.eucJP
j2sdk1.4.2._01
mysql-3.23.53  character-setの指定はなし

swingやawtを使用してアプリケーションの
作成を行っております。
ウィンドウにテキストフィールドを作成して
その入力値をmysqlのデータベースに登録する
簡単なものを作成しました。
テキストフィールドに日本語を入力して
mysqlにデータが登録されているかを
ターミナルからmysqlのデータを
確認したら日本語入力した箇所が????と
なっておりました。
アルファベットや数字は入力を確認できました。

mysqlのデータベースにターミナルから直接mysql文
を入力した日本語のデータがあります。
これを逆にjavaアプリに表示させようと
テキストエリアに表示させたら
空白となりました。アルファベットや
数字は表示されます。

javaのプログラムで日本語の出入力を
行うための処理が必要だと判断したのですが
どう処理してよいものか分かりませんでした。
誠に恐縮ではありますが、御指導いただけないでしょうか?
現在の私の書いたセンスのないソースで
恐縮でありますが、
データベースから日本語を取得して表示するための箇所と
日本語を入力するための箇所を抜粋して
載せておきます。
----ここからがmysqlからのデータを表示させるためのもの----------------------
class Listsql extends JDialog{
	public Listsql(JFrame owner){
		super(owner,"一覧表示",true);
		Container contentPane = getContentPane();
		JPanel panel = new JPanel();
		area = new JTextArea();
		area.setLineWrap(true);
		JButton execute = new JButton("実行");
		execute.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				area.setText("");
				try {
					Class.forName("com.mysql.jdbc.Driver");
					String url = "jdbc:mysql://localhost/task";
					String user = "****";
					String pass = "****";
					Connection con = DriverManager.getConnection(url,user,pass);
					Statement stmt = con.createStatement();
					String sql = "select * from PersonalInfo";
					ResultSet rs = stmt.executeQuery(sql);
					while(rs.next()){
					long id = rs.getLong("ID");
					String name = rs.getString("Name");
					long age = rs.getLong("Age");
					Date birth = rs.getDate("Birth");
					String comment = rs.getString("Comment");
					area.append(id + "/" + name + "/" + age + "/" + birth + "/" + comment +"\n");   
					}
					rs.close();
					stmt.close();
					con.close();
				}catch(Exception e){				
					e.printStackTrace();
					area.setText("エラー発生" + e);			 
				}
			}
		});				
		JButton cancel = new JButton("キャンセル");
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				setVisible(false);
			}
		});
		panel.add(execute);
		panel.add(cancel);
		contentPane.add(area,BorderLayout.NORTH);
		contentPane.add(panel,BorderLayout.SOUTH);
		setSize(700,500); 	
	}
	private JTextField textField;
	private JTextArea area;
}
-------------------------ここまで------------------------------

----ここからmysqlに入力値を登録するためのもの-------------------
class Insertsql extends JDialog{
	public Insertsql(JFrame owner){
		super(owner,"追加",true);
		Container contentPane = getContentPane();
		txtName = new JTextField(10);
		txtAge = new JTextField(10);
		txtBirth = new JTextField(20);
		txtComment = new JTextField(20);
		JLabel lblName = new JLabel("名前");
		JLabel lblAge = new JLabel("年齢");
		JLabel lblBirth = new JLabel("生年月日");
		JLabel lblComment = new JLabel("コメント");
		JPanel txt = new JPanel();
		JPanel panel = new JPanel();
		JButton execute = new JButton("実行");
		execute.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				try {
					Class.forName("com.mysql.jdbc.Driver");
					String url = "jdbc:mysql://localhost/task";
					String user = "****";
					String pass = "****";
					con = DriverManager.getConnection(url,user,pass);
					String strSQL = "insert PersonalInfo(Name,Age,Birth,Comment) values("+"'"+txtName.getText()+"',"+Integer.parseInt(txtAge.getText())+",'"+ txtBirth.getText()+"',"+"'"+txt
Comment.getText()+"')";
					showMyMessage(doUpdate(strSQL)+"レコードが追加されました");
					stmt.close();
					con.close();
				}catch(Exception e){				
					e.printStackTrace();			 
				}
			}
		});				
		JButton cancel = new JButton("キャンセル");
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				setVisible(false);
			}
		});
		panel.add(execute);
		panel.add(cancel);
		txt.add(lblName);
		txt.add(txtName);
		txt.add(lblAge);
		txt.add(txtAge);
		txt.add(lblBirth);
		txt.add(txtBirth);
		txt.add(lblComment);
		txt.add(txtComment);
		contentPane.add(panel,BorderLayout.SOUTH);
		contentPane.add(txt,BorderLayout.NORTH);
		setSize(700,200); 	
	}
	void showMyMessage(String strMessage){
		JOptionPane.showMessageDialog(this,strMessage);
	}
	int doUpdate(String sql){
		int intCount;
		try{
			stmt = con.createStatement();
			intCount = stmt.executeUpdate(sql);
		}catch(Exception ex){
			showMyMessage("doUpdateでエラー発生"+ex);
			intCount = 0;
		}
		return intCount;
		}
	private JTextField txtName;
	private JTextField txtAge;
	private JTextField txtBirth;
	private JTextField txtComment;
	private Connection con;
	private Statement stmt;
}
-----------------------ここまで-----------------
以上です。よろしくお願いします。