티스토리 뷰

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

 

public class JdbcInsertTest {

        public static void main(String[] args) {

              

               Connection conn = null;

               PreparedStatement pstmt = null;      

              

               try {

                      Class.forName("oracle.jdbc.driver.OracleDriver");

                       conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");

                      

                       String query = "insert into dept2 values(?, ?, ?)";

                       pstmt = conn.prepareStatement(query);

  

                         //pstmt.set~은 conn.prepareStatement(); 다음에 들어와야한다

                       pstmt.setInt(1, 50);

                       pstmt.setString(2, "총무부");

                       pstmt.setString(3, "서울");

                      

                       int count = pstmt.executeUpdate();

                       System.out.println("영향받은 행수 : " + count);

                      

               } catch (ClassNotFoundException e) {

                       e.printStackTrace();

               } catch (SQLException e) {

                       e.printStackTrace();

               } finally{

                       if(pstmt != null) try{ pstmt.close();} catch(SQLException e){};

                       if(conn != null) try{ conn.close();} catch(SQLException e){};

               }

 

        }

}


출처: http://whdvy777.tistory.com/entry/JDBC-preparedStatementinsert-%EC%8B%A4%EC%8A%B5


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함