MySQL - Java 간단예제

MySQL - JAVA 간단예제


https://dev.mysql.com/downloads/
에서 Connector/J를 설치해야 한다. (Connector .jar 파일이 프로젝트에 있어야 한다)


[소스 코드]
2015년, 필자가 고등학교때 작성한 코드~
지금 보면 고쳐야 할 부분이 많다.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class DBConnection {
        private String url;
        private int port;
        private String dbName;
        private String userName;
 
        private Connection db_conn = null;
        private PreparedStatement db_pstmt = null;
        private ResultSet db_rs = null;
 
        public static int QUERY_SUCCESS = 1;
        public static int QUERY_OKAY = 0;
        public static int QUERY_FAIL = -1;
 
 
        public int query(String sql) {
                reConnect();
 
                try {
                        db_pstmt = db_conn.prepareStatement(sql);
 
                        switch (db_pstmt.executeUpdate()) {
                        case 1:
                                return QUERY_SUCCESS;
                        case 0:
                                return QUERY_OKAY;
                        }
                } catch (Exception e) {
                        return QUERY_FAIL;
                }
 
                return QUERY_FAIL;
        }
 
        public int selectQuery(String sql) {
                reConnect();
                int nSize = 0;
 
                try {
                        db_pstmt = db_conn.prepareStatement(sql);
                        db_rs = db_pstmt.executeQuery();
 
                        db_rs.last();
                        nSize = db_rs.getRow();
                        db_rs.beforeFirst();
                } catch (Exception e) {
                        return QUERY_FAIL;
                }
 
                return nSize;
        }
 
        public boolean hasNext() {
                boolean b = false;
                try {
                        b = db_rs.next();
                } catch (Exception e) {
                        return false;
                }
                return b;
        }
 
        public String getSelectData(String columnLabel) {
                String str = "";
                try {
                        str = db_rs.getString(columnLabel);
                } catch (Exception e) {}
 
                return str;
        }
 
        public String getSelectData(int columnIndex) {
                String str = "";
                try {
                        str = db_rs.getString(columnIndex);
                } catch (Exception e) {}
 
                return str;
        }
 
        public boolean isConnect() {
                if (selectQuery("select 1"== QUERY_SUCCESS) {
                        return true;
                }
                return false;
        }
 
        private void reConnect() {
                try {
                        db_pstmt = db_conn.prepareStatement("select 1");
                        db_rs = db_pstmt.executeQuery();
                } catch (Exception e) {}
        }
 
        public int connect(String url, int port, String dbName, String userName, String passwd) {
                this.url = url;
                this.port = port;
                this.dbName = dbName;
                this.userName = userName;
 
                try {
                        Class.forName("com.mysql.jdbc.Driver");
                        db_conn = DriverManager.getConnection(
                                        "jdbc:mysql://" + url + ":" + port + "/" + dbName + "?autoReconnect=true", userName, passwd);
                        System.out.println("Successful to Connect Mysql Server at " + url + " port " + port);
                } catch (Exception e) {
                        release();
                        return QUERY_FAIL;
                }
 
                return QUERY_SUCCESS;
        }
 
        public void release() {
                try {
                        if (db_rs != null){db_rs.close();}
                        if (db_pstmt != null){db_pstmt.close();}
                        if (db_conn != null){db_conn.close();}
                } catch (Exception e) {}
        }
 
        /*
        public static void main(String args[]){
                // Init
                DBConnection conn = new DBConnection();
                System.out.println(conn.connect("@호스트명",3306,"@DB명","@사용자명","@비밀번호"));
                // INSERT / UPDATE / DELETE 쿼리 등
                System.out.println(conn.query("INSERT INTO test VALUES('ttt')"));
                // SELECT 쿼리
                System.out.println(conn.selectQuery("SELECT * FROM test"));
                while (conn.hasNext()) {
                        System.out.println(conn.getSelectData("key"));
                        System.out.println(conn.getSelectData("value"));
                }
                // Release
                conn.release();
        }
        */
}
cs

댓글