-
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
3 changed files
with
116 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,31 @@ | ||
package day03; | ||
import java.sql.*; | ||
|
||
public class PreparedStatementTest { | ||
|
||
public static void main(String[] args) throws Exception{ | ||
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 emp(empno, ename, job, hiredate, sal, deptno)"; | ||
sql+=" VALUES(?,?,?,SYSDATE,?,?)"; | ||
//?를 제외한 SQL문을 미리 전처리하여 DBMS포맷에 맞게 컴파일해서 미리 준비시켜둔다. | ||
|
||
PreparedStatement pstmt=con.prepareStatement(sql); | ||
pstmt.setInt(1, 8000); | ||
pstmt.setString(2, "TOM"); | ||
pstmt.setString(3, "MANAGER"); | ||
pstmt.setInt(4, 4000); | ||
pstmt.setInt(5, 10); | ||
|
||
int n=pstmt.executeUpdate(); | ||
|
||
System.out.println(n+"개의 레코드 삽입함"); | ||
|
||
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,43 @@ | ||
package day03; | ||
import java.util.*; | ||
import java.sql.*; | ||
public class PreparedStatementTest2 { | ||
|
||
public static void main(String[] args) throws Exception{ | ||
Scanner sc=new Scanner(System.in); | ||
System.out.println("수정할 사번 입력: "); | ||
int no=sc.nextInt(); | ||
System.out.println("수정할 업무 입력: "); | ||
String job=sc.next(); | ||
|
||
System.out.println("수정할 부서번호 입력: "); | ||
int dno=sc.nextInt(); | ||
System.out.println("수정할 급여 입력: "); | ||
int sal=sc.nextInt(); | ||
|
||
//emp 테이블을 수정하는 문장을 PreparedStatement이용해 실행시키세요 | ||
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="update emp set job=?,deptno=?,sal=? where empno=?"; | ||
|
||
PreparedStatement ps=con.prepareStatement(sql); | ||
|
||
ps.setString(1, job); | ||
ps.setInt(2, dno); | ||
ps.setInt(3, sal); | ||
ps.setInt(4, no); | ||
|
||
int n=ps.executeUpdate(); | ||
|
||
System.out.println(n+"개의 레코드 수정함"); | ||
|
||
ps.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,42 @@ | ||
package day03; | ||
import java.sql.*; | ||
import java.util.Scanner; | ||
|
||
import common.DBUtil; | ||
public class PreparedStatementTest3 { | ||
|
||
public static void main(String[] args) throws Exception{ | ||
Connection con=DBUtil.getCon(); | ||
//검색할 사원의 이름을 입력받아서 해당 사원정보를 출력하세요 | ||
//사번, 사원명, 부서명, 담당업무, 입사일, 근무지 가져와 출력하기 | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("검색할 이름을 입력하세요"); | ||
String name = sc.nextLine(); | ||
|
||
String sql = "select empno, ename, e.deptno , job , hiredate ,d.loc"; | ||
sql +=" from emp e join dept d on e.deptno = d.deptno"; | ||
sql +=" where ename = upper(?)"; | ||
|
||
PreparedStatement ps=con.prepareStatement(sql); | ||
ps.setString(1, name); | ||
ResultSet rs=ps.executeQuery(); | ||
int cnt=0; | ||
while(rs.next()) { | ||
cnt++; | ||
int empno=rs.getInt(1); | ||
String ename=rs.getString(2); | ||
int dno=rs.getInt(3); | ||
String job=rs.getString(4); | ||
Date hdate=rs.getDate(5); | ||
String loc=rs.getString(6); | ||
System.out.println(empno+"\t"+ename+"\t"+dno+"\t"+job+"\t"+hdate+"\t"+loc); | ||
}//------------------- | ||
if(cnt==0) | ||
System.out.println("검색한 사원은 없습니다"); | ||
|
||
rs.close(); | ||
ps.close(); | ||
con.close(); | ||
} | ||
|
||
} |