list 和 set 的区别
1.list 有序可重复
2.set 无序不可以重复
list
arraylist 数组list 查询快 删除和添加慢
linkedlist 添加和删除快 查询慢
处理异常的2种方式:
1.try catch 自己处理
2.throws 给掉用者处理
书籍数据库管理系统
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();
}
}
}
}
Book
package com.zhongruan.model;
public class Book {
private int id;
private String bookname;
private String author;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return bookname;
}
public void setName(String name) {
this.bookname = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "BookDao{" +
"id=" + id +
", bookname='" + bookname + '\'' +
", author='" + author + '\'' +
'}';
}
}
User
package com.zhongruan.model;
public class User {
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User() {
}
private int id;
private String username;
private String password;
private int type;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", type=" + type +
'}';
}
}
BookDao
package com.zhongruan.dao;
import com.zhongruan.model.Book;
import com.zhongruan.util.DBUtil;
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 BookDao {
public List<Book> findBooks(){
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
List<Book> books=new ArrayList<>();
try {
connection= DBUtil.getConnection();
//3.写sql
String sql="select * from book";
//4.得到statement对象
statement=connection.prepareStatement(sql);
//5.执行sql
resultSet = statement.executeQuery();
//6.处理结果集
while (resultSet.next()){
Book book=new Book();
book.setId(resultSet.getInt(1));
book.setName(resultSet.getString(2));
book.setAuthor(resultSet.getString(3));
books.add(book);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//7.关闭资源
DBUtil.closeAll(resultSet,statement,connection);
}
return books;
}
public void delete(int id) throws SQLException, ClassNotFoundException {
Connection connection= DBUtil.getConnection();
String sql="delete from book where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
}
public void charu(String bookname,String author){
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
try {
connection= DBUtil.getConnection();
//3.写sql
String sql="insert into book (bookname,author) values (?,?)";
//4.得到statement对象
statement=connection.prepareStatement(sql);
statement.setString(1,bookname);
statement.setString(2,author);
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
//7.关闭资源
DBUtil.closeAll(resultSet,statement,connection);
}
}
public static void updateBook(int id,String newname,String author) throws SQLException, ClassNotFoundException {
Connection connection=DBUtil.getConnection();
String sql="update book set bookname=? ,author=? where id=?";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setString(1,newname);
statement.setString(2,author);
statement.setInt(3,id);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
}
}
UserDao
package com.zhongruan.dao;
import com.zhongruan.model.User;
import com.zhongruan.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDao {
public User findUserByUsername(String username){
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
User user=null;
try {
connection= DBUtil.getConnection();
//3.写sql
String sql=“select * from tb_user where username=?”;
//4.得到statement对象
statement=connection.prepareStatement(sql);
statement.setString(1,username);
//5.执行sql
resultSet = statement.executeQuery();
//6.处理结果集
while (resultSet.next()){
user=new User();
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
user.setType(resultSet.getInt(4));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//7.关闭资源
DBUtil.closeAll(resultSet,statement,connection);
}
return user;
}
public void charu(String username,String password){
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
try {
connection= DBUtil.getConnection();
//3.写sql
String sql=“insert into tb_user (username,password) values (?,?)”;
//4.得到statement对象
statement=connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
//7.关闭资源
DBUtil.closeAll(resultSet,statement,connection);
}
}
}
LoginException
package com.zhongruan.exception;
public class LoginException extends Exception{
public LoginException(String message) {
super(message);
}
}
View(登陆以及增删改查)
package com.zhongruan;
import com.zhongruan.dao.BookDao;
import com.zhongruan.dao.UserDao;
import com.zhongruan.exception.LoginException;
import com.zhongruan.model.Book;
import com.zhongruan.model.User;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public class View {
public static void main(String[] args) throws LoginException, SQLException, ClassNotFoundException {
boolean flag=true;
while (flag){
Scanner input=new Scanner(System.in);
System.out.println("-----------您尚未登入,请先登入--------");
System.out.println("-----------请输入用户名:--------------");
String username=input.next();
System.out.println("------------请输入密码:---------------");
String password=input.next();
//1.用户名没有 提示注册
//2.密码错误
//3.登入成功
//通过用户名来数据库查询,如果有数据 看密码是否一样
UserDao userDao=new UserDao();
User user = userDao.findUserByUsername(username);
if (user==null){
System.out.println("------该用户尚未注册,请先注册------");
System.out.println("-------请输入注册名:---------------");
String name=input.next();
System.out.println("-------请输入注册密码:-------");
String pw=input.next();
userDao.charu(name,pw);
}else if(user.getPassword().equals(password)){
System.out.println("----欢迎来到书籍管理系统--------");
bookSystem(user);
flag=false;
}else {
// System.out.println("------密码错误,请重新输入--------");
throw new LoginException("登入失败");
}
}
}
public static void bookSystem(User user) throws SQLException, ClassNotFoundException {
Scanner input =new Scanner(System.in);
System.out.println("1.书籍查询 2.书籍添加 3.书籍修改 4.书籍删除 5.退出");
int choice=input.nextInt();
BookDao bookDao=new BookDao();
switch (choice){
case 1:
//1.音乐查询
List<Book> books = bookDao.findBooks();
System.out.println(books);
break;
case 2:
if (user.getType()!=0){
System.out.println("权限不足,请联系管理员");
break;
}
System.out.println("请输入要插入的音乐名:");
String ym=input.next();
System.out.println("请输入插入的作者名:");
String au=input.next();
bookDao.charu(ym,au);
break;
case 3:
System.out.println("请输入要删除的id:");
int id=input.nextInt();
bookDao.delete(id);
break;
case 4:
System.out.println("请输入要修改的id");
int id1=input.nextInt();
System.out.println("请输入新的音乐名");
String newym=input.next();
System.out.println("请输入新的作者名:");
String newau=input.next();
BookDao.updateBook(id1,newym,newau);
break;
case 5:
System.exit(0);
}
}
}
改test
package com.zhongruan;
import com.zhongruan.dao.BookDao;
import com.zhongruan.model.Book;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
BookDao bookDao=new BookDao();
// System.out.println(“请输入书籍名:”);
Scanner input=new Scanner(System.in);
// String bookname=input.next();
// System.out.println(“请输入作者:”);
// String author=input.next();
// bookDao.charu(bookname,author);
// List books = bookDao.findBooks();
// System.out.println(books);
// System.out.println(“请输入你要删除的书籍id:”);
// int id=input.nextInt();
System.out.println("请输入你要修改的ID");
int id1=input.nextInt();
String huche=input.nextLine();
System.out.println("请输入新的书籍名");
String name1=input.nextLine();
System.out.println("请输入新的作者");
String author1=input.nextLine();
BookDao.updateBook(id1,name1,author1);
List<Book> books = bookDao.findBooks();
System.out.println(books);
// try {
// bookDao.delete(id);
// } catch (SQLException e) {
// e.printStackTrace();
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
System.out.println(“请输入用户名:”);
}
}