Statement를 사용하지 말고 PreparedStatement를 사용하라고 권장 하더라..
아래는 예제
public void add(User user) throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test", "sa", "pw!");
PreparedStatement ps = c.prepareStatement("insert into users(id, name, password) values(?,?,?)");
ps.setString(1, user.getId());
ps.setString(2, user.getName());
ps.setString(3, user.getPassword());
ps.executeUpdate();
ps.close();
c.close();
}
public User get(String id) throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test", "sa", "pw!");
PreparedStatement ps = c.prepareStatement("select * from users where id = ?");
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
rs.next();
User user = new User();
user.setId(rs.getString("id"));
user.setId(rs.getString("name"));
user.setId(rs.getString("password"));
rs.close();
ps.close();
c.close();
return user;
}
'JAVA' 카테고리의 다른 글
[JAVA-POI] 엑셀 wirte 하기 (0) | 2014.01.29 |
---|---|
IP찾기 (0) | 2013.02.26 |
Java - 자주사용하는 Collection객체들의 특징 (0) | 2013.02.08 |
[JAVA] 프로세스 리스트 출력 - 확인해보기 (0) | 2013.02.08 |
[JAVA] String의 날짜를 date로 변환 (0) | 2013.01.25 |