前提・実現したいこと
新しいXAMPP7.2.1をインストールしましたが、MySQLがmariaDBになっており、従来のjavaでつくったWebアプリケーションではDBにつながらないようです。
JDBCドライバーをmariaDBのものに変更してみましたが、うまくいきませんでした。加えて、javaの開発環境も4.7にしたので、WTPが標準的になっており、よくわかりません。
システムはEclipse4.2に載っていた「シンプルBlog」というものです。ソースコード(サーブレットとjspは除く)は下に記載しましたが、初心者同然ですので、ソースのどこが悪いか指摘していただけるとありがたいです。
発生している問題・エラーメッセージ
エラーメッセージは表示されませんが、DBの中を表示しません。
エラーメッセージ
該当のソースコード
Java
1 2```Topic.java 3 4package blog; 5 6import java.util.Date; 7 8/** 9 * トピックのビーン 10 */ 11public class Topic { 12 13 /** 連番 */ 14 private int id; 15 /** タイトル */ 16 private String title; 17 /** 内容 */ 18 private String content; 19 /** 投稿日 */ 20 private Date postDate; 21 22 public String toString(){ 23 return super.toString() 24 + ",id=" + id 25 + ",title=" + title 26 + ",content=" + content 27 + ",postDate=" + postDate 28 ; 29 } 30 31 32 // 33 //単純なセッタ、ゲッタ 34 // 35 public int getId() { 36 return id; 37 } 38 public void setId(int id) { 39 this.id = id; 40 } 41 public String getContent() { 42 return content; 43 } 44 public void setContent(String content) { 45 this.content = content; 46 } 47 public Date getPostDate() { 48 return postDate; 49 } 50 public void setPostDate(Date postDate) { 51 this.postDate = postDate; 52 } 53 public String getTitle() { 54 return title; 55 } 56 public void setTitle(String title) { 57 this.title = title; 58 } 59} 60 61ConnectionManager.java 62 63package blog; 64 65import java.sql.*; 66 67/** 68 * �V���v���ȃR�l�N�V�����Ǘ��N���X 69 * 70 * @author Shinji Miyamoto 71 */ 72public class ConnectionManager { 73 74 /** 75 * JDBC�h���C�o�[�̃N���X�� 76 */ 77 final static String DRIVER = "com.mysql.jdbc.Driver"; 78 79 /** 80 * �f�[�^�x�[�X��URL 81 */ 82 final static String URL = "jdbc:mysql://localhost:3306/mydb"; 83 84 85 /** 86 * �f�[�^�x�[�X�̃��[�U 87 */ 88 final static String USER = "root"; 89 90 /** 91 * �f�[�^�x�[�X�̃p�X���[�h 92 */ 93 final static String PASS = ""; 94 95 /** 96 * Conncection���擾���܂��B 97 */ 98 public static Connection getConnection() 99 throws SQLException { 100 101 try { 102 Class.forName(DRIVER); 103 } catch (ClassNotFoundException e) { 104 e.printStackTrace(); 105 throw new IllegalStateException( 106 "fail to class load : " 107 + e.getMessage()); 108 } 109 110 Connection con = DriverManager.getConnection(URL, 111 USER, PASS); 112 113 return con; 114 } 115} 116 117 118BlogController.java 119 120package blog; 121 122import java.sql.Connection; 123import java.sql.ResultSet; 124import java.sql.SQLException; 125import java.sql.Statement; 126import java.util.ArrayList; 127import java.util.List; 128 129/** 130 * Blogを操作するビジネスロジック 131 * 132 * @author Shinji Miyamoto 133 */ 134public class BlogController { 135 136 /** 137 * シングルトンのインスタンス 138 */ 139 private static BlogController controller = new BlogController(); 140 141 /** 142 * このクラスのインスタンスを取得します。 143 * 144 * @return BlogControllerのインスタンス 145 */ 146 public static BlogController getInstance() { 147 return controller; 148 } 149 150 private BlogController() { 151 } 152 153 /** 154 * トピックをポスト(登録)します。 155 * 156 * @param topic 157 * トピック 158 */ 159 public void postTopic(Topic topic) { 160 String sql = "INSERT INTO TOPIC(TITLE, CONTENT)" 161 + " VALUES(" + "'" + topic.getTitle() + "'" 162 + ",'" + topic.getContent() + "'" + ")"; 163 164 Connection con = null; 165 Statement smt = null; 166 try { 167 con = ConnectionManager.getConnection(); 168 smt = con.createStatement(); 169 smt.executeUpdate(sql); 170 } catch (SQLException e) { 171 e.printStackTrace(); 172 } finally { 173 if (smt != null) { 174 try { 175 smt.close(); 176 } catch (Exception ignore) { 177 } 178 } 179 if (con != null) { 180 try { 181 con.close(); 182 } catch (Exception ignore) { 183 } 184 } 185 } 186 } 187 188 /** 189 * 最近の(というか全部の)トピックを取得します。 190 * 191 * @return トピックのリスト 192 */ 193 public List<Topic> getTopics() { 194 195 String sql = "SELECT * FROM TOPIC"; 196 List<Topic> topics = new ArrayList<Topic>(); 197 198 Connection con = null; 199 Statement smt = null; 200 ResultSet rs = null; 201 try { 202 con = ConnectionManager.getConnection(); 203 smt = con.createStatement(); 204 rs = smt.executeQuery(sql); 205 while (rs.next()) { 206 Topic topic = new Topic(); 207 topic.setId(rs.getInt("ID")); 208 topic.setPostDate(rs 209 .getTimestamp("POST_DATE")); 210 topic.setTitle(rs.getString("TITLE")); 211 topic.setContent(rs.getString("CONTENT")); 212 topics.add(topic); 213 } 214 } catch (SQLException e) { 215 e.printStackTrace(); 216 } finally { 217 if (rs != null) { 218 try { 219 rs.close(); 220 } catch (Exception ignore) { 221 } 222 } 223 if (smt != null) { 224 try { 225 smt.close(); 226 } catch (Exception ignore) { 227 } 228 } 229 if (con != null) { 230 try { 231 con.close(); 232 } catch (Exception ignore) { 233 } 234 } 235 } 236 237 return topics; 238 } 239 240 public static void main(String[] args) { 241 BlogController ctrl = BlogController.getInstance(); 242 List<Topic> topics = ctrl.getTopics(); 243 for (int i = 0; i < topics.size(); i++) { 244 System.out.println(topics.get(i)); 245 } 246 247 System.out.println("END"); 248 } 249 250} 251 252### 試したこと 253JDBC指定をmariaDBにしてみましたが反応なし。 254 255### 補足情報(FW/ツールのバージョンなど) 256 257TOMCATのバージョンは9.04です。jdkも9.04です。
回答1件
あなたの回答
tips
プレビュー