자바 mysql 연동해서 출력값구하기 질문

자바 mysql 연동해서 출력값구하기 질문

QA

자바 mysql 연동해서 출력값구하기 질문

본문

mysql> select sum(quantity*price) from orders inner join menus
    -> on orders.m_number = menus.number
    -> where statement=1;
+---------------------+
| sum(quantity*price) |
+---------------------+
|               73000 |
+---------------------+

 

mysql 이용해서 이런식으로 값을 구한후  sql값을 가져오려면 어떻게해야하죠??

string total;을 만들어서 여기에 값을 넣는다던지 하는법이요..

방법만 알려주셔도 너무 좋습니다( _  _ )

이 질문에 댓글 쓰기 :

답변 1

 

 

커멘드창에서 실행해서 데이터 가져 오는 소스 일부분 인데요.

도움 되실지 모르겠네요.

 

완전한 소스는 아니고 검색을 해서 보완을 해야 되는 소스

입니다.

 

sql 은 쿼리 들어가는 변수고

쿼리는 sum(quantity*price) as rst  --> 얼라이언스? 인가 주시고...

 

 

        try { // 1. JDBC 드라이버 로딩 
          Class.forName(driver); // 2. Connection 객체 생성 
          con = DriverManager.getConnection(url, user, pw); // DB 연결 // 3. Statement 객체 생성 
          stmt = con.createStatement(); // 4. SQL 문장을 실행하고 결과를 리턴  
          // stmt.excuteQuery(SQL) : select // stmt.excuteUpdate(SQL) : insert, update, delete .. 
          rs = stmt.executeQuery(SQL); // 5. ResultSet에 저장된 데이터 얻기 - 결과가 2개 이상 
          while (rs.next()) { 
              String id = rs.getInt("rst"); 
          } //5. ResultSet에 저장된 데이터 얻기 - 결과가 1개 
              // if(rs.next()) { // // } // else { // // } 
        } catch (SQLException e) { System.out.println("SQL Error : " + e.getMessage()); 
        } catch (ClassNotFoundException e1) { System.out.println("[JDBC Connector Driver 오류 : " + e1.getMessage() + "]"); 
        } finally { //사용순서와 반대로 close 함 
          if (rs != null) {
              try { rs.close(); } catch (SQLException e) { e.printStackTrace(); }
          }
          if (stmt != null) {
              try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
          }
          if (con != null) { 
              try { con.close(); } catch (SQLException e) { e.printStackTrace(); } 
          }
        }

 

 

답변을 작성하시기 전에 로그인 해주세요.
전체 123,711 | RSS
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT