package com.ktv.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.ktv.pojo.Song;
public class SongDao {
public static List<Song> SELECTED_SONGS=new ArrayList<Song>();
public boolean checkSongName(Song s){
Connection conn=null;
boolean r=false;
try {
conn=ConnectionUtil.getConnection();
String sql="select * from t_song where title=?";
PreparedStatement pre=conn.prepareStatement(sql);
pre.setString(1,s.getTitle());
ResultSet rs=pre.executeQuery();
if(!rs.next()){
r=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return r;
}
public boolean checkFavSong(int userId,int songId){
Connection conn=null;
boolean r=false;
try {
conn=ConnectionUtil.getConnection();
String sql="select * from t_user_song where user_id=? and song_id=?";
PreparedStatement pre=conn.prepareStatement(sql);
pre.setInt(1,userId);
pre.setInt(2,songId);
ResultSet rs=pre.executeQuery();
if(rs.next()){
r=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return r;
}
//点歌
public void addSong(int id){
if(!SELECTED_SONGS.contains(findBySongId(id))){
SELECTED_SONGS.add(findBySongId(id));
}
}
//根据ID删除已点歌曲
public void delSongById(int id){
SELECTED_SONGS.remove(findBySongId(id));
}
//清除已点歌曲
public void clearSongs(){
SELECTED_SONGS.clear();
}
public static List<Song> getSELECTED_SONGS() {
return SELECTED_SONGS;
}
public static void setSELECTED_SONGS(List<Song> sELECTEDSONGS) {
SELECTED_SONGS = sELECTEDSONGS;
}
public boolean saveSong(Song s){
Connection conn=null;
int rs=0;
try {
conn=ConnectionUtil.getConnection();
String sql="insert into t_song(title,singer,class,language,src) values(?,?,?,?,?)";
PreparedStatement pre=conn.prepareStatement(sql);
pre.setString(1,s.getTitle());
pre.setString(2,s.getSinger());
pre.setInt(3,s.getClassTypeId());
pre.setInt(4,s.getLanguageId());
pre.setString(5,s.getSrc());
rs=pre.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return rs>0?true:false;
}
public int updateSongInfo(Song s){
int r=0;
Connection conn=null;
String sql="update t_song set title=?,singer=?,class=?,language=? where id=?";
try {
conn=ConnectionUtil.getConnection();
PreparedStatement pre=conn.prepareStatement(sql);
pre.setString(1,s.getTitle());
pre.setString(2,s.getSinger());
pre.setInt(3,s.getClassTypeId());
pre.setInt(4,s.getLanguageId());
pre.setInt(5,s.getId());
r=pre.executeUpdate();
if(r>0){
r=4;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return r;
}
public int delPersonalSong(int userId,int songId){
Connection conn=null;
int rs=0;
try {
conn=ConnectionUtil.getConnection();
String sql="delete from t_user_song where user_id=? and song_id=?";
PreparedStatement pre=conn.prepareStatement(sql);
pre.setInt(1,userId);
pre.setInt(2,songId);
rs=pre.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return rs>0?4:-1;
}
public int addPersonalSong(int userId,int songId){
Connection conn=null;
int rs=0;
try {
conn=ConnectionUtil.getConnection();
if(checkFavSong(userId,songId)){
return 0;
}
String sql="insert into t_user_song(user_id,song_id) values(?,?)";
PreparedStatement pre=conn.prepareStatement(sql);
pre.setInt(1,userId);
pre.setInt(2,songId);
rs=pre.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return rs>0?4:-1;
}
public List<Song> getAllSongs(int id){
Connection conn=null;
List<Song> songs=new ArrayList<Song>();
try {
conn=ConnectionUtil.getConnection();
String sql="select * from t_song";
PreparedStatement pre=conn.prepareStatement(sql);
ResultSet rs=pre.executeQuery();
while(rs.next()){
Song song=new Song();
song.setId(rs.getInt(1));
song.setTitle(rs.getString(2));
song.setSinger(rs.getString(3));
if(rs.getInt(4)==1){
song.setClassType("轻音乐");
}else if(rs.getInt(4)==2){
song.setClassType("摇滚");
}else if(rs.getInt(4)==3){
song.setClassType("卡农");
}else{
song.setClassType("其他");
}
song.setClassTypeId(rs.getInt(4));
if(rs.getInt(5)==1){
song.setLanguage("中文");
}else if(rs.getInt(5)==2){
song.setLanguage("粤语");
}else if(rs.getInt(5)==3){
song.setLanguage("外文");
}else{
song.setLanguage("其他");
}
song.setLanguageId(rs.getInt(5));
song.setSrc(rs.getString(6));
songs.add(song);
List<Song> favSongs=new ArrayList<Song>();
favSongs=findMySongs(id);
for(Song s:songs){
if(SELECTED_SONGS.contains(s)){
s.setSelected(true);
}
if(favSongs.contains(s)){
s.setFav(true);
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return songs;
}
public List<Song> findMySongs(Integer id){
Connection conn=null;
List<Song> songs=new ArrayList<Song>();
try {
conn=ConnectionUtil.getConnection();
String sql="select * from t_user_song us left join t_song s on us.song_id=s.id where us.user_id=?";
PreparedStatement pre=conn.prepareStatement(sql);
pre.setInt(1,id);
ResultSet rs=pre.executeQuery();
while(rs.next()){
Song song=new Song();
song.setId(rs.getInt(4));
song.setTitle(rs.getString(5));
song.setSinger(rs.getString(6));
if(rs.getInt(7)==1){
song.setClassType("轻音乐");
}else if(rs.getInt(7)==2){
song.setClassType("摇滚");
}else if(rs.getInt(7)==3){
song.setClassType("卡农");
}else{
song.setClassType("其他");
}
song.setClassTypeId(rs.getInt(7));
if(rs.getInt(8)==1){
song.setLanguage("中文");
}else if(rs.getInt(8)==2){
song.setLanguage("粤语");
}else if(rs.getInt(8)==3){
song.setLanguage("外文");
}else{
song.setLanguage("其他");
}
song.setLanguageId(rs.getInt(8));
song.setSrc(rs.getString(9));
songs.add(song);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return songs;
}
public Song findBySongId(Integer id){
Connection conn=null;
Song song=new Song();
try {
conn=ConnectionUtil.getConnection();
String sql="select * from t_song where id=?";
PreparedStatement pre=conn.prepareStatement(sql);
pre.setInt(1,id);
ResultSet rs=pre.executeQuery();
if(rs.next()){
song.setId(rs.getInt(
- 1
- 2
- 3
前往页