Code example for PreparedStatement

Methods: executesetBlobsetIntsetStringclose

0
	} 
 
	public void closeConnections(PreparedStatement st,Connection conn){
		try { 
			if (st != null) {
                st.close();
            }if(conn !=null){
            	conn.close();
            } 
		} catch (Exception e2) {
			e2.printStackTrace();
		} 
	} 
	 
	public void addAvImage(Blob imageData, int imageType, String imageLink, int userId, int avId){
		try {	 
			conn = DbConnection.GetConnection();
			st = conn.prepareStatement("Insert into AVIMAGE (ImageData, ImageType, ImageLink, UserId, av_Id) values (?, ?, ?, ?, ?)");
			st.setBlob(1, imageData);
			st.setInt(2, imageType);
			st.setString(3, imageLink);
			st.setInt(4, userId);
			st.setInt(5, avId);
			st.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{ 
			closeConnections(st, conn);
		} 
	} 
	 
	public AvImage getAvImageById(int id){
		AvImage avImage = null;
		try { 
			conn = DbConnection.GetConnection();
			st = conn.prepareStatement("Select * from AVIMAGE where ID = ?");
	        st.setInt(1,id);
	        rs = st.executeQuery();
	        if (rs.next()) {
	        	avImage = new AvImage();		
	        	avImage.setId(rs.getInt(1));
	        	avImage.setImageData(rs.getBlob(2));
	        	avImage.setImageType(rs.getInt(3));
	        	avImage.setImageLink(rs.getString(4));
	        	avImage.setUserId(rs.getInt(5));
	        	avImage.setAvId(rs.getInt(6));
            }			 
		} catch (Exception e) {
			e.printStackTrace();
		}finally{ 
			closeConnections(st, conn);
		} 
		return avImage;
	} 
 
	 
	public void deleteAvImage(int id){
		try { 
			conn = DbConnection.GetConnection();
			st = conn.prepareStatement("delete from AVIMAGE where id = ?");
			st.setInt(1, id);
			st.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{ 
			closeConnections(st, conn);
		} 
	} 
	 
	public void updateAvImage(AvImage avImage){
		try { 
			conn = DbConnection.GetConnection();
			st = conn.prepareStatement("UPDATE AVIMAGE SET imageData = ?, ImageType = ?, ImageLink = ?, UserId = ?, av_Id = ? WHERE id = ?");
			st.setBlob(1, avImage.getImageData());
			st.setInt(2, avImage.getImageType());
			st.setString(3, avImage.getImageLink());
			st.setInt(4, avImage.getUserId());
			st.setInt(5, avImage.getAvId());
			st.setInt(6, avImage.getId());
			st.execute();			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{ 
			closeConnections(st, conn);
		} 
Connect your IDE to all the code out there  Get Codota for Java