DAO 클레스 => openConn, Closecone 공통되는 부분을 부모 class로 묶었습니다.
다른 exDAO 클레스를 만들땐 extends DAO 로 상속받아 사용하면 됩니다.
package com.semi.model;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DAO {
protected Connection con = null;
protected PreparedStatement pstmt = null;
protected ResultSet rs = null;
protected void openConn() {
try {
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("jdbc/myMariaDB");
con = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
protected void closeConn(ResultSet rs, PreparedStatement pstmt, Connection con) {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void closeConn(PreparedStatement pstmt, Connection con) {
try {
if (pstmt != null) pstmt.close();
if (con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
aws s3에 파일을 업로드 하는데 사용된 jquery ajax 요청 예제입니다.
<%=request.getContextPath()%>/upload.api 로 post 요청을 보내면 s3 에 파입 업로드 가능합니다.
모든 파일 업로드는 ajax 로 요청!
여러개의 파일 업로드api는 필요하면 구현하겠습니다.
파일 10개 정도 이하는 ajax 요청 여러번 보내는걸로 대체 가능합니다.
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" id="fileInput" name="file" />
<button type="button" onclick="uploadFile('<%=request.getContextPath()%>')">Upload</button>
</form>
<script type="text/javascript">
function uploadFile(baseUrl) {
let formData = new FormData();
let fileInput = $('#fileInput')[0];
let file = fileInput.files[0];
formData.append("file", file);
$.ajax({
url: baseUrl + '/upload.api',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(response) {
alert("File uploaded successfully.");
},
error: function(xhr, status, error) {
alert("An error occurred: " + error);
}
});
}
</script>
이제 aws s3 와 aws rds mariaDB 를 모두 사용 가능하게 되었습니다!
aws s3 => /upload.api 로 요청하면 끝
aws rds mariaDB => DAO 상속받아 쓰면 끝
오라클 문법과 mariaDB 문법은 약간 다르니 주의 바랍니다!
오라클 문법으로 쓰고 gpt 한테 mySQL 문법으로 변경 요청하면 잘 바꿔줍니다.
그냥 ~~ 하는 쿼리문 mySQL문으로 써줘 해도 잘써줍니다.
'2024 웹' 카테고리의 다른 글
class loader 장단점 (0) | 2024.04.10 |
---|---|
git pull 받기 (0) | 2024.04.09 |
semi project1 - git hub 클론 받기 (0) | 2024.04.08 |
git 연동 이클립스 (0) | 2024.04.05 |
semi project1 (0) | 2024.04.04 |
댓글