Last active
October 13, 2022 02:07
-
-
Save zmrdltl/b94b11b6ea463baaf84ecbf8abcb002c to your computer and use it in GitHub Desktop.
jdbc connection class 예제
This file contains 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 com.tmax.dx.service.app; | |
import java.sql.Connection; | |
import java.sql.*; | |
import com.tmax.superobject.manager.DbcpConnectionManager; | |
public class ConnectionManager { | |
public Connection getConnection() throws SQLException { | |
return DbcpConnectionManager.getInstance().getConnection("DX_DATABASE"); | |
} | |
} |
This file contains 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 com.tmax.dx.service.app; | |
import java.sql.Connection; | |
import java.sql.SQLException; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonObject; | |
import com.tmax.superobject.object.AbstractServiceObject; | |
import com.tmax.dx.model.dao.app.AppDAO; | |
import com.tmax.dx.model.dto.app.AppDTO; | |
import com.tmax.superobject.object.BodyObject; | |
import com.tmax.superobject.object.DefaultBodyObject; | |
public class CreateAppService extends AbstractServiceObject { | |
@Override | |
public void service(BodyObject bodyObject) { | |
Connection conn = null; | |
try { | |
JsonObject request = bodyObject.getJsonObject(); | |
ConnectionManager cm = new ConnectionManager(); | |
conn = cm.getConnection(); | |
conn.setAutoCommit(false); | |
AppDAO appDAO = new AppDAO(conn); | |
AppDTO appDTO = new AppDTO(); | |
int appId = request.get("appId").getAsInt(); | |
int roomId = request.get("roomId").getAsInt(); | |
int userId = request.get("userId").getAsInt(); | |
String appName = request.get("appName").getAsString(); | |
String timestamp = request.get("timestamp").getAsString(); | |
appDTO.setId(appId); | |
appDTO.setRoomId(roomId); | |
appDTO.setUserId(userId); | |
appDTO.setAppName(appName); | |
appDTO.setTimeStamp(timestamp); | |
JsonObject json = new JsonObject(); | |
try { | |
AppDTO outAppDto = appDAO.createApp(appDTO); | |
json.addProperty("id", outAppDto.getId()); | |
json.addProperty("roomId", outAppDto.getRoomId()); | |
json.addProperty("userId", outAppDto.getUserId()); | |
json.addProperty("appName", outAppDto.getAppName()); | |
json.addProperty("timestamp", outAppDto.getTimeStamp()); | |
} catch (Exception e) { | |
json.addProperty("error", e.toString()); | |
e.printStackTrace(); | |
} | |
JsonArray jsonArray = new JsonArray(); | |
jsonArray.add(json); | |
BodyObject response = new DefaultBodyObject(); | |
JsonObject responseBody = new JsonObject(); | |
responseBody.add("response", jsonArray); | |
response.setJsonObject(responseBody); | |
setReply(response); | |
} catch (SQLException e) { | |
try { | |
conn.rollback(); | |
} catch (SQLException e1) { | |
e1.printStackTrace(); | |
} | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void completeService() { | |
} | |
} |
This file contains 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 com.tmax.dx.service.app; | |
import java.sql.Connection; | |
import java.sql.SQLException; | |
import java.util.List; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonObject; | |
import com.tmax.dx.model.dao.app.AppDAO; | |
import com.tmax.dx.model.dto.app.AppDTO; | |
import com.tmax.superobject.object.AbstractServiceObject; | |
import com.tmax.superobject.object.BodyObject; | |
import com.tmax.superobject.object.DefaultBodyObject; | |
public class SelectAllAppListService extends AbstractServiceObject { | |
@Override | |
public void service(BodyObject bodyObject) { | |
Connection conn = null; | |
try{ | |
ConnectionManager cm = new ConnectionManager(); | |
conn = cm.getConnection(); | |
conn.setAutoCommit(false); | |
AppDAO appDAO = new AppDAO(conn); | |
List<AppDTO> appDTOList = appDAO.selectAllAppList(); | |
JsonArray jsonArray = new JsonArray(); | |
for (AppDTO appDTO : appDTOList) { | |
JsonObject json = new JsonObject(); | |
json.addProperty("id", appDTO.getId()); | |
json.addProperty("room_id", appDTO.getRoomId()); | |
json.addProperty("user_id", appDTO.getUserId()); | |
json.addProperty("app_name", appDTO.getAppName()); | |
json.addProperty("timestamp", appDTO.getTimeStamp()); | |
jsonArray.add(json); | |
} | |
BodyObject response = new DefaultBodyObject(); | |
JsonObject responseBody = new JsonObject(); | |
responseBody.add("response", jsonArray); | |
response.setJsonObject(responseBody); | |
setReply(response); | |
} catch (SQLException e) { | |
try { | |
conn.rollback(); | |
} catch (SQLException e1) { | |
e1.printStackTrace(); | |
} | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void completeService() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment