Last active
November 13, 2016 13:52
-
-
Save jb7959/473eaa5db97c951358e8dc5de7901158 to your computer and use it in GitHub Desktop.
Implemetation of DAO with MSSQL on Azure by JDBC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Model.DAO; | |
import com.microsoft.sqlserver.jdbc.*; | |
import java.sql.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by jerry on 2016-11-13. | |
* 이 클래스는 MicroSoft 의 MSSQL 서버와 연동하는 Model.DAO | |
* https://msdn.microsoft.com/ko-kr/library/ms378526(v=sql.110).aspx | |
* https://azure.microsoft.com/ko-kr/documentation/articles/sql-database-develop-java-simple/ | |
* https://msdn.microsoft.com/library/mt720656.aspx | |
* http://www.codejava.net/java-se/jdbc/connect-to-microsoft-sql-server-via-jdbc | |
*/ | |
public class DaoToMSSQL { | |
//싱글톤으로 만들기. | |
private static DaoToMSSQL ourInstance = new DaoToMSSQL(); | |
public static DaoToMSSQL getInstance() { | |
return ourInstance; | |
} | |
//Todo 아래 DB 비밀번호 암호화 및 처리방법 추가 하기 2016-11-13 | |
private static String connectionUrl = "jdbc:sqlserver://BoardName.database.windows.net:1433;database=WebBoard;user=userID@webboard;password={*******};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;;"; | |
private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver.class"; | |
public List<String> runSQL(String select, String from) throws SQLException { | |
String SQL = "SELECT " + select + "FROM " + from; | |
return select(SQL); | |
} | |
public List<String> runSQL(String select, String from, String where) throws SQLException { | |
String SQL = "SELECT " + select + "FROM " + from + "Where " + where; | |
return select(SQL); | |
} | |
private List<String> select(String SQL) throws SQLException { | |
List<String> list = new ArrayList<>(); | |
Connection connection = this.connecting(); | |
Statement statement = connection.createStatement(); | |
ResultSet resultSet = statement.executeQuery(SQL); | |
//DB 레코드 전체 조회 | |
while (resultSet.next()) { | |
int i = 1; | |
//DB 컬럼갯수까지 | |
while (i<=resultSet.getMetaData().getColumnCount()) { | |
System.out.println(resultSet.getString(i)); | |
list.add(resultSet.getString(i)); | |
i++; | |
} | |
} | |
clossingContection(resultSet, statement, connection); | |
return list; | |
} | |
private Connection connecting() { | |
Connection connection = null; | |
try { | |
//드라이버 등록 | |
//Class.for(driver) 이것과 아래 동일. | |
DriverManager.registerDriver(new SQLServerDriver()); | |
//연결 | |
connection = DriverManager.getConnection(connectionUrl); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
return connection; | |
} | |
} | |
private boolean clossingContection(ResultSet rs, Statement stmt, Connection conn) { | |
boolean result = true; | |
this.clossingContection(rs); | |
this.clossingContection(stmt); | |
this.clossingContection(conn); | |
return result; | |
} | |
private boolean clossingContection(ResultSet rs) { | |
boolean result = true; | |
if (rs != null) { | |
try { | |
rs.close(); | |
} catch (SQLException e) { | |
result = false; | |
} | |
} | |
return result; | |
} | |
private boolean clossingContection(Connection conn) { | |
boolean result = true; | |
if (conn != null) { | |
try { | |
conn.close(); | |
} catch (SQLException e) { | |
result = false; | |
} | |
} | |
return result; | |
} | |
private boolean clossingContection(Statement stmt) { | |
boolean result = true; | |
if (stmt != null) { | |
try { | |
stmt.close(); | |
} catch (SQLException e) { | |
result = false; | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment