Java - 데이터 입력하기
select 가 아닐경우 결과집합이 없다. select문장은 Query 를 이용해서 ResultSet을 얻지만, insert, update, delete 조작을 할때는 Update를 사용한다.
- executeQuery (결과집합이 있는경우)
- executeUpdate( 결과집합이 없는경우 ) ->몇개가 결과로 받았는지 알려줌
prepareStatement : 실행하기전에 채워넣은 문장을 만들어주는 역할을 한다 (미리준비를한다.) prepareStatement문을 실행할때 sql을 전달하지않느다. 이미 statement가 sql을 가지고있기때문이다.
public class Program2 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String title = "TEST2";
String writerId ="newlec";
String content ="hahaha";
String files = "";
String url = "jdbc:oracle:thin:@localhost:1521/xe";
String sql = "INSERT INTO NOTICE(TITLE,WRITER_ID, CONTENT, FILES) VALUES ( ?,?,?,?)";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url,"lec","0000");
PreparedStatement st = con.prepareStatement(sql);
st.setString(1, title);
st.setString(2, writerId);
st.setString(3, content);
st.setString(4, files);
int result = st.executeUpdate();
System.out.println(result);
st.close();
con.close();
}
}
1이 잘 출력된다.
댓글남기기