-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package day01; | ||
import java.sql.*; | ||
import javax.swing.*; | ||
|
||
public class MemoInsert { | ||
|
||
public static void main(String[] args) | ||
throws ClassNotFoundException, SQLException | ||
{ | ||
String name=JOptionPane.showInputDialog("작성자 입력하세요"); | ||
String msg=JOptionPane.showInputDialog("메시지를 입력하세요"); | ||
if(name==null) return; | ||
|
||
//memo테이블에 insert문을 작성해서 메모글을 insert하는 프로그램을 완성하세요 | ||
//1. 홍길동 첫번째 작성한 글입니다 sysdate | ||
Class.forName("oracle.jdbc.driver.OracleDriver"); | ||
String url="jdbc:oracle:thin:@localhost:1521:XE"; | ||
String user="scott", pwd="tiger"; | ||
|
||
Connection con=DriverManager.getConnection(url,user,pwd); | ||
|
||
String sql="insert into memo(idx,name,msg,wdate) values(memo_seq.nextval,'"+name+"','"+msg+"',sysdate)"; | ||
|
||
System.out.println(sql); | ||
|
||
Statement stmt=con.createStatement(); | ||
//boolean b=stmt.execute(sql);//select문일때만 true를 반환함 | ||
//DML문장(insert/delete/update)일때는: int executeUpdate()로 실행시킨다. | ||
int b=stmt.executeUpdate(sql); | ||
//sql문에 의해 영향받은 레코드 개수를 반환한다. | ||
System.out.println("b: "+b+"개의 레코드를 넣었습니다"); | ||
|
||
stmt.close(); | ||
con.close(); | ||
|
||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package day01; | ||
import java.sql.*; | ||
/* JDBC : JAVA DATABASE CONNECTIVITY | ||
* Java 언어 <=== JDBC Driver(통역관) ====> DBMS(Oracle)==> SQL | ||
* | ||
* [1] 이클립스에서 ojdbc6.jar(드라이버) 파일을 Build Path에서 Add External Library로 추가한다 | ||
* [2] DB서버, TNSListener startup | ||
* [3] Java를 이용해서 DB에 연결해보자. | ||
* */ | ||
|
||
public class MyFirstJDBC { | ||
|
||
public static void main(String[] args) { | ||
try { | ||
//1. 드라이버 로딩 | ||
Class.forName("oracle.jdbc.driver.OracleDriver"); | ||
System.out.println("Driver Loading Success!!"); | ||
|
||
// String url="jdbc:oracle:thin:@localhost:1521:XE"; | ||
// //프로토콜:dbms유형:driver타입:host:port:전역데이터베이스이름 | ||
// String user="scott", pwd="tiger"; | ||
// | ||
// //2. DB와 연결=> DriverManager클래스의 getConnection()를 이용 | ||
// | ||
// Connection con=DriverManager.getConnection(url, user, pwd); | ||
// System.out.println("DB Connected..."); | ||
|
||
String url="jdbc:oracle:thin:@localhost:1521:XE"; | ||
//프로토콜:dbms유형:driver타입:host:port:전역데이터베이스이름 | ||
String user="scott", pwd="tiger"; | ||
|
||
//2. DB와 연결=> DriverManager클래스의 getConnection() | ||
|
||
Connection con=DriverManager.getConnection(url,user,pwd); | ||
System.out.println("DB Connected..."); | ||
|
||
|
||
//3. Query문 작성-SQL문 | ||
String sql="CREATE TABLE memo("; | ||
sql+="idx number(4) primary key,"; //글번호 | ||
sql+="name varchar2(30) not null, ";//작성자 | ||
sql+="msg varchar2(100),"; | ||
sql+="wdate date default sysdate)"; | ||
|
||
//4. Statement객체 얻기 => Connection의 createStatement()를 이용 | ||
Statement stmt=con.createStatement(); | ||
|
||
//5. Statement 의 execute()/executeXXX() 메서드를 이용해서 쿼리문을 실행시킨다. | ||
|
||
//boolean execute(): 모든 sql문을 실행시킨다. | ||
//int executeUpdate(): insert/delete/update문을 실행시킨다 | ||
//ResultSet executeQuery(): select문을 실행시킨다. | ||
|
||
boolean b=stmt.execute(sql); | ||
System.out.println("b: "+b); | ||
//sql문이 select문이면 true를 반환하고, 그 외의 문장이면 false를 반환한다 | ||
|
||
//6. DB관련 자원 반납 | ||
stmt.close(); | ||
con.close(); | ||
}catch(ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
}catch(SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
|