サンプル集  >  Java  >  JDBC:Select
JDBC:Select
2005/06/19

JDBCを使ってAccess2000のDBに接続し、テーブルの内容を表示します。

◆環境
OS Windows 2000 Professional
J2SE 1.4.2.05
Database Microsoft Access2000

DBAccess.java
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
84: 
85: 
86: 
87: 
88: 
89: 
90: 
91: 
92: 
93: 
94: 
95: 
96: 
97: 
98: 
99: 
// 2001/12/04 (c) ymlib.com
import java.sql.*;

class DBAccess
{
    public static void main( String args[] )
    {
        Connection  con;     // データベース接続用I/F
        Statement   stmt;    // SQL文発行用I/F
        ResultSet   res;     // SQLの検索結果操作用I/F
        String      sql;     // SQL文用Stringオブジェクト
        String      name;    // 氏名
        String      address; // 住所
        int         age;     // 年齢
        String      job;     // 職業

        try
        {
            // JDBCドライバの読み込み
            Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

            // データベースへ接続
            con
            = DriverManager.getConnection( "jdbc:odbc:JET-Db1"
                                         , "guest""guest" );

            // Statementインターフェースの取得
            stmt = con.createStatement();

            // SQL文の発行と結果の受け取り
            sql = "Select * from Db1";
//            sql = convertUnicodeToSJIS(sql);
            res = stmt.executeQuery( sql );

            // ResultSetからデータ(検索結果)の取り出し
            while ( res.next() )
            {
                name
                = convertSJISToUnicode( res.getBytes( 1 ) );
                address
                = convertSJISToUnicode( res.getBytes( 2 ) );
                age
                = res.getInt( 3 );
                job
                = convertSJISToUnicode( res.getBytes( 4 ) );

                System.out.println( name
                           + "\t" + address
                           + "\t" + age
                           + "\t" + job );
            }

            // データベースのクローズ(コネクションの切断)
            res.close();
            stmt.close();
            con.close();
        }
        catch ( ClassNotFoundException ce )
        {
            ce.printStackTrace();
        }
        catch ( SQLException sqle )
        {
            sqle.printStackTrace();
        }
    }

    public static String convertUnicodeToSJIS( String source )
    {
        String ret = null;

        try
        {
            ret
            = new String( source.getBytes( "SJIS" ), "8859_1" );
        }
        catch ( java.io.UnsupportedEncodingException e )
        {
            e.printStackTrace();
        }

        return ret;
    }

    public static String convertSJISToUnicode( byte[] source )
    {
        String ret = null;
        try
        {
            ret = new String( source, "SJIS" );
        }
        catch ( java.io.UnsupportedEncodingException e )
        {
            e.printStackTrace();
        }

        return ret;
    }
}

◆環境設定
・テーブル


・データ


・JDBCドライバ
[コントロールパネル]-[管理ツール]-[データ ソース (ODBC)]






◆実行結果
>java DBAccess
山田 太郎      岡山    18      学生
青木 花子      群馬    23      フリーター

▲ PageTop  ■ Home


Copyright (C) 2012 ymlib.com