import java.util.*;
import java.sql.*;
/*
* クラス:消費税テーブル
*
*/
public class SHOHIZEI_TBL {
/*
* 検索<BR>
* @param HashMap taisho 検索対象、NULLのとき、*(全部)
* @param Vector where 条件、NULLのとき、無条件、
* @return Vector 結果(1レコード1ハッシュマップで入っている)
*/
public Vector select(HashMap map,Vector where)
{
StringBuffer sqlbuf = new StringBuffer("SELECT ");
if ( map == null )
{
sqlbuf.append("*");
}
else
{
String[] taisho =(String[])map.keySet().toArray(new String[0]);
for(int i = 0 ; i < taisho.length ; i ++ )
{
if ( i > 0 )
{
sqlbuf.append(",");
}
sqlbuf.append(taisho[i]);
}
}
sqlbuf.append(" FROM SHOHIZEI_TBL ");
String wstr = makeWhere(where);
if( ( wstr != null ) && (wstr.trim().length() > 0 ) )
{
sqlbuf.append(" WHERE " + wstr);
}
return selectExec(sqlbuf.toString());
}
/*
* 追加<BR>
* @param HashMap taisho 対象、NULLのときエラー
* @return int 0:OK,1:Error
*/
public int insert(HashMap taisho)
{
String SHOHI_ID =(String)taisho.get("SHOHI_ID");
String ZEIRITU =(String)taisho.get("ZEIRITU");
String ZEIKUBUN =(String)taisho.get("ZEIKUBUN");
int out_flg;
// SQL文の書き出し
StringBuffer sqlbuf = new StringBuffer("INSERT INTO SHOHIZEI_TBL(");
//==========================//
// 項目名の書き出し
//==========================//
out_flg = 0;
if ( SHOHI_ID != null )
{
if ( out_flg == 0 )
{
out_flg = 1;
}
else
{
sqlbuf.append(",");
}
sqlbuf.append("SHOHI_ID");
}
if ( ZEIRITU != null )
{
if ( out_flg == 0 )
{
out_flg = 1;
}
else
{
sqlbuf.append(",");
}
sqlbuf.append("ZEIRITU");
}
if ( ZEIKUBUN != null )
{
if ( out_flg == 0 )
{
out_flg = 1;
}
else
{
sqlbuf.append(",");
}
sqlbuf.append("ZEIKUBUN");
}
// VALUES句
sqlbuf.append(") VALUES (");
//==========================//
// データの書き出し
//==========================//
out_flg = 0;
if ( SHOHI_ID != null )
{
if ( out_flg == 0 )
{
out_flg = 1;
}
else
{
sqlbuf.append(",");
}
sqlbuf.append("");
sqlbuf.append(SHOHI_ID);
sqlbuf.append("");
}
if ( ZEIRITU != null )
{
if ( out_flg == 0 )
{
out_flg = 1;
}
else
{
sqlbuf.append(",");
}
sqlbuf.append("");
sqlbuf.append(ZEIRITU);
sqlbuf.append("");
}
if ( ZEIKUBUN != null )
{
if ( out_flg == 0 )
{
out_flg = 1;
}
else
{
sqlbuf.append(",");
}
sqlbuf.append("'");
sqlbuf.append(ZEIKUBUN);
sqlbuf.append("'");
}
// おしまい部分
sqlbuf.append(")");
// 実行
MyConnection con = (MyConnection)taisho.get("MyConnection");
return exec(sqlbuf.toString(),con);
}
/*
* 削除<BR>
* @param HashMap taisho 対象、NULLのとき、全削除
* @return int 0:OK,1:Error
*/
public int del(HashMap taisho)
{
// SQL文の書き出し
StringBuffer sqlbuf = new StringBuffer("DELETE FROM SHOHIZEI_TBL");
if ( taisho != null )
{
String wrec = whereOneRec(taisho);
if ( ( wrec != null ) && (wrec.trim().length() > 0 ) )
{
sqlbuf.append(" WHERE ");
sqlbuf.append(wrec);
}
}
MyConnection con = (MyConnection)taisho.get("MyConnection");
return exec(sqlbuf.toString(),con);
}
/*
* 更新<BR>
* @param HashMap taisho 対象、NULLのとき、全削除
* @return int 0:OK,1:Error
*/
public int update(HashMap taisho,Vector where)
{
// データチェック
if ( taisho == null )
{
return -1;
}
if ( taisho.size() <= 0 )
{
return -1;
}
// SQL文の書き出し
StringBuffer sqlbuf = new StringBuffer("UPDATE SHOHIZEI_TBL SET ");
String rec = setOneRec(taisho);
if ( rec == null )
{
return -1;
}
sqlbuf.append(rec);
String wstr = makeWhere(where);
if( ( wstr != null ) && (wstr.trim().length() > 0 ) )
{
sqlbuf.append(" WHERE " + wstr);
}
MyConnection con = (MyConnection)taisho.get("MyConnection");
return exec(sqlbuf.toString(),con);
}
/*
* SQL実行<BR>
* @param HashMap taisho 対象、NULLのとき、全削除
* @return int 0:OK,1:Error
*/
public int exec(String cmd,MyConnection oldcon)
{
try
{
// コネクションがない場合:トランザクション処理していない
if ( oldcon == null )
{
Class.forName(MyConnection.MyDriverName);
Connection con =
DriverManager.getConnection(MyConnection.MyURL,
MyConnection.MyUserName,MyConnection.MyPassword);
Statement stmt = con.createStatement();
stmt.execute(cmd);
stmt.close();
con.close();
}
else
{
oldcon.stmt.addBatch(cmd);
}
}
catch (Exception e)
{
e.printStackTrace();
return -1;
}
return 0;
}
/*
* SQLの検索実行<BR>
* @param HashMap taisho 対象、NULLのとき、全削除
* @return Vector 結果(1レコード1ハッシュマップで入っている)
*/
public Vector selectExec(String cmd)
{
Vector allData = new Vector();
try
{
Class.forName(MyConnection.MyDriverName);
Connection con =
DriverManager.getConnection(MyConnection.MyURL,
MyConnection.MyUserName,MyConnection.MyPassword);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(cmd);
ResultSetMetaData meta = rs.getMetaData();
while(rs.next())
{
HashMap mp = new HashMap();
for(int i = 1 ; i <= meta.getColumnCount(); i ++ )
{
mp.put(meta.getColumnName(i),
rs.getString(meta.getColumnName(i)) );
}
allData.add(mp);
}
stmt.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
return allData;
}
/*
* where句を作る
* @param Vector where
* @return String 結果(1レコード1ハッシュマップで入っている)
*/
public String makeWhere(Vector where)
{
StringBuffer buf = new StringBuffer();
if ( where == null )
{
return null;
}
else if ( where.size() == 0 )
{
return null;
}
else
{
if ( where.size() == 1 )
{
buf.append(whereOneRec((HashMap)where.elementAt(0)));
}
else
{
for(int i = 0 ; i < where.size() ; i ++ )
{
if ( i == 0 )
{
buf.append("( ");
}
else
{
buf.append("OR ( ");
}
buf.append(whereOneRec((HashMap)where.elementAt(i)));
buf.append(" ) ");
}
}
}
return buf.toString();
}
/*
* where句の検索条件のAND1個分を作る
* @param HashMap taisho
* @return String 結果(1レコード1ハッシュマップで入っている)
*/
public String whereOneRec(HashMap taisho)
{
return makeOneRec(taisho," AND ");
}
/*
* set句の更新条件を作る
* @param HashMap taisho
* @return String 結果(1レコード1ハッシュマップで入っている)
*/
public String setOneRec(HashMap taisho)
{
return makeOneRec(taisho," , ");
}
/*
* where句の検索条件のAND1個分を作る
* @param HashMap taisho
* @return String 結果(1レコード1ハッシュマップで入っている)
*/
public String makeOneRec(HashMap taisho,String kugiri)
{
StringBuffer buf = new StringBuffer();
String SHOHI_ID = (String)taisho.get("SHOHI_ID");
String ZEIRITU = (String)taisho.get("ZEIRITU");
String ZEIKUBUN = (String)taisho.get("ZEIKUBUN");
int out_flg = 0;
if ( SHOHI_ID != null )
{
if ( out_flg == 0 )
{
out_flg = 1;
}
else
{
buf.append(kugiri);
}
buf.append("SHOHI_ID ");
char c = 0;
if ( SHOHI_ID.length() > 0 )
{
c = SHOHI_ID.charAt(0);
}
switch(c)
{
case '<':
case '>':
case '=':
buf.append(SHOHI_ID);
break;
case '"':
buf.append("=");
buf.append(SHOHI_ID);
break;
default:
buf.append("= ");
buf.append("");
buf.append(SHOHI_ID);
buf.append("");
break;
}
}
if ( ZEIRITU != null )
{
if ( out_flg == 0 )
{
out_flg = 1;
}
else
{
buf.append(kugiri);
}
buf.append("ZEIRITU ");
char c = 0;
if ( ZEIRITU.length() > 0 )
{
c = ZEIRITU.charAt(0);
}
switch(c)
{
case '<':
case '>':
case '=':
buf.append(ZEIRITU);
break;
case '"':
buf.append("=");
buf.append(ZEIRITU);
break;
default:
buf.append("= ");
buf.append("");
buf.append(ZEIRITU);
buf.append("");
break;
}
}
if ( ZEIKUBUN != null )
{
if ( out_flg == 0 )
{
out_flg = 1;
}
else
{
buf.append(kugiri);
}
buf.append("ZEIKUBUN ");
char c = 0;
if ( ZEIKUBUN.length() > 0 )
{
c = ZEIKUBUN.charAt(0);
}
switch(c)
{
case '<':
case '>':
case '=':
buf.append(ZEIKUBUN);
break;
case '"':
buf.append("=");
buf.append(ZEIKUBUN);
break;
default:
buf.append("= ");
buf.append("'");
buf.append(ZEIKUBUN);
buf.append("'");
break;
}
}
return buf.toString();
}
}
|