package test;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
public class JDBC3 {
public static void main(String[] args) {
Connection con = null;
Statement st = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1523:orcl789","test","rootoor");
String sql = "insert into test_T values('자바씨', '34')";
st = con.createStatement();
st.executeUpdate(sql);
System.out.println("입력 성공");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (st != null) st.close();
if (con != null) con.close();
} catch (Exception e) {}
}
}
}
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
public class JDBC5 {
public static void main(String[] args)
throws Exception{
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1523:orcl789", "test", "rootoor");
PreparedStatement pstmt = con.prepareStatement (
"insert into test_T (age, name) values (?, ?)");
while (1>0) {
System.out.println("이름을 입력하세요 : ");
Scanner sc = new Scanner(System.in);
String name = sc.next();
System.out.println("나이를 입력하세요 : ");
String age = sc.next();
pstmt.setString(1, age);
pstmt.setString(2, name);
int cnt = pstmt.executeUpdate();
if (cnt > 0) {
System.out.println("저장 완료");
System.out.println("종료하시겠습니까? y / n");
String select = sc.next();
if (select.equalsIgnoreCase("y")) {
if (pstmt != null) pstmt.close();
if (con != null) con.close();
break;
} else if (select.equalsIgnoreCase("n")) {
continue;
}
}
}
}
}
'코딩 > JAVA' 카테고리의 다른 글
JDBC 2. 데이터 삭제, 수정 (0) | 2022.08.25 |
---|---|
JAVA 34. Getter Setter (0) | 2022.08.08 |
JAVA 33. 문자의 길이와 위치 (0) | 2022.08.08 |
JAVA 32. IO(Input / Output) (0) | 2022.08.08 |
JAVA 31. 다형성 (0) | 2022.08.08 |