카테고리 없음
[JDBC] scanner 입력받아 인서트
코딩 뉴비
2020. 12. 25. 14:19
www.munsam.info/xe/JAVA/517609
자바 프로그래밍 - 키보드로 입력받아 DB에 저장하기(insert)
키보드로 입력받은 정보를 insert문으로 DB에 저장하는 프로그램 public static void main(String[] args) { Scanner sc=new Scanner(System.in); try{ &n...
www.munsam.info
참고
package realestate.project;
import java.sql.*;
import java.util.Scanner;
public class DaoLogin {
// 1. 오라클 접속 클래스 -> 메모리로 로딩 (초기화)
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// public static void login() {
public static void main(String[] args) {
// 2. url, id, password 생성
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String id = "housemate";
String pass = "housemate";
// 3. 오라클 객체 생성
try {
Connection con = DriverManager.getConnection(url, id, pass);
Scanner sc = new Scanner(System.in);
String cId, cPw, cName, cGcode, cPhone, cEmail, cAdd;
// String sysdate = "sysdate";
// String code = "'"+"ccode_seq.NEXTVAL"+"'";
// String date = "'sysdate'";
int cnt = 0;
System.out.println("회원가입에 필요한 정보를 입력해 주세요...");
System.out.print("아이디 : ");
cId = sc.next();
System.out.print("비밀번호 : ");
cPw = sc.next();
System.out.print("이름 : ");
cName = sc.next();
System.out.print("주민등록번호 : ");
cGcode = sc.next();
System.out.print("전화번호 : ");
cPhone = sc.next();
System.out.print("이메일 : ");
cEmail = sc.next();
System.out.print("주소 : ");
cAdd = sc.next();
PreparedStatement ps = con.prepareStatement("INSERT INTO cst VALUES (ccode_seq.NEXTVAL,?,?,?,?,?,?,?,sysdate)");
// (ccode, cid, cpw, cname, gcode, cphone, cemail, caddress)
// ps.setString(1, "");
ps.setString(1, cId);
ps.setString(2, cPw);
ps.setString(3, cName);
ps.setString(4, cGcode);
ps.setString(5, cPhone);
ps.setString(6, cEmail);
ps.setString(7, cAdd);
// ps.setString(9, date);
ps.executeUpdate();
Statement st = null;
ResultSet rs = null;
st = (Statement) con.createStatement();
rs = st.executeQuery("SELECT * FROM cst");
while (rs.next()) {
for (int i = 1; i < 5; i++) {
String str = rs.getString(i);
}
}
} catch (SQLException sqex) {
System.out.println("SQLException: " + sqex.getMessage());
System.out.println("SQLState: " + sqex.getSQLState());
}
}
}
// } catch (SQLException e) {
// e.printStackTrace();
// } catch (InterruptedException e) { // 예외처리 한번 더.
// e.printStackTrace();
//
// } finally { // 이렇게 해줌으로서 혹시 모를 예외를 방지.....
// // 예외가 발생했든 안했든 finally는 무조건 실행함.
// try { // 8. 열린 객체 모두 닫기 (종료)
//
// if (rs != null)
// rs.close();
// if (preStmt != null)
// preStmt.close();
// if (con != null)
// con.close();
//
// } catch (SQLException e) {
// e.printStackTrace();
// }
// }
//
// }
//
//}