音乐系统的增删改查及JAVA基础知识

一.今天首先讲了JAVA的基础知识
1.数组和集合的区别:
数组大小是固定的 集合不是固定
数组只能放一种类型的数据 集合可以放多种类型的数据
2.list 和 set 的区别
list 有序可重复
set 无序不可以重复
3.list
arraylist 数组list 查询快 删除和添加慢
linkedlist 添加和删除快 查询慢
set
treeset 通过树状保存数据

4.异常和错误
1.运行时异常 可以不处理 jvm虚拟机 处理
2.编译异常 一定要处理

处理异常的2种方式:
1.try catch 自己处理
2.throws 给掉用者处理

分层
1.model 实体类
2.dao 持久层 对数据库操作 增删改查

二.音乐系统的增删改查
先在Dao下新添加两个类,分别命名为MusicDao和Test1
MusicDao的代码如下:
public class MusicDao {
public List findMusics() throws SQLException {
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
List musics=new ArrayList<>();
try {
connection=DBUtil.getConnection();
String sql=“select *from music”;
statement=connection.prepareStatement(sql);
resultSet=statement.executeQuery();
while (resultSet.next()){
Music music=new Music();
music.setId(resultSet.getInt(1));
music.setName(resultSet.getString(2));
music.setAuthor(resultSet.getString(3));
}
}
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 charu(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();
DBUtil.closeAll(null,statement,connection);
}
public void updateMusic(int id,String newname) throws SQLException, ClassNotFoundException {
Connection connection=DBUtil.getConnection();
String sql=“update music set name=? where id=?”;
PreparedStatement statement=connection.prepareStatement(sql);
statement.setString(1,newname);
statement.setInt(2,id);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
}
}
然后在Test1中实现增删改查
public class Test1 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
MusicDao musicDao=new MusicDao();
List musics=musicDao.findMusics();
System.out.println(musics);
Scanner input=new Scanner(System.in);
// System.out.println(“请输入你想添加的歌名:”);
// String name=input.next();
// System.out.println(“请输入这首歌的歌手:”);
// String author=input.next();
// System.out.println(“请输入你想修改的歌的id:”);
// int ids=input.nextInt();
// System.out.println(“请输入你想修改歌的歌名:”);
// String names=input.next();
System.out.println(“请输入你想删除歌的id:”);
int id=input.nextInt();
// musicDao.charu(name,author);
// musicDao.updateMusic(ids,names);
musicDao.delete(id);
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值