Maria DB JDBC 기본 구문 - Engineer of NiagaraFramework

Tridium, NiagaraFramework, SCADA, HMI, PLC, Automation, SmartFAM etc.. About controls.
나이아가라 프레임워크 QnA : neverlikekami@gmail.com

2019년 6월 4일 화요일

Maria DB JDBC 기본 구문

MariaDB 와 JAVA 연동을 위한 기본 구문 


참고 소스


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MainEntry {
  public static void main(String[] args) {
    Connection con = null;
    PreparedStatement pstmt = null;  
    ResultSet rs = null;
    try {
          Class.forName("org.mariadb.jdbc.Driver");
          con = DriverManager.getConnection("jdbc:mariadb://100.100.100.7:3306/dbname","userId","password");
          pstmt = con.prepareStatement("select * from his_bus_voltage");
          rs = pstmt.executeQuery();
          
          while(rs.next()) {
          //.
          }
        } catch(Exception e) {
          e.printStackTrace();
        } finally {
          try {
              if(rs != null) {
              rs.close(); // 선택 사항
          }
              if(pstmt != null) {
              pstmt.close(); // 선택사항이지만 호출 추천
              }
              if(con != null) {
              con.close(); // 필수 사항
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
    }
  }
}
 
cs

댓글 없음:

댓글 쓰기

Post list