은행계좌 Mysql 연동 (입금)

2016. 9. 5. 23:39프로그래밍/자바

반응형

public static void depositAcc(){

try {

System.out.println("계좌번호를 입력하세요 : ");

Scanner sc = new Scanner(System.in);

String accNumber = sc.nextLine();

System.out.println("입금할 금액을 입력하세요: ");

int money = sc.nextInt();

Connection con = null;

con = DriverManager.getConnection("jdbc:mysql://localhost","root","mysql");

java.sql.Statement st = null;

ResultSet rs = null;

st = con.createStatement();

rs = st.executeQuery("use dbname");

String sql;

sql="SELECT acc_money, user_no from user_info where acc_number ='" + accNumber +"'";

rs = st.executeQuery(sql);

int acc_money = 0;

int user_no = 0;

while(rs.next()){

acc_money = rs.getInt("acc_money");

user_no = rs.getInt("user_no");

}

acc_money = money + acc_money;

sql="UPDATE user_info SET acc_money= ? where user_no = ?";

String sql2;

sql2="INSERT into inout_history(acc_number, acc_in, user_no) "

+ "values(?,?,?)";

java.sql.PreparedStatement pstmt = null;

pstmt = con.prepareStatement(sql);

pstmt.setInt(1, acc_money);

        pstmt.setInt(2, user_no);

        pstmt.executeUpdate();

   

        pstmt = con.prepareStatement(sql2);

pstmt.setString(1,accNumber);

pstmt.setInt(2, money);

pstmt.setInt(3, user_no);

pstmt.executeUpdate();

   

con.close();

System.out.println("계좌가 성공적으로 입금 되었습니다");


} catch (SQLException e) {

System.out.println(e.getMessage());

System.out.println("시스템에 오류가 생겼습니다.");

}

}


반응형