(오라클+자바) 데이타베이스에 데이타 올리기 - 03
(오라클+자바) 데이타베이스에 데이타 올리기 - 01
(오라클+자바) 데이타베이스에 데이타 올리기 - 02
이제 드디어 기다리던 자바와 오라클을 연동해서 하는 데이타베이스에서 데이타를 올리는 방법이다.
이 방법은 나도 솔직하게 이해를 못해서 그냥 먼저 예제를 올려줄려고 한다...
테이블에 데이타를 넣는것 -> insert
테이블에 이미 있는 데이타에 수정을 하는것 -> update
테이블에 이미 있는 데이타에 삭제를 하는것 ->delete
insert 먼저 적어보자
package dbcoonect2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
publicclass Insertuser {
public static void main(String[] args) {
Connection
con =null;
Statement
stmt =null;
ResultSet
rs = null;
//preparestatment->bring data
//result set -> container for the data
//result set에 들어갈수 있는것들 -> multiple column, row and single column ,row (4 of them)
//
//preparestatment->bring data
//result set -> container for the data
//result set에 들어갈수 있는것들 -> multiple column, row and single column ,row (4 of them)
//
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.5:1521:xe", "lucky", "lucky");
stmt = con.createStatement();
stmt.executeUpdate("insert into users(us_id,
us_pwd, us_name) values('sibaldog', 'sibaldog','sisisi')");
System.out.println("The data has been
entered");
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
}finally {
if(rs != null) {try {rs.close();} catch (SQLException e) {} finally {} }
if(con != null) {try {con.close();} catch (SQLException e) {}finally {} }
if(stmt != null) {try {stmt.close();} catch (SQLException e) {}finally {} }
}
}
}
삭제
package dbcoonect2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
publicclass DeleteUser {
publicstaticvoid main(String[] args) {
Connection
con =null;
Statement
stmt =null;
ResultSet
rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.5:1521:xe", "lucky", "lucky");
stmt = con.createStatement();
stmt.executeUpdate("delete from users where
us_id='sibaldog'");
System.out.println("The data has been
deleted");
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
}finally {
if(rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}finally {} }
if(con != null) {try {con.close();} catch (SQLException e) {e.printStackTrace();}finally {} }
if(stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}finally {} }
}
}
}
수정
package dbcoonect2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
publicclass UpdateUser {
publicstaticvoid main(String[] args) {
Connection
con =null;
Statement
stmt =null;
ResultSet
rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.5:1521:xe", "lucky", "lucky");
stmt = con.createStatement();
intresult = stmt.executeUpdate("update users set us_id='l4'
where us_id='lucky1id'");
System.out.println(result + " The data has been
updated");
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
}finally {
if(rs != null) {try {rs.close();} catch (SQLException e) {} finally {} }
if(con != null) {try {con.close();} catch (SQLException e) {}finally {} }
if(stmt != null) {try {stmt.close();} catch (SQLException e) {}finally {} }
}
}
}
select 데이타
package dbcoonect2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
publicclass SelectUser {
publicstaticvoid main(String[] args) {
Connection
con =null;
Statement
stmt =null;
ResultSet
rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.5:1521:xe", "lucky", "lucky");
stmt = con.createStatement();
rs = stmt.executeQuery("select us_id,
us_pwd,us_name, us_level from users");
intcnt = 0;
while(rs.next()) {
cnt++;
String
id = rs.getString("us_id");
String
pwd = rs.getString("us_pwd");
String
name = rs.getString("us_name");
intlvl = rs.getInt("us_level");
System.out.println(id+" " + pwd + " " +name + " " + lvl);
}
System.out.println( cnt +"개의레코드가조회되었습다 ");
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
}finally {
if(rs != null) {try {rs.close();} catch (SQLException e) {} finally {} }
if(con != null) {try {con.close();} catch (SQLException e) {}finally {} }
if(stmt != null) {try {stmt.close();} catch (SQLException e) {}finally {} }
}
}
}
selectCnt
//CRUD작업의 메서드들을 정의해준다.(selectCnt작업)
public int selectCnt() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int dataCnt = 0;
try {
con = DriverManager.getConnection(url, uid, pwd);
pstmt = con.prepareStatement("select count(*) as cnt from epm2");
rs = pstmt.executeQuery();
while(rs.next()) {
dataCnt = rs.getInt("cnt");
}
}catch(SQLException se) {
se.printStackTrace();
}finally {
if(pstmt != null) {
try {pstmt.close();}catch(SQLException se) {}finally {}
}
if(con != null) {
try {con.close();}catch(SQLException se) {}finally {}
}
if(rs != null) {
try {rs.close();}catch(SQLException se) {}finally {}
}
}
return dataCnt;
}
}
selectAll
public List<Epm2VO> selectAll() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Epm2VO> list = null;
try {
con = DriverManager.getConnection(url, uid, pwd);
pstmt = con.prepareStatement("select * from epm2");
rs = pstmt.executeQuery();
while(rs.next()) {
Epm2VO vo = new Epm2VO();
vo.setEpmId(rs.getInt("epm_id"));
vo.setIrum(rs.getString("irum"));
vo.setEmail(rs.getString("email"));
vo.setPhone(rs.getString("phone"));
vo.setHiDate(rs.getString("hidate"));
vo.setPay(rs.getLong("pay"));
list.add(vo);
}
}catch(SQLException se) {
se.printStackTrace();
}finally {
if(pstmt != null) {
try {pstmt.close();}catch(SQLException se) {}finally {}
}
if(con != null) {
try {con.close();}catch(SQLException se) {}finally {}
}
if(rs != null) {
try {rs.close();}catch(SQLException se) {}finally {}
}
}
return list;
}
selectCnt
//CRUD작업의 메서드들을 정의해준다.(selectCnt작업)
public int selectCnt() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int dataCnt = 0;
try {
con = DriverManager.getConnection(url, uid, pwd);
pstmt = con.prepareStatement("select count(*) as cnt from epm2");
rs = pstmt.executeQuery();
while(rs.next()) {
dataCnt = rs.getInt("cnt");
}
}catch(SQLException se) {
se.printStackTrace();
}finally {
if(pstmt != null) {
try {pstmt.close();}catch(SQLException se) {}finally {}
}
if(con != null) {
try {con.close();}catch(SQLException se) {}finally {}
}
if(rs != null) {
try {rs.close();}catch(SQLException se) {}finally {}
}
}
return dataCnt;
}
}
selectAll
public List<Epm2VO> selectAll() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Epm2VO> list = null;
try {
con = DriverManager.getConnection(url, uid, pwd);
pstmt = con.prepareStatement("select * from epm2");
rs = pstmt.executeQuery();
while(rs.next()) {
Epm2VO vo = new Epm2VO();
vo.setEpmId(rs.getInt("epm_id"));
vo.setIrum(rs.getString("irum"));
vo.setEmail(rs.getString("email"));
vo.setPhone(rs.getString("phone"));
vo.setHiDate(rs.getString("hidate"));
vo.setPay(rs.getLong("pay"));
list.add(vo);
}
}catch(SQLException se) {
se.printStackTrace();
}finally {
if(pstmt != null) {
try {pstmt.close();}catch(SQLException se) {}finally {}
}
if(con != null) {
try {con.close();}catch(SQLException se) {}finally {}
}
if(rs != null) {
try {rs.close();}catch(SQLException se) {}finally {}
}
}
return list;
}
select function and process of select in oracle.
1. DB connection
2. run a command(prepare for query)
3. After operating query-> it brings the selected result
4. It returns the selected result to the client.
댓글
댓글 쓰기