[Day5] JSP/JDBC 5 [11/1]
1. transaction ํธ๋์ญ์ ์ฒ๋ฆฌ
-- [ ํธ๋์ญ์
์ฒ๋ฆฌ ]
-- ์) ๊ณ์ข ์ด์ฒด = A UPDATE , B UPDATE
-- ๋
ผ๋ฆฌ์ ์ธ ์ด ๋๊ฐ์ ์์
์ ํ๋๋ก ๋ฌถ์ด์
๋ชจ๋ ์ฑ๊ณต/ ๋ชจ๋ ์คํจ.
CREATE OR REPLACE PROCEDURE UP_๊ณ์ข์ด์ฒด
(
p๊ณ์ข์ ๋ณด1
, p๊ณ์ข์ ๋ณด2
, p์ด์ฒด๊ธ์ก
)
IS
BEGIN
UPDATE ํ
์ด๋ธ๋ช
SET ์๊ณ = ์๊ณ - p์ด์ฒด๊ธ์ก
WHERE p๊ณ์ข์ ๋ณด1;
UPDATE ํ
์ด๋ธ๋ช
SET ์๊ณ = ์๊ณ + p์ด์ฒด๊ธ์ก
WHERE p๊ณ์ข์ ๋ณด2;
COMMIT;
EXCEPTION
WHEN THEN
ROLLBACK;
END;
[ํธ๋์ญ์ ์คํจ]
Connection conn = null;
conn = DBconn.getConnection();
PreparedStatement psmt = null;
int rowCount = 0;
String sql = "INSERT INTO dept VALUES(?,?,?)";
// 1. deptํ
์ด๋ธ ๋ถ์ ์ถ๊ฐ O
try{
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,50);
pstmt.setString(2,"QC");
pstmt.setString(3,"SEOUL");
rowCount = pstmt.executeUpdate();
if(rowCount ==1) System.out.println("> 50/QC/SEOUL ๋ถ์ ์ถ๊ฐ ์ฑ๊ณต!!!");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e){
e.printStackTrace();
}
}
// 2. deptํ
์ด๋ธ ๋ถ์ ์ถ๊ฐ X
try{
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,100);
pstmt.setString(2,"XXX");
pstmt.setString(3,"YYY");
rowCount = pstmt.executeUpdate();
if(rowCount ==1) System.out.println("> 50/QC/SEOUL ๋ถ์ ์ถ๊ฐ ์ฑ๊ณต!!!");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e){
e.printStackTrace();
}
}
[ํธ๋์ญ์ ์ฑ๊ณต]
conn.setAutoCommit(false); // ์๋์ปค๋ฐ off
conn.commit(); // ์ค๋ฅ๊ฐ ์๋๋ฉด ์ปค๋ฐ
conn.rollback(); // ๋กค๋ฐฑ
Connection conn = null;
conn = DBconn.getConnection();
PreparedStatement psmt = null;
int rowCount = 0;
String sql = "INSERT INTO dept VALUES(?,?,?)";
try{
conn.setAutoCommit(false); // ์๋์ปค๋ฐ off
// 1๋ฒ O
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,50);
pstmt.setString(2,"QC");
pstmt.setString(3,"SEOUL");
rowCount = pstmt.executeUpdate();
if(rowCount ==1) System.out.println("> 50/QC/SEOUL ๋ถ์ ์ถ๊ฐ ์ฑ๊ณต!!!");
// 2๋ฒ X
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,100);
pstmt.setString(2,"XXX");
pstmt.setString(3,"YYY");
rowCount = pstmt.executeUpdate();
if(rowCount ==1) System.out.println("> 50/QC/SEOUL ๋ถ์ ์ถ๊ฐ ์ฑ๊ณต!!!");
conn.commit(); // ์ค๋ฅ๊ฐ ์๋๋ฉด ์ปค๋ฐ
} catch (SQLException e) {
e.printStackTrace();
try{
conn.rollback(); // ๋กค๋ฐฑ
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally {
try {
pstmt.close();
} catch (SQLException e){
e.printStackTrace();
}
}
2. CallableStatement (cstmt) = null
- ์ ์ฅ ํ๋ก์์ ์ฌ์ฉํ๋ stmt
String sql = "{ call ์ ์ฅํ๋ก์์ ๋ช (ํ๋ผ๋ฏธํฐ...)}"; |
* ํ๋ก์์ ํ ์คํธ
EXEC UP_INSERTEMP (pempno => 9999, pname => 'kim');
* java
psvm() {
// 1. up_insertemp ์ ์ฅ ํ๋ก์์ ํ๋ ์์ฑ
int empno = 9999;
String ename = "ADMIN";
String mgr = 1234;
int deptno = 10;
Date hiredate = new Date(1991, 2, 23);
Connection conn = null;
CollableStatement cstmt = null;
int rowCount = 0;
String sql = "{ call UP_INSERTEMP(pempno => ?, pename => ?, pdeptno => ?, phiredate => ? )}";;
conn = DBConn.getConnection();
try{
cstmt = conn.prepareCall(sql);
cstmt.setInt(1,empno);
cstmt.setString(2, ename);
cstmt.setInt(3,mgr);
cstmt.setInt(4,deptno);
cstmt.setDate(5,hiredate);
rowCount = cstmt.executeUpdate();
if(rowCount ==1) System.out.println("์ฌ์ ์ถ๊ฐ ์ฑ๊ณต!!!");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try{
cstmt.close();
} catch (SQLException e){
e.printStackTrace();
}
}
}
EX 02_03 ํ์ผ ๋ฆฌํฉํ ๋ง
1) ์ ์ฅ ํ๋ก์์ ๋ง๋ค๊ณ
2) ์๋ฐ ํ์ผ ์์ฑ
- ์ ์ฅ ํ๋ก์์ (์ญ์ )
๋ถ์ ๋ฒํธ ์ ๋ ฅ ๋ฐ์์ ๋ถ์ ์ญ์
CREATE OR REPLACE PROCEDURE UP_DELETEDEPT
(
pdeptno dept.deptno%type
)
IS
BEGIN
DELETE FROM dept
WHERE deptno = pdeptno;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20011, '> DEPT DELETE X...');
END;
EXEC UP_DELETEDEPT(50);
INSERT INTO dept VALUES ( 50, 'QC', 'SEOUL');
COMMIT;
- ์๋ฐ ํ์ผ ์์ฑ
private static void deleteDept() {
System.out.print("> ์ญ์ ํ ๋ถ์๋ฒํธ(deptno) ์
๋ ฅ ? ");
int pdeptno = scanner.nextInt();
String sql = "{ CALL UP_DELETEDEPT( ? ) }";
try {
cstmt = conn.prepareCall(sql);
cstmt.setInt(1, pdeptno);
int rowCount = cstmt.executeUpdate();
if( rowCount == 1 ) {
System.out.println("> 1๊ฐ ๋ถ์ ์ญ์ ์๋ฃ!!!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
cstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
update
- ํน์ ๋ถ์ ๋ฒํธ์ dname๊ณผ loc ์์
- ์ ์ฅ ํ๋ก์์ ํ๋ผ๋ฏธํฐ ๋ํดํธ๊ฐ ์ค์ ๋ฐฉ๋ฒ ๋ ๊ฐ์ง
pdname dept.dname%type := null
ploc dept.loc%type DEFAULT NULL
CREATE OR REPLACE PROCEDURE UP_UPDATEDEPT
(
pdeptno dept.deptno%type
, pdname dept.dname%type := null
, ploc dept.loc%type DEFAULT NULL
)
IS
BEGIN
UPDATE dept
SET dnmae = NVL(pdname, dname), loc = NVL(ploc, loc)
WHERE deptno = pdeptno;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20013, '>DEPT INSERT X...');
END;
// ๋ถ์ ์์
private static void updateDept() {
System.out.print("> ์์ ํ ๋ถ์๋ฒํธ(deptno) ์
๋ ฅ ? ");
int pdeptno = scanner.nextInt();
System.out.print("> ์์ ํ ๋ถ์๋ช
, ์ง์ญ๋ช
์
๋ ฅ ? ");
String pdname = scanner.next();
String ploc = scanner.next();
String sql = { CALL UP_UPDATEDEPT(pdeptno => ?, pdname => ? ploc => ?) };
// System.out.println( sql );
// 1. 2.
// 3. UPDATE ์ฒ๋ฆฌ
try {
cstmt = conn.prepareCall(sql);
cstmt.setInt(1,pdeptno);
cstmt.setString(2,pdname);
cstmt.setString(3,ploc);
int rowCount = cstmt.executeUpdate();
if( rowCount == 1 ) {
System.out.println("> 1๊ฐ ๋ถ์ ์์ ์๋ฃ!!!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 4.
}
select
ํ๋ก์์
CREATE OR REPLACE PROCEDURE UP_SELECTDEPT
(
pcursor OUT SYS_REFCURSOR
)
IS
vsql VARCHAR2(1000); -- ๋์ ์ฟผ๋ฆฌ
BEGIN
vsql := 'SELECT * '
vsql := vsql || ' FROM dept ';
-- OPEN FOR ๋ฌธ
OPEN pcursor FOR vsql;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20013, '>DEPT INSERT X...');
END;
์๋ฐ
refcursor -> rs (result set) ์ผ๋ก ๋ฐ๋ก ๋ฐ์์ค์ง ์์ !!
์ปค์๋ฅผ cstmt์ ๋ฑ๋กํ๊ณ getObject๋ก ๋ฐ์์ด!
// ์ถ๋ ฅ์ฉ ๋งค๊ฐ๋ณ์ OUT ๋ฑ๋ก
cstmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
cstmt.executeQuery();
// ์ถ๋ ฅ์ฉ ๋งค๊ฐ๋ณ์๋ก๋ถํฐ ๊ฐ์ ธ์ด
rs = (ResultSet)cstmt.getObject(1);
private static void selectAllDept() {
String sql { CALL UP_SELECTDEPT((?) };
ArrayList<DeptDTO> deptList = null;
try {
cstmt = conn.prepareCall(sql);
// ์ถ๋ ฅ์ฉ ๋งค๊ฐ๋ณ์ OUT ๋ฑ๋ก
cstmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
cstmt.executeQuery();
// ์ถ๋ ฅ์ฉ ๋งค๊ฐ๋ณ์๋ก๋ถํฐ ๊ฐ์ ธ์ด
rs = (ResultSet)cstmt.getObject(1);
if( rs.next() ) {
deptList = new ArrayList<DeptDTO>();
do {
DeptDTO dto = new DeptDTO( rs.getInt("deptno") , rs.getString("dname"), rs.getString("loc"));
deptList.add(dto);
} while (rs.next());
} // if
printDept( deptList );
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
cstmt.close();
rs.close();
DBConn.close(); ํ๋ก๊ทธ๋จ ์ข
๋ฃํ ๋ ๊ทธ ๋ ํ ๋ฒ๋ง ๋ซ๊ธฐ.
} catch (SQLException e) {
e.printStackTrace();
}
} // try
}
id_check ์์
ํ๋ก์์
create or replace PROCEDURE up_idCheck
(
pempno IN emp.empno%type
,pempnoCheck OUT NUMBER -- ์ฌ์ฉ๊ฐ๋ฅ 0, ์ฌ์ฉ๋ถ๊ฐ๋ฅ 1
)
IS
BEGIN
SELECT COUNT(*) INTO pempnoCheck
FROM emp
WHERE empno = pempno;
-- EXCEPTION
END;
์๋ฐ
psvm{
Syso.("์ค๋ณต์ฒดํฌํ ์์ด๋ ์
๋ ฅ");
Scanner scanner = new Scanner(System.in);
int pempno = scanner.nextInt();
Connection conn = null;
CallableStatement cstmt = null;
String sql = " {CALL up_idCheck(?,?) }";
conn = DBConn.getConnection();
try {
cstmt = conn.prepareCall(sql);
cstmt.setInt(1,pempno); // IN
cstmt.registerOutParameter(2, oracle.jdbc.OracleTypes.INTEGER);
cstmt.executeQuery();
int idCheck = (int)cstmt.getObject(2);
if(idCheck == 0) {
System.out.println("์ฌ์ฉ๊ฐ๋ฅํ ์์ด๋");
} else {
System.out.println("์ฌ์ฉ์ค์ธ ์์ด๋");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
cstmt.close();
DBConn.close(); ํ๋ก๊ทธ๋จ ์ข
๋ฃํ ๋ ๊ทธ ๋ ํ ๋ฒ๋ง ๋ซ๊ธฐ.
} catch (SQLException e) {
e.printStackTrace();
}
} // try
}
search
-sql-
CREATE OR REPLACE PROCEDURE UP_SEARCHDEPT
(
psearchCondition NUMBER -- 1,2,3
, psearchword VARCHAR2 -- ๊ฒ์์ด
, pcursor OUT SYS_REFCURSOR
)
IS
vsql VARCHAR2(1000); -- ๋์ ์ฟผ๋ฆฌ ๋ณ์ ์ ์ธ
BEGIN
vsql := 'SELECT * ';
vsql := vsql || ' FROM dept ';
IF psearchCondition = 1 THEN
vsql := vsql || ' WHERE REGEXP_LIKE( dname, :psearchword , ''i'' )';
ELSIF psearchCondition = 2 THEN
vsql := vsql || ' WHERE REGEXP_LIKE( loc, :psearchword, ''i'' )';
ELSIF psearchCondition = 3 THEN
vsql := vsql || ' WHERE REGEXP_LIKE( dname, :psearchword, ''i'' ) OR REGEXP_LIKE( loc, :psearchword, ''i'' )';
END IF;
vsql := vsql || ' ORDER BY deptno ASC ';
-- OPEN~FOR๋ฌธ
IF psearchCondition = 1 THEN
OPEN pcursor FOR vsql
USING psearchword;
ELSE
OPEN pcursor FOR vsql
-- INTO
USING psearchword, psearchword;
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20014, '> DEPT SEARCH X...');
END;
-java-
private static void searchDept() {
// ๊ฒ์์กฐ๊ฑด ์ ํ(์
๋ ฅ) 1) ๋ถ์๋ช
2) ์ง์ญ๋ช
3) ๋ถ์๋ช
+ ์ง์ญ๋ช
// ๊ฒ์์ด
System.out.println("[ ๊ฒ์ ์กฐ๊ฑด ] ");
System.out.println("1) ๋ถ์๋ช
");
System.out.println("2) ์ง์ญ๋ช
");
System.out.println("3) ๋ถ์๋ช
+ ์ง์ญ๋ช
");
System.out.print("> ๊ฒ์์กฐ๊ฑด ์ ํ ? "); // 1, 2, 3
int searchCondition = scanner.nextInt();
System.out.print("> ๊ฒ์์ด ์
๋ ฅ ? ");
String searchWord = scanner.next();
String sql = "{CALL UP_SEARCHDEPT(?,?,?)}";
// selectAllDept ๋ฉ์๋ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ
ArrayList<DeptDTO> deptList = null;
try {
cstmt = conn.prepareCall(sql);
cstmt.setInt(1, searchCondition);
cstmt.setString(2, searchWord);
cstmt.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);
cstmt.executeQuery();
rs = (ResultSet)cstmt.getObject(3);
if( rs.next() ) {
deptList = new ArrayList<DeptDTO>();
do {
DeptDTO dto = new DeptDTO( rs.getInt("deptno") , rs.getString("dname"), rs.getString("loc"));
deptList.add(dto);
} while (rs.next());
} // if
printDept( deptList );
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
} // try
}
}
์ถ๊ฐ๋ฌธ์
1. ๋ถ์๋ฒํธ๋ฅผ ์ ๋ ฅ๋ฐ์์ ํด๋น ๋ถ์์๋ค์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ ์ฝ๋ฉ์ ํ์ธ์.
1) CallableStatement ์ฌ์ฉ 2) UP_SELECTDEPTEMP ์ ์ฅ ํ๋ก์์ ์์ฑ ํ ์ฌ์ฉ. 3) printEmp( List list) ๋ฉ์๋ ์ฌ์ฉํด์ ์ถ๋ ฅ. |
-sql-
CREATE OR REPLACE PROCEDURE UP_SELECTDEPTEMP
(
pdpateno dept.deptno%type
, pcursor OUT SYS_REFCURSOR
)
IS
vsql VARCHAR2(1000);
BEGIN
vsql := 'SELECT * '
vsql := vsql || ' FROM emp ';
vsql := vsql || ' WHERE deptno =: pdeptno ';
-- OPEN FOR ๋ฌธ
OPEN pcursor FOR vsql
-- INTO
USING pdeptno
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20013, '>EMP SELECT X...');
END;
-java-
public static void main(String[] args) {
Connection conn = null;
CallableStatement cstmt = null;
ResultSet rs = null;
ArrayList<EmpDTO> list = null;
String sql = "{CALL UP_SELECTDEPTEMP(?,?)}";
Scanner scanner = new Scanner(System.in);
int deptno = 0;
System.out.println("๋ถ์ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์");
pdeptno = scanner.nextInt();
conn = DBConn.getConnection();
try {
cstmt = conn.prepareCall(sql);
cstmt.setInt(1, pdeptno);
cstmt.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
cstmt.executeQuery();
rs = (ResultSet)cstmt.getObject(2);
printEmp(list);
if( rs.next() ) {
list = new ArrayList<EmpDTO>();
do {
EmpDTO dto = new DeptDTO();
dto.setEmpno(rs.getInt("empno"));
dto.setEname(rs.getString("ename"));
dto.setHiredate(rs.getDate("hiredate"));
dto.setDeptno(rs.getInt("deptno"));
list.add(dto);
} while (rs.next());
} // if
printEmp( list );
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
cstmt.close();
rs.close();
DBConn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} // try
}
private static void printEmp(List<EmpDTO> list) {
Iterator<EmpDTO> ir = list.Iterator();
while(ir.hashNext()){
EmpDTO dto = ir.next();
System.out.println(dto);
}
}
2. ID(empno), PWD(ename) ์ ์ ๋ ฅ๋ฐ์์
๋ก๊ทธ์ธ ์ฑ๊ณต ๋ก๊ทธ์ธ ์คํจ - ID๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค. ๋ก๊ทธ์ธ ์คํจ - ID๋ ์กด์ฌํ์ง๋ง PWD๊ฐ ์ผ์นํ์ง ์์ต๋๋ค. ๋ผ๊ณ ์ถ๋ ฅํ๋ ์ฝ๋ฉ์ ํ์ธ์. 1) CallableStatement ์ฌ์ฉ 2) ์ค๋ผํด ์์ ๋ ์์ฑํ UP_LOGON ์ ์ฅ ํ๋ก์์ ์ฌ์ฉ. |
-sql-
CREATE OR REPLACE PROCEDURE up_logon
(
pempno emp.empno%type
, pename emp.ename%type
, plogonCheck OUT NUMBER -- ์ฑ๊ณต 0 ID X -1 1
)
IS
vename emp.ename%type;
BEGIN
SELECT COUNT(*) INTO plogonCheck -- 1, 0
FROM emp
WHERE empno = pempno;
IF plogonCheck = 1 THEN -- ID ์กด์ฌํ๋ค.
SELECT ename INTO vename
FROM emp
WHERE empno = pempno;
IF vename = pename THEN
plogonCheck := 0;
ELSE
plogonCheck := 1;
END IF;
ELSE
plogonCheck := -1;
END IF;
-- EXCEPTION
END;
-java-
private static void id_Check() {
Connection conn = null;
CallableStatement cstmt = null;
String sql { CALL UP_LOGON((?, ?, ?) };
Scanner scanner = new Scanner(System.in);
System.out.print("ID, PW ์
๋ ฅ");
int pempno = scanner.nextInt();
String pename = scanner.next();
conn = DBConn.getConnection();
try {
cstmt = conn.prepareCall(sql);
cstmt.setInt(1, pempno);
cstmt.setString(2, pename);
cstmt.registerOutParameter(3, oracle.jdbc.OracleTypes.INTEGER);
cstmt.executeQuery();
int idCheck = (int)cstmt.getObject(3);
if(idCheck == 0) {
System.out.println("๋ก๊ทธ์ธ ์ฑ๊ณต");
} else if(idCheck == -1) {
System.out.println("๋ก๊ทธ์ธ ์คํจ, ๋น๋ฐ๋ฒํธ ์ค๋ฅ");
} else if(idCheck == 1){
System.out.println("๋ก๊ทธ์ธ ์คํจ, ์์ด๋ ์์");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
cstmt.close();
DBConn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Reflection
- ResultSet ๊ฒฐ๊ณผ๋ฌผ์ ๋ํ ์ ๋ณด ์ถ์ถํด์ ์ฌ์ฉํ ์ ์๋ ๊ธฐ์
public static void main(String[]args) {
String sql = "SELECT table_name"
+ "FROM tabs"
+ "ORDER BY table_name ASC";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String tableName = null;
conn = DBConn.getConnection();
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
int no = 1;
while(rs.next()) {
tableName = rs.getString(1);
System.out.printf("%d. %s\n", no++, tableName);
} // while
// SCOTT์ด ์์ ํ ๋ชจ๋ ํ
์ด๋ธ ์ถ๋ ฅ
} catch (SQLException e){
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e){
e.printStackTrace();
}
}
System.out.print(">ํ
์ด๋ธ๋ช
์
๋ ฅ");
Scanner scanner = new Scanner(System.in);
tableName = scanner.next();
sql = "SELECT *"
+" FROM + tableName";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
// getMetaData() : rs๊ฐ์ฒด๋ก๋ถํฐ ์ปฌ๋ผ์ ๋ณด(์,์๋ฃํ, ์ปฌ๋ผ๋ช
) ์ป์ด์ค๋ ๋ฉ์๋
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
System.out.println(">์นผ๋ผ๊ฐฏ์:" + columnCount);
String columnName = rsmd.getColumnName(1); // ์ฒซ๋ฒ์งธ ์นผ๋ผ๋ช
System.out.println(">์นผ๋ผ๊ฐฏ์:" + columnCount);
int columnType = rsmd.getColumnType(1);
System.out.println(">์นผ๋ผํ์
:" + columnType);
String columnTypeName = rsmd.getColumnTypeName(1);
System.out.println(">์นผ๋ผํ์
:" + columnTypeName);
int p = rsmd.getPrecision(1);
System.out.println(">์ ๋ฐ๋:" +p);
int s = rsmd.getScale(1);
System.out.println(">์ค์ผ์ผ:" +s);
for(int i=1; i<columnCount; i++) {
String columnName = rsmd.getCountName(i);
System.out.printf("%s\t", columnName);
}
// NUMBER 2(getInt) Scale != 0 (์ค์) (getDouble)
// VARCHAR2 12(
// DATE 93
for(int i=1; i<columnCount; i++) {
int columnType = rsmd.getColumnType(i);
int scale = rsmd.getScale(i);
if(columnType == 2 && scale == 0) {
System.out.printf("%d\t", rs.getInt(i));
} else if(columnType ==2 && scale != 0) {
System.out.printf("%2f\t", rs.getDouble(i));
} else if(columnType == 93) {
System.out.printf("%tf\t", rs.getDate(i));
} else if(columnType ==12) {
System.out.printf("%s\t", rs.getString(i));
}
} catch (SQLException e){
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e){
e.printStackTrace();
}
}
DBConn.close();
System.out.println("END");
'๐จโ๐ป Web Development > JDBC | JSP | Servlet' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day2] JSP/Servlet 2 - db์ฐ๊ฒฐ ํ ์ด๋ธ ๋ฐํ, ์๋ธ๋ฆฟ, request๊ฐ์ฒด (0) | 2022.12.06 |
---|---|
[Day1] JSP/Servlet 1 - ๊ฐ์ (0) | 2022.12.06 |
[Day4] JSP/JDBC 4 - Board (0) | 2022.11.01 |
[Day3] JSP/JDBC 3 - ์ฌ์ฉ์ ๋ณ๊ฒฝ, ์ฌ๋ฌ์ถ๋ ฅ๋ฐฉ์, linkedhashmap์ฌ์ฉ DTO ์ ์ฅ, MVC, JUnit (0) | 2022.10.31 |
[Day2] JSP/JDBC 2 - ๋์ ์ฟผ๋ฆฌ, PreparedStatement (0) | 2022.10.31 |
์ต๊ทผ๋๊ธ