티스토리 뷰

JVM

MySQL & JDBC 연결

글을 쓰는 개발자 2021. 5. 15. 17:19
반응형
public class Program {
	private String url = "jdbc:mysql://localhost:3306/<database>?characterEncoding=UTF-8&serverTimezone=UTC";
	private String host = "root";
	private String password = "******";
	private String driver = "com.mysql.cj.jdbc.Driver";
    
    public void select() throws ClassNotFoundException, SQLException {
    	String sql = "SELECT* FROM NOTICE";
		Connection con;
		Class.forName(driver);
		con = DriverManager.getConnection(url, host, password);
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(sql);
		while (rs.next()) {
			int id = rs.getInt("id");
			String title = rs.getString("TITLE");
			String writeId = rs.getString("WRITE_ID");
			String content = rs.getString("CONTENT");
			Date REGDATE = rs.getDate("REGDATE");
			int hit = rs.getInt("HIT");
			String files = rs.getString("Files");
			Notice notice = new Notice(id, title, writeId, content, REGDATE, hit, files);
			System.out.println(notice.toString());
		}
		rs.close();
		st.close();
		con.close();    
    }
    
    public int insert(Notice notice) throws ClassNotFoundException, SQLException {
		String title = notice.getTitle();
		String content = notice.getWrite_id();
		String file = notice.getFiles();
		int id = notice.getId();
		String sql = "UPDATE NOTICE SET TITLE=?,CONTENT=?,FILES=? WHERE ID = ? ";
		Connection con;
		Class.forName(driver);
		con = DriverManager.getConnection(url, host, password);
		PreparedStatement st = con.prepareStatement(sql);
		st.setString(1, title);
		st.setString(2, content);
		st.setString(3, file);
		st.setInt(4, id);
		int result = st.executeUpdate();
		st.close();
		con.close();
		return result;
	}
}

timezone에러 해결방법

?characterEncoding=UTF-8&serverTimezone=UTC
반응형

'JVM' 카테고리의 다른 글

[java][2d array][Comparator] 2차원 배열 정렬  (0) 2021.07.30
[JAVA][String] 정렬하는 방법  (0) 2021.07.27
JAVA ".class"에 대하여  (0) 2021.07.05
String charAt()  (0) 2021.06.25
자바 문자 비교  (0) 2021.05.12
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
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
글 보관함