Code example for PreparedStatement

Methods: execute, setLong, setString

0
public class SessionDao { 
 
	public void addSession(Connection conn, int userId, String createTime) throws SQLException {
		String sql = "insert into SessionUser (userId, time) value (?,?) ON DUPLICATE KEY UPDATE time=?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setLong(1, userId);
		ps.setString(2, createTime);
		ps.setString(3, createTime);
		ps.execute();
	} 
	 
	public void remove(Connection conn, int userId) throws SQLException {
		String sql = "delete from SessionUser where userId=" + userId;
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.execute();
	} 
	 
	public void clear(Connection conn) throws SQLException {
		String sql = "truncate table SessionUser";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.execute();