Addition, deletion and alteration of music management

Posted by Restless on Fri, 04 Oct 2019 02:49:35 +0200

Exceptions and errors

1. Runtime exceptions can be handled without jvm virtual machine processing
2. Compile exceptions must be handled

Two ways to handle exceptions:
1.try catch handles it by itself
2.throws are handled by the user

Custom exception

Entity classes: with database objects
1. Construction Method
2. attribute
3.get set
4.toString

Layered
1.model entity class
2.dao persistence layer adds, deletes and modifies database operations

On the basis of yesterday, a new MusicDao file is created. The code is as follows:

package com.zhongruan.dao;

import com.zhongruan.Util.DBUtil;
import com.zhongruan.model.Music;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MusicDao {
    public List<Music> findMusic(){
        ResultSet resultSet=null;
        PreparedStatement statement=null;
        Connection connection=null;
        List<Music> musics=new ArrayList<>();

        try {
            connection= DBUtil.getConnection();
            String sql="select * from music";
            //4. Get the statement
            statement=connection.prepareStatement(sql);
            //5. implementation of sql
            resultSet=statement.executeQuery();
            //6. Processing result sets
            while (resultSet.next()){
                Music music=new Music();
                music.setId(resultSet.getInt(1));
                music.setName(resultSet.getString(2));
                music.setAuthor(resultSet.getString(3));
                musics.add(music);
            }
            //7. Closing resources
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.closeAll(resultSet,statement,connection);
        }
        return musics;
    }
    public void  delete(int id) throws SQLException,ClassNotFoundException {
        Connection connection = DBUtil.getConnection();
        String sql="delete from music where id=?";
        PreparedStatement statement =connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.executeUpdate();
        DBUtil.closeAll(null,statement,connection);
    }

    public void  update(int id) throws SQLException,ClassNotFoundException {
        Connection connection = DBUtil.getConnection();
        String sql="update music set name ='disaster',author='yyqx' where id=?";
        PreparedStatement statement =connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.executeUpdate();
        DBUtil.closeAll(null,statement,connection);
    }
    public void  insert(String name,String author) throws SQLException,ClassNotFoundException {
        Connection connection = DBUtil.getConnection();
        String sql="insert into music(NAME,author) VALUES (?,?) ";
        PreparedStatement statement =connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setString(2,author);
        statement.executeUpdate();

    }
}

Then the code of Test is changed, and the results are as follows:

package com.zhongruan;

import com.zhongruan.Util.DBUtil;
import com.zhongruan.dao.MusicDao;
import com.zhongruan.model.Music;

import java.awt.*;
import java.awt.image.DirectColorModel;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        MusicDao musicDao=new MusicDao();
        System.out.println("Please enter the title of the song.");
        Scanner input=new Scanner(System.in);
        String name=input.next();
        System.out.println("Please enter the author");
        String author=input.next();
        musicDao.insert(name,author);
        List<Music> musics=musicDao.findMusic();
        System.out.println(musics);
        System.out.println("Enter the music you want to delete id: ");
        try {
            musicDao.update(3);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
}

Topics: SQL Java Database jvm