#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <time.h>
HENV henv;
HDBC hdbc;
HSTMT hstmt;
int ConnecToDB ( unsigned char *id, unsigned char *pwd, unsigned
char *dsn )
{
RETCODE retcode;
retcode = SQLAllocEnv(&henv);
if(retcode == SQL_SUCCESS) {
retcode = SQLAllocConnect(henv,&hdbc);
if(retcode == SQL_SUCCESS)
{
SQLSetConnectOption(hdbc,SQL_LOGIN_TIMEOUT,5);
retcode =
SQLConnect(hdbc,dsn,SQL_NTS,id,SQL_NTS,pwd,SQL_NTS);
if(retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WITH_INFO)
{
return 1;
}
return 0;
}
return 0;
}
return 0;
}
int DisconnectDB()
{
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
return 1;
}
void get_new_account_num(char *num)
{
char query[1048];
RETCODE retcode;
SQLCHAR szA[1048];
SQLINTEGER cbA;
int NewNum;
if(ConnecToDB("","","bankdsn" ) == 0)
{
printf("db접속 에러");
return;
}
strcpy(query,"select max(num) from account");
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,query,SQL_NTS );
if(retcode == SQL_SUCCESS)
{
SQLFetch(hstmt);
SQLGetData(hstmt,1,SQL_CHAR,&szA,1048,&cbA);
NewNum = atoi(szA);
sprintf(num,"%d",++NewNum);
}
else
{
strcpy(num,"1");
}
DisconnectDB();
}
void get_current_time(char *create)
{
time_t timer;
struct tm *t;
time(&timer);
t= localtime(&timer);
sprintf(create,"%d-%d-%d %d:%d:%d",t -> tm_year + 1900, t ->
tm_mon + 1,t -> tm_mday,t -> tm_hour,t -> tm_min, t->
tm_sec);
}
void account_open(void)
{
char
num[14],create[20],name[20],branch[20],password[5],tel[30];
char query[1048];
RETCODE retcode;
printf("\n>>새로운 계좌를 생성합니다.\n>>");
get_new_account_num(num);
get_current_time(create);
printf("이름: ");
gets(name);
printf("지점: ");
gets(branch);
printf("비밀번호: ");
gets(password);
printf("전화번호: ");
gets(tel);
printf("\n>>새로운 계좌 번호 : %s\n\n",num);
if(ConnecToDB("","","bankdsn" ) == 0)
{
printf("db접속 에러");
return;
}
sprintf(query,"insert into account values
('%s','%s','%s','%s','%s','%s',0)",num,create,name,branch,password,tel);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,query,SQL_NTS );
if( retcode == SQL_SUCCESS ) printf("입력 성공\n\n");
else printf("입력에 실패 하였습니다.\n\n");
DisconnectDB();
}
void account_search(void)
{
char num[13],password[5];
char query[1048];
RETCODE retcode;
SQLCHAR szA[100];
SQLINTEGER cbA;
printf("\n해당 계좌의 다른 정보를 검색합니다.\n");
printf("계좌번호:");
gets(num);
printf("비밀번호: ");
gets(password);
if(ConnecToDB("","","bankdsn" ) == 0)
{
printf("db접속 에러");
return;
}
sprintf(query,"select * from account where num=%s and
password='%s'",num,password);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,query,SQL_NTS );
retcode = SQLFetch(hstmt);
if(retcode != SQL_SUCCESS)
{
printf("해당 계좌 없거나 비밀번호가 일치 하지
않습니다.\n\n");
return;
}
SQLGetData(hstmt,1,SQL_CHAR,&szA,1048,&cbA);
printf("계좌번호:%s\n",szA);
SQLGetData(hstmt,2,SQL_CHAR,&szA,1048,&cbA);
printf("개설일시:%s\n",szA);
SQLGetData(hstmt,3,SQL_CHAR,&szA,1048,&cbA);
printf("Customer Name:%s\n",szA);
SQLGetData(hstmt,4,SQL_CHAR,&szA,1048,&cbA);
printf("지점명:%s\n",szA);
SQLGetData(hstmt,5,SQL_CHAR,&szA,1048,&cbA);
printf("전화번호:%s\n",szA);
SQLGetData(hstmt,6,SQL_CHAR,&szA,1048,&cbA);
printf("잔액:%s\n",szA);
DisconnectDB();
}
void account_close(void)
{
char num[13],password[5];
char query[1048];
RETCODE retcode;
printf("\n>>해당 계좌를 삭제합니다.\n>>");
printf("계좌번호:");
gets(num);
printf("비밀번호: ");
gets(password);
if(ConnecToDB("","","bankdsn" ) == 0)
{
printf("db접속 에러");
return;
}
sprintf(query,"delete * form account where num = '%s' and
password ='%s'",num,password);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,query,SQL_NTS );
if( retcode == SQL_SUCCESS ) printf("삭제 성공\n\n");
else printf("삭제 실패\n\n");
DisconnectDB();
}
void desposit(void)
{
char num[13],password[5],date[20];
char query[1048];
RETCODE retcode;
SQLCHAR szA[100];
SQLINTEGER cbA;
int balance;
int desposit;
printf("\n>>해당 계좌를 입급합니다.\n>>");
printf("계좌번호:");
gets(num);
printf("비밀번호: ");
gets(password);
if(ConnecToDB("","","bankdsn" ) == 0)
{
printf("db접속 에러");
return;
}
sprintf(query,"select * from account where num=%s and
password='%s'",num,password);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,query,SQL_NTS );
retcode = SQLFetch(hstmt);
if(retcode != SQL_SUCCESS)
{
printf("해당 계좌 없거나 비밀번호가 일치 하지
않습니다.\n\n");
DisconnectDB();
return;
}
get_current_time(date);
printf("입급액: ");
gets(balance);
balance=balance+desposit;
sprintf(query,"update account set balance = %d where num =
'%s'",balance,num);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect( hstmt,query,SQL_NTS );
if(retcode != SQL_SUCCESS)
{
printf("질의 구문에 오류가 있습니다.\n\n");
DisconnectDB();
return;
}
sprintf(query,"insert into transaction values
('%s','%s',%d,%d,%d)",num,date,desposit,0,balance);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect( hstmt,query,SQL_NTS );
if( retcode != SQL_SUCCESS)
{
printf("질의구문에 오류가 있습니다.\n");
DisconnectDB();
return;
}
DisconnectDB();
}
void payment(void)
{
char num[13],password[5],date[20];
char query[1048];
RETCODE retcode;
SQLCHAR szA[100];
SQLINTEGER cbA;
int balance;
int desposit;
printf("\n>>해당 계좌에서 출금합니다.\n>>");
printf("계좌번호:");
gets(num);
printf("비밀번호: ");
gets(password);
if(ConnecToDB("","","bankdsn" ) == 0)
{
printf("db접속 에러");
return;
}
get_current_time(date);
sprintf(query,"select * from account where num=%s and
password='%s'",num,password);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,query,SQL_NTS );
retcode = SQLFetch(date);
if(retcode != SQL_SUCCESS)
{
printf("해당 계좌 없거나 비밀번호가 일치 하지
않습니다.\n\n");
DisconnectDB();
return;
}
printf("출금액: ");
gets(balance);
sprintf(query,"update account set balance=%d where
num='%s'",balance,num);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect(hstmt,query,SQL_NTS );
if(retcode !=SQL_SUCCESS)
{
printf("계좌 에 있는 금액보다 큽니다.\n");
DisconnectDB();
return;
}
sprintf(query,"insert into transction
values('%s','%s',%d,%d,%d)",num,date,0,balance);
SQLAllocStmt(hdbc,&hstmt);
retcode = SQLExecDirect( hstmt,query,SQL_NTS );
if(retcode !=SQL_SUCCESS)
{
printf("질의문에 에러가 있습니다.\n");
return;
}
DisconnectDB();
}
void main(void)
{
char ch;
do
{
printf("\n####################################################\n");
printf("\n Bank Beta version #\n");
printf("\n####################################################\n");
printf("1.계좌개설,2.계좌검색,3.계좌해지,4.입급,5.출금,6.종료\n");
ch = getchar();
fflush(stdin);
switch(ch)
{
case '1': account_open();
break;
case '2': account_search();
break;
case '3': account_close();
break;
case '4': desposit();
break;
case '5': payment();
break;
case '6': break;
default : printf("올바른 메뉴가 아닙니다.\n");
}
}while(ch != '6');
}/*메인 프로그램 끝*/
|