連載
Servlet/JSP+MySQLでアプリケーションサーバ その1:快速MySQLでデータベースアプリ!(8)(3/3 ページ)
いよいよWebアプリケーションサーバの大本命Java Servlet/JSPの登場だ。Javaを使えばさまざまな方法でMySQLを操作できるようになる。今回はServlet/JSPの基本となるJavaアプリケーションを作成してみよう。
Appendix
本文で使用したリストです。また、リスト下部のリンクをクリックすることで行番号なしのテキストファイルをダウンロードできます。ファイルはシフトJISになっていますので、漢字コードを適宜変更してご利用ください。
1 import java.net.*;
2 import java.sql.*;
3
4 public class Sample1{
5
6 public static void main(String args[]){
7 String server = "localhost"; //MySQLサーバ
8 String db = "ATMARKIT"; //
9 String user = "test"; //ユーザー名
10 String pass = "test2001"; //パスワード
11 String url = "jdbc:mysql://" + server + "/" + db;
12 Connection con = null;
13 try{
14 //ドライバのロード
15 Class.forName("org.gjt.mm.mysql.Driver");
16
17 //MySQLサーバへの接続
18 con = DriverManager.getConnection(url,user,pass);
19
20 System.out.println("接続成功です!");
21
22 //切断
23 con.close();
24 }catch(SQLException e) {
25 System.err.println("接続失敗です〜\n理由:" + e.toString());
26 }catch(Exception e){
27 e.printStackTrace();
28 }finally{
29 try{
30 //切断
31 con.close();
32 }catch(Exception e){}
33 }
34 }
35
36 }
リスト1 Sample1.java(リスト1のテキストファイル版)
1 import java.net.*;
2 import java.sql.*;
3
4 public class Sample2{
5
6 public static void main(String args[]){
7 String server = "localhost"; //MySQLサーバ
8 String db = "ATMARKIT"; //
9 String user = "test"; //ユーザー名
10 String pass = "test2001"; //パスワード
11 String url = "jdbc:mysql://" + server + "/" + db;
12 Connection con = null;
13 try{
14 //ドライバのロード
15 Class.forName("org.gjt.mm.mysql.Driver");
16
17 //MySQLサーバへの接続
18 con = DriverManager.getConnection(url,user,pass);
19
20 //Statementオブジェクトの生成
21 Statement stmt = con.createStatement();
22 String sql_str = "INSERT INTO list (name,memo) VALUES (\'2nd\',\'2nd trial\')";
23 stmt.executeUpdate(sql_str);
24 System.out.println("レコードの挿入成功です!");
25
26 //切断
27 con.close();
28 stmt.close();
29 }catch(SQLException e) {
30 System.err.println("接続失敗です〜\n理由:" + e.toString());
31 }catch(Exception e){
32 e.printStackTrace();
33 }finally{
34 try{
35 //切断
36 con.close();
37 }catch(Exception e){}
38 }
39 }
40
41 }
リスト2 Sampl2.java(リスト2のテキストファイル版)
1 import java.net.*;
2 import java.sql.*;
3
4 public class Sample3{
5
6 public static void main(String args[]){
7 String server = "localhost"; //MySQLサーバ
8 String db = "ATMARKIT"; //
9 String user = "test"; //ユーザー名
10 String pass = "test2001"; //パスワード
11 String url = "jdbc:mysql://" + server + "/" + db + "?useUnicode=true&characterEncoding=EUC_JP";
12 Connection con = null;
13 try{
14 //ドライバのロード
15 Class.forName("org.gjt.mm.mysql.Driver");
16
17 //MySQLサーバへの接続
18 con = DriverManager.getConnection(url,user,pass);
19
20 //Statementオブジェクトの生成
21 Statement stmt = con.createStatement();
22 //漢字を含んだSQL文
23 String sql_str = "INSERT INTO list (name,memo) VALUES (\'3番目\',\'3番目のテスト\')";
24 stmt.executeUpdate(sql_str);
25 System.out.println("漢字を含んだレコードの挿入成功です!");
26
27 //切断
28 con.close();
29 stmt.close();
30 }catch(SQLException e) {
31 System.err.println("接続失敗です〜\n理由:" + e.toString());
32 }catch(Exception e){
33 e.printStackTrace();
34 }finally{
35 try{
36 //切断
37 con.close();
38 }catch(Exception e){}
39 }
40 }
41 }
リスト3 Sample3.java(リスト3のテキストファイル版)
1 import java.net.*;
2 import java.sql.*;
3
4 public class Sample4{
5
6 public static void main(String args[]){
7 String server = "localhost"; //MySQLサーバ
8 String db = "ATMARKIT"; //
9 String user = "test"; //ユーザー名
10 String pass = "test2001"; //パスワード
11 String url = "jdbc:mysql://" + server + "/" + db + "?useUnicode=true&characterEncoding=EUC_JP";
12 Connection con = null;
13 try{
14 //ドライバのロード
15 Class.forName("org.gjt.mm.mysql.Driver");
16
17 //MySQLサーバへの接続
18 con = DriverManager.getConnection(url,user,pass);
19
20 //Statementオブジェクトの生成
21 Statement stmt = con.createStatement();
22 //SQL文の生成
23 String sql_str = "SELECT * FROM list";
24 //SQL文の実行とResultSetオブジェクトの取得
25 ResultSet rs = stmt.executeQuery(sql_str);
26
27 //検索結果の抽出
28 while(rs.next()){
29 int id = rs.getInt("id");
30 String name = rs.getString("name");
31 String memo = rs.getString("memo");
32
33 System.out.println("-------" + id + "---------");
34 System.out.println("name:" + name);
35 System.out.println("memo:" + memo);
36 }
37
38 //切断
39 rs.close();
40 stmt.close();
41 con.close();
42 }catch(SQLException e) {
43 System.err.println("接続失敗です〜\n理由:" + e.toString());
44 }catch(Exception e){
45 e.printStackTrace();
46 }finally{
47 try{
48 //切断
49 con.close();
50 }catch(Exception e){}
51 }
52 }
53
54 }
リスト4 Sample4.java(リスト4のテキストファイル版)
Copyright © ITmedia, Inc. All Rights Reserved.