• 締切済み

javaの文字化けに困っています。

改善方法はありますか。 public class daytime extends HttpServlet { protected String decodeString(String str){ try{ byte[] byteData = str.getBytes("ISO_8859_1"); str = new String(byteData, "Shift_JIS"); }catch(UnsupportedEncodingException e){ return null; } return str; } public void doGet(HttpServletRequest request ,HttpServletResponse response) response.setContentType("text/html;charset=Shift_JIS"); PrintWriter out = response.getWriter(); String tmp; String name = ""; tmp = request.getParameter("name"); Calendar cal = Calendar.getInstance(); out.println("<html lang=\"ja\" >"); out.println("<head>"); out.println("<title>ContextParam Example</title>"); out.println("<style>"); out.println("</style>"); out.println("<meta http-equiv=\"Content-Type\" Content=\"text/html;charset=Shift_JIS\">"); out.println("</head>"); out.println("<body>"); out.println("<table>"); out.println("<p>"); Connection conn = null; String url = "jdbc:mysql://localhost/list2"; String user = "list2"; String password = "list2"; out.println("</table>"); try { Class.forName("com.mysql.jdbc.Driver").newInstance(); out.println("Mysqlのロードに成功"); conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); String sql = "SELECT * FROM goods"; ResultSet rs = stmt.executeQuery(sql); out.println("<table>"); while(rs.next()){ out.println("<tr>"); out.println("<td>" +rs.getInt("id") + "</td>"); out.println("<td>" +rs.getString("name") + "</td>"); out.println("<td>" +rs.getString("mail") + "</td>"); out.println("<td>" +rs.getInt("price") + "</td>"); out.println("<form method=\"GET\" action=\"./daytime3\">"); out.println("<input type=\"hidden\" name=\"id\" value=\""+ rs.getInt("id") + "\">"); out.println("<td><input type=\"submit\" value=\"更新\"></td>"); out.println("<td><input type=\"submit\" value=\"削除\"></td>"); out.println("</form>"); out.println("</tr>"); } out.println("</table>"); rs.close(); stmt.close(); } catch (ClassNotFoundException e){ out.println("ClassNotFoundException:" + e.getMessage()); }catch (SQLException e){ out.println("SQLException:" + e.getMessage()); } catch (Exception e){ out.println("Exception:" + e.getMessage()); }finally{ try{ if (conn != null){ conn.close(); out.println("DBに切断"); }else{ out.println("データベースの接続ない"); } }catch (SQLException e){ } } out.println("</p>"); out.println("<tr>"); out.println("</tr>"); out.println("<A HREF='/dddd/daytime2'>登録</A>"); out.println("<A HREF='/dddd/daytime5'>アンケート</A>"); out.println("<div style=\"font-size: 40px; text-align: center; font-weight: bold\">"); out.println(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE)); out.println("</div>"); out.println("</body>"); out.println("</html>"); protected String decodeString(String str){ try{ byte[] byteData = str.getBytes("ISO_8859_1"); str = new String(byteData, "Shift_JIS"); }catch(UnsupportedEncodingException e){ return null; } return str; } } }

みんなの回答

noname#204159
noname#204159
回答No.1

パソコン内に閉じたプログラムなら、S-JIFで可能。 サーバを経由するなら、UTF-8くらいの文字コード変換は必須。

suigintoh009
質問者

補足

追加、直すところがありましたら教えてください。

