티스토리 뷰
자바 jdbc 를 이용해 라이믹스 게시판에 insert 하는 소스이다.
mariadb-java-client-1.1.9.jar jdbc 드라이버를 사용하는 경우이다.
최신 마리아 db driver는 사용방법이 다르다.
myriadb-java-client-2.4.0.jar 는 다른글에서 적어둔다.
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Jdbc {
final static int MODULE_SRL = 52;
final String DRIVER = "org.mariadb.jdbc.Driver";
final String DB_IP = "localhost";
final String DB_PORT = "3306";
final String DB_NAME = "rx";
final String DB_USER_ID = "rx";
final String DB_PW = "YOUR_PASSWORD";
final String DB_URL =
"jdbc:mariadb://" + DB_IP + ":" + DB_PORT + "/" + DB_NAME;
public Connection getConn(){
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(DB_URL, DB_USER_ID, DB_PW);
if (conn != null) {
System.out.println("DB 접속 성공");
}
} catch (ClassNotFoundException e) {
System.out.println("드라이버 로드 실패");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("DB 접속 실패");
e.printStackTrace();
}
return conn;
}
public int getSeq() {
Connection conn = null;
conn = getConn();
int seq = 0;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
String sql = "insert into rx_sequence values(?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "0");
pstmt.executeUpdate();
rs = pstmt.getGeneratedKeys();
while (rs.next()) {
seq = rs.getInt(1);
}
} catch (Exception e) {
System.out.println("error: " + e);
} finally {
try {
if (rs != null) { rs.close(); }
if (pstmt != null) { pstmt.close(); }
if (conn != null) { conn.close(); }
} catch (SQLException e) {
e.printStackTrace();
}
}
return seq;
}
public void insertDb(String tit, String cont, String nick) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
LocalDateTime now=LocalDateTime.now();
String date=now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
int document_srl = getSeq();
System.out.println("document_srl="+document_srl);
int module_srl = MODULE_SRL;
String lang_code = "ko";
String is_notice = "N";
String title = tit;
String title_bold = "N";
String title_color = "N";
String content = cont;
String nick_name = nick;
String regdate = date;
String last_update = date;
int list_order = -document_srl;
int update_order = -document_srl;
String allow_trackback = "N";
String notify_message = "N";
String status = "PUBLIC";
String comment_status = "ALLOW";
conn = getConn();
String sql = "insert into rx_documents (document_srl, module_srl, lang_code, is_notice, title"
+ ", title_bold, title_color, content, nick_name, list_order"
+ ", regdate, last_update, update_order, allow_trackback, notify_message"
+ ", status, comment_status)"
+ " values("
+ "?,?,?,?,?"
+ ",?,?,?,?,?"
+ ",?,?,?,?,?"
+ ",?,?"
+ ")";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, document_srl);
pstmt.setInt(2, module_srl);
pstmt.setString(3, lang_code);
pstmt.setString(4, is_notice);
pstmt.setString(5, title);
pstmt.setString(6,title_bold);
pstmt.setString(7,title_color);
pstmt.setString(8,content);
pstmt.setString(9,nick_name);
pstmt.setInt(10,list_order);
pstmt.setString(11,regdate);
pstmt.setString(12,last_update);
pstmt.setInt(13,update_order);
pstmt.setString(14,allow_trackback);
pstmt.setString(15,notify_message);
pstmt.setString(16,status);
pstmt.setString(17,comment_status);
pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("error: " + e);
} finally {
try {
if (rs != null) { rs.close(); }
if (pstmt != null) { pstmt.close(); }
if (conn != null) { conn.close(); }
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception{
System.out.println("시작.");
Jdbc jdbc=new Jdbc();
//jdbc.getConn();
List<String> listNick=new ArrayList<>();
listNick.add("홍길동");
listNick.add("이몽룡");
listNick.add("성춘향");
Random random = new Random();
int i = random.nextInt(listNick.size());
String nick=listNick.get(i);
System.out.println("nick="+nick);
jdbc.insertDb("제목", "본문", nick);
System.out.println("끝.");
}
}
'라이믹스' 카테고리의 다른 글
자바 마리아db jdbc 드라이버 다운로드 (0) | 2023.06.19 |
---|---|
[자바] 네이버 뉴스 파싱 (0) | 2023.06.19 |
자바 db_insert_id insert한 키값 가져오기 (0) | 2023.06.19 |
로컬 xampp 에서 라이믹스 설치하기 (0) | 2023.06.18 |
xampp 라이믹스 이미지변환 기능을 사용하기 위해 GD라이브러리가 설치되어 있어야 합니다. (0) | 2023.06.17 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- XE3
- 문자열
- C
- xe addon
- 파싱
- webix
- xe애드온
- 포인터
- XE
- php
- 자바 smtp
- 이클립스
- MySQL
- ocjap
- 스크래핑
- 자바
- 오라클
- EC
- C언어
- proc
- 파이썬
- 프로씨
- 플러터
- esql
- KG
- ocajp
- Python
- 라이믹스 모듈
- JDBC
- 인포믹스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함