パッケージ jp.co.sds_corp.util.db
クラス DbAccess
- java.lang.Object
-
- jp.co.sds_corp.util.db.DbAccess
-
public class DbAccess extends Object
DBアクセス共通クラス。
接続先の指定方法は、以下の3通りが可能です。1.Propertiesファイルから取得
DbAccess(ResourceBundle),DbAccess(String)を参照してください。propertiesファイルは以下の形式です。 JNDIから取得する場合 DB_JNDI_URL=ap1.sds-corp.co.jp DB_SOURCE=jdbc/vfx2DataSource DB_USER=user DB_PASSWORD=password DB_ENCRYPTED=true DB_ENCODE=EUC-JP JDBCを直接使用する場合 DB_URL=jdbc:oracle:oci8:@DB DB_DRIVER=oracle.jdbc.OracleDriver DB_USER=user DB_PASSWORD=password DB_ENCRYPTED=true DB_ENCODE=EUC-JP DB_JAR=../lib/ojdbc14.jar-JP また、キーをつけての指定も可能です。 dbkey.DB_CONTEXT= dbkey.DB_JNDI_URL= dbkey.DB_SOURCE= dbkey.DB_URL=jdbc:oracle:oci8:@DB dbkey.DB_DRIVER=oracle.jdbc.OracleDriver dbkey.DB_USER=user dbkey.DB_PASSWORD=password dbkey.DB_ENCRYPTED=true dbkey.DB_ENCODE=EUC-JP dbkey.DB_JAR=../lib/ojdbc14.jar-JP
2.XMLから取得
DbAccess(IConfiguration)を参照してください。XMLは以下の形式です。 dbキーを指定する場合 <db type="properties"> <key>db_key</key> </db> JNDIを指定使用する場合 <db type="jndi"> <context>org.jnp.interfaces.NamingContextFactory</context> <url>localhost</url> <datasource>java:/DefaultDS</datasource> <user>user</user> <password>password</password> <encrypted>true</encrypted> <encode>MS932</encode> </db> JDBCドライバを直接指定する場合 <db type="jdbc"> <driver>com.sybase.jdbc2.jdbc.SybDriver</driver> <url>jdbc:sybase:Tds:localhost:4100/db</url> <user>user</user> <password>password</password> <encrypted>true</encrypted> <encode>MS932</encode> <jar>./lib/ojdbc14.jar</jar> <properties key="some" value="props"/> </db>3.自分で
DbInfoクラスを構築
DbAccess(DbInfo)を参照してください。- 作成者:
- 小見山 聡
-
-
コンストラクタの概要
コンストラクタ コンストラクタ 説明 DbAccess()コンストラクタ デフォルトのDBの接続情報を取得するDbAccess(String key)コンストラクタ 指定されたキーによりDBの接続情報を取得するDbAccess(ResourceBundle prop)指定されたpropertiesファイルで初期化します。DbAccess(IConfiguration config)指定されたXMLで初期化します。DbAccess(DbInfo inf)指定された情報で初期化します。
-
メソッドの概要
すべてのメソッド staticメソッド インスタンス・メソッド concreteメソッド 修飾子とタイプ メソッド 説明 voidcancel()SQLの取り消しvoidclearWarnings()SQL Warningをクリアします。voidcommit()データベーストランザクションのコミットvoidconnect()DBの接続情報により、DBにコネクトするvoidconnect(Driver drv)ドライバを指定してDBサーバと接続します。voidconnect(Driver drv, Properties prop)ドライバを指定してDBサーバと接続します。voidconnect(Properties prop)DBの接続情報により、DBにコネクトするintcountRow(String tbl, String where)row数の取得booleandelete(DbTable tbl)テーブルから、キーで指定されたレコードを削除しますvoiddisconnect()データベース接続の終了booleanexecute(String sql)更新、挿入の実行booleanexecutePrepared()準備された問い合わせの実行voidexecutePreparedQuery()準備された問い合わせの実行voidexecuteQuery(String sql)問い合わせの実行booleanfetch(ResultSet rset, DbTable tbl)検索結果の取得。booleanfetch(DbTable tbl)検索結果の取得。ConnectiongetConnection()Connectionの取得DbInfogetDbInfo()booleangetMoreResults()次の結果の取得booleangetParameter()パラメータを取得します。ResultSetgetResultSet()結果セットの取得PreparedStatementgetStatement()Statementの取得PreparedStatementgetStatement(String sql)SQL を実行して結果のStatementを取得します。intgetUpdateCount()更新、挿入の実行booleangetUseAutoCommit()自動Commitを使用するかを取得します。SQLWarninggetWarnings()SQL Warningを取得します。booleaninsert(DbTable tbl)テーブルにオブジェクトの内容をinsertしますbooleaninsertPrepared(DbTable tbl)テーブルにオブジェクトの内容をinsertしますbooleanisSqlOutput()SQL文出力フラグを取得します。booleanisTrim()文字列をtrimするかを取得します。static voidmain(String[] args)voidprepareInsert(DbTable tbl)テーブルにオブジェクトの内容をinsertしますvoidprepareQuery(String sql)問い合わせの準備voidrollback()データベーストランザクションのロールバックvoidselectAll(DbTable tbl, String where)テーブルから全カラムを取得するSQL文を作成し、実行しますbooleanselectAllByKey(DbTable tbl)テーブルから、キーで指定されたレコードの 全カラムを取得するSQL文を実行しますvoidsetAutoCommit(boolean bFlg)自動COMMITの設定voidsetResultSet(ResultSet var)結果セットの設定voidsetSqlOutput(boolean var)SQL文出力フラグを設定します。voidsetTrim(boolean var)文字列をtrimするかを設定します。voidsetUseAutoCommit(boolean var)自動Commitを使用するかを設定します。StringtoString()この出力先の文字列表現を返します。
-
-
-
コンストラクタの詳細
-
DbAccess
public DbAccess()
コンストラクタ デフォルトのDBの接続情報を取得する
-
DbAccess
public DbAccess(ResourceBundle prop)
指定されたpropertiesファイルで初期化します。- パラメータ:
prop- properiesファイル
-
DbAccess
public DbAccess(String key)
コンストラクタ 指定されたキーによりDBの接続情報を取得する- パラメータ:
key- このキーでprpertiesファイルを検索する
-
DbAccess
public DbAccess(DbInfo inf)
指定された情報で初期化します。- パラメータ:
inf- DB接続情報
-
DbAccess
public DbAccess(IConfiguration config) throws Exception
指定されたXMLで初期化します。- パラメータ:
config- DB接続情報- 例外:
Exception
-
-
メソッドの詳細
-
getDbInfo
public DbInfo getDbInfo()
-
connect
public void connect() throws DbExceptionDBの接続情報により、DBにコネクトする- 例外:
DbException
-
connect
public void connect(Properties prop) throws DbException
DBの接続情報により、DBにコネクトする- 例外:
DbException
-
connect
public void connect(Driver drv) throws DbException
ドライバを指定してDBサーバと接続します。- パラメータ:
drv- ドライバ- 例外:
DbException
-
connect
public void connect(Driver drv, Properties prop) throws DbException
ドライバを指定してDBサーバと接続します。- パラメータ:
drv- ドライバprop- 接続プロパティ- 例外:
DbException
-
setAutoCommit
public void setAutoCommit(boolean bFlg) throws DbException自動COMMITの設定- パラメータ:
bFlg- 自動COMMITのon/off- 例外:
DbException
-
setUseAutoCommit
public void setUseAutoCommit(boolean var)
自動Commitを使用するかを設定します。- パラメータ:
var- 設定するデータ
-
getUseAutoCommit
public boolean getUseAutoCommit()
自動Commitを使用するかを取得します。- 戻り値:
- 自動Commitを使用するか
-
setTrim
public void setTrim(boolean var)
文字列をtrimするかを設定します。- パラメータ:
var- 設定するデータ
-
isTrim
public boolean isTrim()
文字列をtrimするかを取得します。- 戻り値:
- 文字列をtrimするか
-
executeQuery
public void executeQuery(String sql) throws DbException
問い合わせの実行- パラメータ:
sql- 問い合わせ内容- 例外:
DbException
-
prepareQuery
public void prepareQuery(String sql) throws DbException
問い合わせの準備- パラメータ:
sql- 問い合わせ内容- 例外:
DbException
-
executePreparedQuery
public void executePreparedQuery() throws DbException準備された問い合わせの実行- 例外:
DbException
-
fetch
public boolean fetch(DbTable tbl) throws DbException
検索結果の取得。 検索結果は、DbColumn変数に設定される- パラメータ:
tbl- 検索結果の格納バッファ- 戻り値:
- 成功:true 失敗:false
- 例外:
DbException
-
fetch
public boolean fetch(ResultSet rset, DbTable tbl) throws DbException
検索結果の取得。 検索結果は、DbColumn変数に設定される- パラメータ:
tbl- 検索結果の格納バッファ- 戻り値:
- 成功:true 失敗:false
- 例外:
DbException
-
executePrepared
public boolean executePrepared() throws DbException準備された問い合わせの実行- 戻り値:
- 最初の結果が ResultSet オブジェクトの場合は true。 更新カウントであるか、または結果がない場合は false
- 例外:
DbException
-
execute
public boolean execute(String sql) throws DbException
更新、挿入の実行- パラメータ:
sql- 引数 update,insert,deleteの文字列- 戻り値:
- 最初の結果が ResultSet オブジェクトの場合は true。 更新カウントであるか、または結果がない場合は false
- 例外:
DbException
-
getMoreResults
public boolean getMoreResults() throws DbException次の結果の取得- 戻り値:
- 最初の結果が ResultSet オブジェクトの場合は true。 更新カウントであるか、または結果がない場合は false
- 例外:
DbException
-
getWarnings
public SQLWarning getWarnings() throws DbException
SQL Warningを取得します。- 戻り値:
- SQL警告
- 例外:
DbException
-
clearWarnings
public void clearWarnings() throws DbExceptionSQL Warningをクリアします。- 例外:
DbException
-
getUpdateCount
public int getUpdateCount() throws DbException更新、挿入の実行- 戻り値:
- 実行対象の行の数
- 例外:
DbException
-
getParameter
public boolean getParameter() throws DbExceptionパラメータを取得します。- 戻り値:
- true:パラメータ有り false:パラメータ無し
- 例外:
DbException
-
cancel
public void cancel() throws DbExceptionSQLの取り消し- 例外:
DbException
-
commit
public void commit() throws DbExceptionデータベーストランザクションのコミット- 例外:
DbException
-
rollback
public void rollback() throws DbExceptionデータベーストランザクションのロールバック- 例外:
DbException
-
disconnect
public void disconnect() throws DbExceptionデータベース接続の終了- 例外:
DbException
-
getConnection
public Connection getConnection()
Connectionの取得- 戻り値:
- Connection
-
getStatement
public PreparedStatement getStatement() throws DbException
Statementの取得- 戻り値:
- PreparedStatement
- 例外:
DbException
-
getStatement
public PreparedStatement getStatement(String sql) throws DbException
SQL を実行して結果のStatementを取得します。- パラメータ:
sql- SQL文- 戻り値:
- 実行結果
- 例外:
DbException
-
getResultSet
public ResultSet getResultSet()
結果セットの取得
-
setResultSet
public void setResultSet(ResultSet var)
結果セットの設定
-
countRow
public int countRow(String tbl, String where) throws DbException
row数の取得- パラメータ:
where- countする条件。nullまたは""ならテーブルの全レコードが対象- 戻り値:
- 0以上:行数 0未満:エラー
- 例外:
DbException
-
selectAll
public void selectAll(DbTable tbl, String where) throws DbException
テーブルから全カラムを取得するSQL文を作成し、実行します- パラメータ:
tbl- テーブル情報where- where/order by/group by句- 例外:
DbException
-
selectAllByKey
public boolean selectAllByKey(DbTable tbl) throws DbException
テーブルから、キーで指定されたレコードの 全カラムを取得するSQL文を実行します- 戻り値:
- true:正常 false:該当データなし
- 例外:
DbException
-
insert
public boolean insert(DbTable tbl) throws DbException
テーブルにオブジェクトの内容をinsertします- パラメータ:
tbl- テーブル情報- 戻り値:
- true:正常 false:該当データなし
- 例外:
DbException
-
prepareInsert
public void prepareInsert(DbTable tbl) throws DbException
テーブルにオブジェクトの内容をinsertします- パラメータ:
tbl- テーブル情報- 例外:
DbException
-
insertPrepared
public boolean insertPrepared(DbTable tbl) throws DbException
テーブルにオブジェクトの内容をinsertします- パラメータ:
tbl- テーブル情報- 戻り値:
- true:正常 false:該当データなし
- 例外:
DbException
-
delete
public boolean delete(DbTable tbl) throws DbException
テーブルから、キーで指定されたレコードを削除します- パラメータ:
tbl- テーブル情報- 戻り値:
- true:正常 false:該当データなし
- 例外:
DbException
-
setSqlOutput
public void setSqlOutput(boolean var)
SQL文出力フラグを設定します。- パラメータ:
var- SQL文出力フラグ
-
isSqlOutput
public boolean isSqlOutput()
SQL文出力フラグを取得します。- 戻り値:
- SQL文出力フラグ
-
main
public static void main(String[] args)
-
-