関連するQ&A

  • JSPからMysqlへの接続

    タイトルどおり、以下のようなコードでMysqlデータベースに接続しようとしました。 <%@ page contentType="text/html; charset=Shift_JIS" import="java.sql.*" %> <html> <head> <title>データを表示する</title> </head> <body> <% //データベース設定 String sv = "localhost"; //サーバ名 String db = "test"; //データベース名 String user = "feifei"; //ユーザ名 String pass = "feifei"; //パスワード String encode = "EUC_JP"; //文字コード //データベースに接続する Class.forName("org.gjt.mm.mysql.Driver"); String url = "jdbc:mysql://" + sv + "/" + db + "?user=" + user + "&password=" + pass + "&useUnicode=true&characterEncoding=" + encode; Connection conn = DriverManager.getConnection(url); //データを取得する Statement st = conn.createStatement(); String sql = "SELECT * FROM table1"; //実行するSQL ResultSet rs = st.executeQuery(sql); //データを全件表示する out.println("<table border=0>"); while(rs.next()){ out.println("<tr>"); out.println("<td>" + rs.getInt("id") + "</td>"); out.println("<td>" + rs.getString("name") + "</td>"); out.println("<td>" + rs.getInt("point") + "</td>"); out.println("</tr>"); } out.println("</table>"); //データベース接続を閉じる conn.close(); %> </body> </html> しかし、なぜかわかりませんが、表示した内容のうち日本語はすべて[?]と文字化けしてしまいます。 ちゃんとディレクティブで「charset=Shift_JIS」と指定しているのになぞです。 どうか教えてください。よろしくお願いします。

    • ベストアンサー
    • Java
  • データベースへの接続で困ってます

    JSPを関連させてcom.mysql.jdbc.Driverというドライバでデータベースに接続したいのですが、エラーが出てしまってどうすればいいかお手上げ状態になっています。プログラムを貼るので、何かおかしいとこがありましたら教えてもらいたいです。よろしくお願いします。 /*プログラム*/ <%@ page import="java.sql.*" %> <html> <head> <title>データを表示する</title> </head> <body> <% //データベースに接続する Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/sqldb?useUnicode=true&characterEncoding=SJIS"; String user = "matsuzaki"; String password = "matsuzaki"; Connection conn = DriverManager.getConnection(url, user, password); //データを取得する Statement st = conn.createStatement(); String sql = "SELECT * FROM cd"; //実行するSQL ResultSet rs = st.executeQuery(sql); //データを全件表示する out.println("<table border=1>"); while(rs.next()){ out.println("<tr>"); out.println("<td>" + rs.getInt("name") + "</td>"); out.println("<td>" + rs.getString("artist") + "</td>"); out.println("<td>" + rs.getInt("price") + "</td>"); out.println("</tr>"); } out.println("</table>"); //データベース接続を閉じる conn.close(); %> </body> </html>

    • ベストアンサー
    • MySQL
  • Javaによる現在時刻の表示について

    Javaで現在時刻を表示するプログラムを作りました。 今のままだと 2009/8/26(木) 15:8:7となり、 月日・時間分秒が一桁のときは前に0をつけて二桁(15:08:07のように)表示にしたいと思います。 何かいい方法はないでしょうか? if文で作成するとなると、かなりたくさんのコードを書かないといけない気がします。 良い案があれば是非ご教授頂ければと思います。 import java.util.Calendar; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Servlet_106 extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ res.setContentType("text/html; charset=Shift_JIS"); PrintWriter out = res.getWriter(); String title = "現在時刻:"; Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int day = cal.get(Calendar.DATE); StringBuffer dow = new StringBuffer(); switch (cal.get(Calendar.DAY_OF_WEEK)){ case Calendar.SUNDAY: dow.append("日"); break; ~~~(略)~~~ } int hour = cal.get(Calendar.HOUR_OF_DAY); int minute = cal.get(Calendar.MINUTE); int second = cal.get(Calendar.SECOND); out.println("<html lang=\"ja\">"); out.println("<head><title></title></head>"); out.println("<body>"); out.println("<p>現在時刻:" + year + "/" + month + "/" + day + "(" + dow + ")" + hour + ":" + minute + ":" + second + "</p>"); out.println("</body></html>"); out.close(); } }

    • ベストアンサー
    • Java
  • Javaと言うより、for文の長さの問題なのですが……

    <%@ page language="java" contentType="text/html; charset=Shift_JIS" import="java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> <title>カレンダー</title> </head> <body> <% //カレンダーオブジェクトを生成する Calendar myCal = Calendar.getInstance(); //年月日を取得する int year = myCal.get(Calendar.YEAR); int month = myCal.get(Calendar.MONTH) + 1; if (request.getMethod().equals("POST")) { year = Integer.parseInt(request.getParameter("yyyy")); month = Integer.parseInt(request.getParameter("mm")); } //年月選択リストを表示する out.println("<form method='POST' action='Calendar2.jsp'>"); out.println("<table><tr><td>"); //年 out.println("<select name='yyyy'>"); for (int i = 2000; i <= 2020; i++) { out.println("<option"); if (i == year) { out.println(" selected "); } out.println(">" + i); } out.println("</select>年"); //月 out.println("<select name='mm'>"); for (int i = 1; i <= 12; i++) { out.println("<option"); if (i == month) { out.println(" selected "); } out.println(">" + i); } out.println("</select>月"); out.println("</td>"); out.println("<td><input type='submit' value='更新'></td>"); out.println("</tr></table></form>"); //カレンダーを表示する out.println("<table border='1'>"); out.println("<tr>"); out.println("<td>日</td>"); out.println("<td>月</td>"); out.println("<td>火</td>"); out.println("<td>水</td>"); out.println("<td>木</td>"); out.println("<td>金</td>"); out.println("<td>土</td>"); out.println("</tr>"); //1日の曜日を取得する myCal.set(year, month - 1, 1); int dw1 = myCal.get(Calendar.DAY_OF_WEEK); //末日を取得する int lastd = myCal.getActualMaximum(Calendar.DAY_OF_MONTH); //表示する日付を初期化する int day = 0; //1日から末日まで日付を表示する 添付したcalendar.jspなのですが、ずばりココ↓の部分なのですが、 for (int i=0; i<=5; i++) { ここで最大は5以下になっているのですが、そうすると1列多くならないでしょうか。 これだと行数が6つになりますよね。 曜日のセルはその前に作っているので、曜日だけが入るのですから5行でいいと思うのですが、自分の考えは間違っているのでしょうか。 宜しくお願い致します。 for (int i = 0; i <= 5; i++) { if (day >= lastd) {break;} out.println("<tr>"); for (int j = 0; j <= 6; j++) { day = i * 7 + j - dw1 + 2; if (day > lastd || day < 1) { out.println("<td> </td>"); } else { out.println("<td>" + day + "</td>"); } } out.println("</tr>"); } out.println("</table>"); %> </body> </html>

    • ベストアンサー
    • Java
  • Javaと言うより、for文の長さの問題なのですが……

    <%@ page language=\"java\" contentType=\"text/html; charset=Shift_JIS\" import=\"java.util.*\"%> <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> <html> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\"> <title>カレンダー</title> </head> <body> <% //カレンダーオブジェクトを生成する Calendar myCal = Calendar.getInstance(); //年月日を取得する int year = myCal.get(Calendar.YEAR); int month = myCal.get(Calendar.MONTH) + 1; if (request.getMethod().equals(\"POST\")) { year = Integer.parseInt(request.getParameter(\"yyyy\")); month = Integer.parseInt(request.getParameter(\"mm\")); } //年月選択リストを表示する out.println(\"<form method=\'POST\' action=\'Calendar2.jsp\'>\"); out.println(\"<table><tr><td>\"); //年 out.println(\"<select name=\'yyyy\'>\"); for (int i = 2000; i <= 2020; i++) { out.println(\"<option\"); if (i == year) { out.println(\" selected \"); } out.println(\">\" + i); } out.println(\"</select>年\"); //月 out.println(\"<select name=\'mm\'>\"); for (int i = 1; i <= 12; i++) { out.println(\"<option\"); if (i == month) { out.println(\" selected \"); } out.println(\">\" + i); } out.println(\"</select>月\"); out.println(\"</td>\"); out.println(\"<td><input type=\'submit\' value=\'更新\'></td>\"); out.println(\"</tr></table></form>\"); //カレンダーを表示する out.println(\"<table border=\'1\'>\"); out.println(\"<tr>\"); out.println(\"<td>日</td>\"); out.println(\"<td>月</td>\"); out.println(\"<td>火</td>\"); out.println(\"<td>水</td>\"); out.println(\"<td>木</td>\"); out.println(\"<td>金</td>\"); out.println(\"<td>土</td>\"); out.println(\"</tr>\"); //1日の曜日を取得する myCal.set(year, month - 1, 1); int dw1 = myCal.get(Calendar.DAY_OF_WEEK); //末日を取得する int lastd = myCal.getActualMaximum(Calendar.DAY_OF_MONTH); //表示する日付を初期化する int day = 0; //1日から末日まで日付を表示する 添付したcalendar.jspなのですが、ずばりココ↓の部分なのですが、 for (int i=0; i<=5; i++) { ここで最大は5以下になっているのですが、そうすると1列多くならないでしょうか。 これだと行数が6つになりますよね。 曜日のセルはその前に作っているので、曜日だけが入るのですから5行でいいと思うのですが、自分の考えは間違っているのでしょうか。 宜しくお願い致します。 for (int i = 0; i <= 5; i++) { if (day >= lastd) {break;} out.println(\"<tr>\"); for (int j = 0; j <= 6; j++) { day = i * 7 + j - dw1 + 2; if (day > lastd || day < 1) { out.println(\"<td> </td>\"); } else { out.println(\"<td>\" + day + \"</td>\"); } } out.println(\"</tr>\"); } out.println(\"</table>\"); %> </body> </html>

    • ベストアンサー
    • Java
  • java.util.Dateについて

    まずは下記ソースを参照してください。 import java.util.Date; import java.util.GregorianCalendar; import java.util.Calendar; public class Test{     public static void main( String args[] ){         GregorianCalendar cal = new GregorianCalendar(2004, 5, 31);         System.out.println("年:" + cal.get(Calendar.YEAR));         System.out.println("月:" + cal.get(Calendar.MONTH));         System.out.println("日:" + cal.get(Calendar.DAY_OF_MONTH));         System.out.println(); // ただの改行         System.out.println("比較:" + new Date().compareTo(cal.getTime()));     } } 上記ソースについて質問です。 1.「年」「月」「日」を表示しておりますが、本来ならば 「年:2004」 「月:5」 「日:31」 と表示されると思っておりましたが実際は、 「年:2004」 「月:6」 「日:1」 と表示されてしまいます。 なぜでしょうか? 2.12行目にてjava.util.DateクラスのcompareToメソッドを用いて、 現在日付(2004/5/28)と7行目で指定している日付の比較をしていますが、 7行目で設定している日付を現在日付よりも前後に設定して実行させても、 「-1」しか返りません。 なぜでしょうか? 環境は、 J2SDK:1.4.2 OS:Windows XP Pro です。

    • ベストアンサー
    • Java
  • java Calendarクラス

    javaで月、日を入力してカレンダーを作成したのですが 年と月のsetでmonth-1はマジックナンバーなので直したいのですが どなたかわかる方教えてください。 package sample; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Test { private final static int firstday = 1; public static void main(String[] args) { //カレンダーのインスタンスを取得します Calendar cal = Calendar.getInstance(); //文字入力 BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); //年を取得 int year =0; //月を取得 int month =0; //最後の日付 int lastDay =0; //月初めの曜日を取得 int week =0; //年妥当性チェック boolean CheckYear = true; //月妥当性チェック boolean CheckMonth = true; try { //年妥当性チェック while(CheckYear){ System.out.println("年を入力してください"); //年を入力します year = Integer.parseInt(input.readLine()); //年が4桁の場合 if(String.valueOf(year).length()==4){ CheckYear = false; }else{ System.out.println("年は4桁で入力してください"); } } //月妥当性チェック while(CheckMonth){ System.out.println("月を入力してください"); //月を入力します month = Integer.parseInt(input.readLine()); //月が1~12の場合 if(month>=1&&month<=12){ CheckMonth = false; }else{ System.out.println("月1~12を入力してください"); } } }catch(IOException e){ System.out.println("数字以外は入力しないでください"); System.out.println("処理を中断します"); return; }catch (Exception a) { System.out.println("数字以外は入力しないでください"); System.out.println("処理を中断します"); return; } //年、月をセットします cal.set(year,month-1,cal.getActualMinimum(Calendar.DATE)); //月初めの曜日を取得 week = cal.get(Calendar.DAY_OF_WEEK); //年月を出力する System.out.println(String.valueOf(year)+"年"+String.valueOf(month)+"月"); //曜日を出力する System.out.println("日 月  火  水  木  金  土"); //最後の日付を取得する lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); /* * 最後の日付を取得する */ if(month==1||month==3||month==7||month==8||month==10||month==12) { lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); }else if(month==4||month==6||month==9||month==11){ lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); }else if(year%4==0&&month==2){ lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); }else if(year%4!=0&&month==2){ lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); } /* * カレンダーを出力する */ //最後の日付まで繰り返す // 最後の日付まで繰り返す for (int i = 1; i <= lastDay; i++) { // 1日とそれ以外で分岐する if (i == 1) { // 1日の曜日位置まで移動する for (int j = 1; j < cal.get(Calendar.DAY_OF_WEEK); j++) { System.out.print(" "); } } else { // 日付を増やす cal.add(Calendar.DAY_OF_MONTH, firstday); } // 1~9と10~で表示を変える if (i < 10) { System.out.print(" " + i); } else { System.out.print(" " + i); } // 土曜日になったら改行する if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { System.out.println(""); } } } }

  • JAVAについて

    現在JAVAについて学んでいるのですが、下記のソースが実行出来ません。どなたかわかる方、回答宜しくお願いいたします。 public class Lound { void main(String[] args) { String rtn = methWithParamReturn(3); if(rtn.equals("0")) System.out.println("0あかんよ"); else System.out.println(rtn); } public String methWithParamReturn(int x) { System.out.println(x); x= x*x; String str = String.valueOf(x); return str; } }

  • eclipse,mysql,javaエラー

    Class.forName("com.mysql.jdbc.Driver")が接続出来ません どなたかお教え頂けないでしょうか MySQLを再インストールしてもだめです 下記環境で動かしています。 eclipse-3.72 MySQL5.5.13,JDBC:4.0.0 mysql-connector-java5.1.17-bin.jar jdk.1.6.0_26 eclipseで 接続プロファイルを作成し、mysql-connector-java5.1.17-bin.jarを設定して 接続デスト pingが正常に完了しましたで問題がありません。 またjavaのビルド・パスにもmysql-connector-java5.1.17-bin.jarが有りますが javaを実行すると下記状態になります Class.forName("com.mysql.jdbc.Driver")が接続出来ません 下記が実行ソースプログラム import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class test01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ response.setContentType("text/html; charset=Shift_JIS"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>データベーステスト</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>"); Connection conn = null; String url = "jdbc:mysql://localhost/testdb"; String user = "root"; String password = "test"; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); out.println("ドライバのロードに成功しました<br>"); conn = DriverManager.getConnection(url, user, password); out.println("データベース接続に成功しました<br>"); }catch (ClassNotFoundException e){ out.println("ClassNotFoundException:" + e.getMessage()); }catch (SQLException e){ out.println("SQLException:" + e.getMessage()); }catch (Exception e){ out.println("Exception:" + e.getMessage()); }finally{ try{ if (conn != null){ conn.close(); out.println("データベース切断に成功しました"); }else{ out.println("コネクションがありません"); } }catch (SQLException e){ out.println("SQLException:" + e.getMessage()); } } out.println("</p>"); out.println("</body>"); out.println("</html>"); } }

    • ベストアンサー
    • Java
  • 以下のJAVAプログラムでご質問です。

    import java.util.*; import java.io.*; import java.text.*; public class JTest9 { public static void main(String[] args) throws IOException { System.out.println("基準となる年月日を入力して下さい。"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str1 = br.readLine(); String str2 = br.readLine(); String str3 = br.readLine(); int year = Integer.parseInt(str1); int month = Integer.parseInt(str2); int day = Integer.parseInt(str3); String targetDateText = year + "/" + month + "/" + day; int addDate = 10; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); Date targetDate = sdf.parse(targetDateText); Calendar cal = Calendar.getInstance(); cal.setTime(targetDate); cal.add(Calendar.DATE,addDate); System.out.println(sdf.format(cal.getTime())); } } 上記は、コンソールから、年、月、日 を入力し、その入力した日付から10日後の日付を 出力しようとしているプログラムです。 しかし、17行目のsdf.parse(targetDateText);において、コンソール上に下記エラーが出ます。 Exception in thread "main" java.lang.Error: Unresolved compilation problem: 処理されない例外の型 ParseException このエラーを解決したいのですが、どのように修正すればよいのでしょうか。 ご助言の程、よろしくお願い致します。

    • ベストアンサー
    • Java