import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class クラス名 extends HttpServlet { public void init() throws ServletException { } //HTTP Get リクエストの処理 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); Connection con = null; String jdbcUrl = "jdbc:mysql://localhost:3306/データベース名?useUnicode=true&characterEncoding=UTF8"; String user = "ユーザ名"; String password = "パスワード"; try{ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(jdbcUrl,user,password); }catch(ClassNotFoundException e){ }catch(SQLException e){ } Statement st = null; ResultSet rs = null; String sql1 = ""; //データ挿入用 String sql2 = ""; //データ更新用 String sql3 = ""; //データ削除用 String sql = ""; //データ表示用 int cnt; String test = ""; try{ st = con.createStatement(); ///// データ挿入 //sql1 = "insert into テーブル名 values('data1','data2','data3')"; //cnt = st.executeUpdate(sql1); ///// データ更新 //sql2 = "update テーブル名 set name='new_data' where name='old_data'"; //cnt = st.executeUpdate(sql2); ///// データ削除 //sql3 = "delete from テーブル名 where name='data'"; //cnt = st.executeUpdate(sql3); sql = "select * from テーブル名"; rs = null; rs = st.executeQuery(sql); while(rs.next()){ test += rs.getString("name") + " / "; test += rs.getString("telno") + " / "; test += rs.getString("email") + "
"; } }catch(Exception e){ System.out.println(e); }finally{ try{ if(rs != null){ rs.close(); } if(st != null){ st.close(); } if(con != null){ con.close(); } }catch(Exception e){ } } out.println("JavaServlet MySQL TEST"); out.println(""); out.println(""); out.println("

DB接続テスト(JavaServlet MySQL版)

"); out.println(test); out.println(""); out.close(); } //HTTP Post リクエストの処理 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }