|
package lightbox;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import java.sql.*;
public class database {
private webapp gbl;
private Connection con;
public Statement stmt;
public ResultSet rset;
// *****************************************************
// 初期化
// *****************************************************
public void Initialize( webapp my ) {
gbl = my;
}
// *****************************************************
// 接続文字列の補助
// *****************************************************
public void ConnectHelp( ) {
String strWork;
String Mess1 = "web.xml よりセットしました";
String Mess2 = "ユーザがセットしています";
String Mess3 = "セットできませんでした";
strWork = gbl.get( "DriverName" );
if ( strWork.equals( "" ) ) {
strWork = gbl.getApplication("DriverName");
if ( strWork == null ) {
gbl.set(
"DriverName", "sun.jdbc.odbc.JdbcOdbcDriver"
);
gbl.set(
"SET_DriverName", "デフォルトをセットしました"
);
}
else {
gbl.set( "DriverName", strWork );
gbl.set( "SET_DriverName", Mess1 );
}
}
else {
gbl.set( "SET_DriverName", Mess2 );
}
strWork = gbl.get( "ConnectionUrl" );
if ( strWork.equals( "" ) ) {
strWork = gbl.getApplication("ConnectionUrl");
if ( strWork == null ) {
gbl.set( "ConnectionUrl", Mess3 );
}
else {
gbl.set( "ConnectionUrl", strWork );
gbl.set( "SET_ConnectionUrl", Mess1 );
}
}
else {
gbl.set( "SET_ConnectionUrl", Mess2 );
}
strWork = gbl.get( "ConnectionDbname" );
if ( strWork.equals( "" ) ) {
strWork = gbl.getApplication("ConnectionDbname");
if ( strWork == null ) {
gbl.set( "ConnectionDbname", Mess3 );
}
else {
gbl.set( "ConnectionDbname", strWork );
gbl.set( "SET_ConnectionDbname", Mess1 );
}
}
else {
gbl.set( "SET_ConnectionDbname", Mess2 );
}
strWork = gbl.get( "ConnectionUser" );
if ( strWork.equals( "" ) ) {
strWork = gbl.getApplication("ConnectionUser");
if ( strWork == null ) {
gbl.set( "ConnectionUser", Mess3 );
}
else {
gbl.set( "ConnectionUser", strWork );
gbl.set( "SET_ConnectionUser", Mess1 );
}
}
else {
gbl.set( "SET_ConnectionUser", Mess2 );
}
strWork = gbl.get( "ConnectionPass" );
if ( strWork.equals( "" ) ) {
strWork = gbl.getApplication("ConnectionPass");
if ( strWork == null ) {
gbl.set( "ConnectionPass", Mess3 );
}
else {
gbl.set( "ConnectionPass", strWork );
gbl.set( "SET_ConnectionPass", Mess1 );
}
}
else {
gbl.set( "SET_ConnectionPass", Mess2 );
}
}
// *****************************************************
// ドライバ文字列セット
// *****************************************************
public void setDriverName( String strName ) {
gbl.put( "DriverName", strName );
}
// *****************************************************
// DB 接続 URL セット
// *****************************************************
public void setConnectionUrl( String strUrl ) {
gbl.put( "ConnectionUrl", strUrl );
}
// *****************************************************
// DB 接続 Dbname セット
// *****************************************************
public void setConnectionDbname( String strUrl ) {
gbl.put( "ConnectionDbname", strUrl );
}
// *****************************************************
// DB 接続 User セット
// *****************************************************
public void setConnectionUser( String strUser) {
gbl.put( "ConnectionUser", strUser );
}
// *****************************************************
// DB 接続 Password セット
// *****************************************************
public void setConnectionPass( String strPass ) {
gbl.put( "ConnectionPass", strPass );
}
// *****************************************************
// DB 接続
// *****************************************************
public boolean Connect( ) throws Exception {
ConnectHelp( );
gbl.put( "ERROR_CONNECT", "OK" );
Class.forName( gbl.get( "DriverName" ) );
try {
con = DriverManager.getConnection(
gbl.get( "ConnectionUrl" ),
gbl.get( "ConnectionUser" ),
gbl.get( "ConnectionPass" )
);
stmt = con.createStatement();
if ( !gbl.get( "ConnectionDbname" ).equals( "" ) ) {
stmt.executeUpdate(
"use " + gbl.get( "ConnectionDbname" )
);
}
return true;
}
catch( SQLException e ) {
gbl.put( "ERROR_CONNECT", e.getMessage() );
return false;
}
}
// *****************************************************
// DB 接続解除
// *****************************************************
public void DisConnect( ) throws Exception {
gbl.put( "ERROR_DISCONNECT", "OK" );
try {
stmt.close();
con.close();
}
catch( SQLException e ) {
gbl.put( "ERROR_DISCONNECT", e.getMessage() );
}
}
// *****************************************************
// レコードセット取得
// *****************************************************
public boolean Query( String Sql ) throws Exception {
gbl.put( "ERROR_QUERY", "OK" );
boolean bRet = true;
try {
rset = stmt.executeQuery ( Sql );
bRet = rset.next();
}
catch( SQLException e ) {
gbl.put( "ERROR_QUERY", e.getMessage() );
bRet = false;
}
return bRet;
}
public boolean Query() throws Exception {
gbl.put( "ERROR_QUERY_NEXT", "OK" );
boolean bRet = true;
try {
bRet = rset.next();
}
catch( SQLException e ) {
gbl.put( "ERROR_QUERY_NEXT", e.getMessage() );
bRet = false;
}
return bRet;
}
// *****************************************************
// 列データ取得
// *****************************************************
public String Fields( String strName ) throws Exception {
gbl.put( "ERROR_FIELDS", "OK" );
String strResult = "";
try {
strResult = rset.getString( strName );
}
catch( SQLException e ) {
gbl.put( "ERROR_FIELDS", e.getMessage() );
}
return strResult;
}
// *****************************************************
// DB 更新処理
// *****************************************************
public int Execute( String Query ) throws Exception {
gbl.put( "ERROR_SQL", "OK" );
int nRet;
try {
nRet = stmt.executeUpdate( Query );
}
catch( SQLException e ) {
gbl.put( "ERROR_SQL", e.getMessage() );
return -1;
}
return nRet;
}
}
| |