ArrayList和LinkedList的区别:ArrayList在每个位置存储了元素,而LinkedList存储了元素+前面节点+后面节点。ArrayList为数组列表,LinkedList为链表。Arraylist查询快,删除和添加慢。Linkedlist添加和删除快,查询慢。
list和set的区别:
1.list有序可重复
2.set无序不可重复
异常和错误
运行时异常可以不处理由运行环境处理
编译异常必须处理
音乐系统
Test:
package com.zhongruan;
import com.zhongruan.dao.MusicDao;
import com.zhongruan.model.Music;
import com.zhongruan.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
MusicDao musicDao=new MusicDao();
List musics=musicDao.findMusics();
System.out.println(musics);
try {
musicDao.delete(2);
} catch (SQLException e) {
e.printStackTrace();
}
try {
musicDao.zj();
} catch (SQLException e) {
e.printStackTrace();
}
try {
musicDao.xg();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
music:
package com.zhongruan.model;
public class Music {
private int id;
private String name;
private String author;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Music{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}
}
为简便与避免输出,以上可用Generate 中的getter and setting快速得到@Override以前的代码,以后的可用Generate中的Constructer来得到。
夹包DBUtil:
package com.zhongruan.util;
import java.sql.*;
public class DBUtil {
public static Connection getConnection() throws ClassNotFoundException, SQLException{
//1.加载驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.创建连接
Connection connection= DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/zjgm?useSSL=true&characterEncoding=utf-8&user=root&password=123456”);
return connection;
}
public static void closeAll(ResultSet resultSet,Statement statement,Connection connection){
if (resultSet!=null){
try {
resultSet.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
if (statement!=null){
try {
statement.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try{
connection.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
Testzsg:
package com.zhongruan;
import com.zhongruan.model.Music;
import com.zhongruan.util.DBUtil;
import java.sql.*;
import java.util.List;
public class Testzsg {
public static void main(String[] args) throws SQLException {
PreparedStatement statement = null;
Connection connection = null;
try {
connection = DBUtil.getConnection();
// 增
String sql = “insert into music values (‘2’,‘Loser’,‘米津玄师’)”;
// 改
// String sql= “update music set name=‘Lemon’ where id=‘2’”;
// 删
// String sql= “delete from music where id=‘2’”;
statement = connection.prepareStatement(sql);
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(null, statement, connection);
}
}
}
音乐系统增删改查