用Java还有mysql编写一个图书管理系统 源代码
时间: 2023-07-18 08:10:48 浏览: 151
很抱歉,由于篇幅限制,我不能在这里提供完整的Java源代码。但是,我可以为您提供一个简单的Java源代码框架,供您参考。
1. 数据库连接类
```
import java.sql.*;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/library";
private static final String USER = "root";
private static final String PASSWORD = "123456";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void closeConnection(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
2. Book类
```
public class Book {
private int id;
private String name;
private String author;
private double price;
private String publish;
private String summary;
public Book() {}
public Book(int id, String name, String author, double price, String publish, String summary) {
this.id = id;
this.name = name;
this.author = author;
this.price = price;
this.publish = publish;
this.summary = summary;
}
// setter and getter methods
}
```
3. User类
```
public class User {
private int id;
private String username;
private String password;
private String email;
private String phone;
public User() {}
public User(int id, String username, String password, String email, String phone) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.phone = phone;
}
// setter and getter methods
}
```
4. BookDao类
```
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BookDao {
public List<Book> getAllBooks() {
List<Book> bookList = new ArrayList<>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
stmt = conn.createStatement();
String sql = "SELECT * FROM book";
rs = stmt.executeQuery(sql);
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getDouble("price"));
book.setPublish(rs.getString("publish"));
book.setSummary(rs.getString("summary"));
bookList.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(conn, stmt, rs);
}
return bookList;
}
public void addBook(Book book) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO book(name, author, price, publish, summary) VALUES (?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, book.getName());
pstmt.setString(2, book.getAuthor());
pstmt.setDouble(3, book.getPrice());
pstmt.setString(4, book.getPublish());
pstmt.setString(5, book.getSummary());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(conn, pstmt, null);
}
}
public void updateBook(Book book) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE book SET name=?, author=?, price=?, publish=?, summary=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, book.getName());
pstmt.setString(2, book.getAuthor());
pstmt.setDouble(3, book.getPrice());
pstmt.setString(4, book.getPublish());
pstmt.setString(5, book.getSummary());
pstmt.setInt(6, book.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(conn, pstmt, null);
}
}
public void deleteBook(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM book WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(conn, pstmt, null);
}
}
}
```
5. UserDao类
```
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
public User getUserByUsernameAndPassword(String username, String password) {
User user = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM user WHERE username=? AND password=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
user.setPhone(rs.getString("phone"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(conn, pstmt, rs);
}
return user;
}
public void addUser(User user) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO user(username, password, email, phone) VALUES (?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getEmail());
pstmt.setString(4, user.getPhone());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(conn, pstmt, null);
}
}
}
```
希望这个简单的Java源代码框架能够对您有所帮助!
阅读全文