欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

java基于mysql數(shù)據(jù)庫設(shè)計

錢瀠龍1年前10瀏覽0評論

Java基于MySQL數(shù)據(jù)庫設(shè)計是一種常見的應(yīng)用開發(fā)方式。MySQL是一種開源的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它被廣泛應(yīng)用于各種應(yīng)用程序中。

使用Java語言開發(fā)MySQL數(shù)據(jù)庫程序通常涉及以下步驟:

1. 創(chuàng)建一個用于連接數(shù)據(jù)庫的Java類
2. 創(chuàng)建一個用于操作MySQL數(shù)據(jù)庫表的Java類
3. 編寫SQL語句,實現(xiàn)數(shù)據(jù)的增加、刪除、修改、查詢等操作

以下是示例代碼:

public class DatabaseConnection {
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
private Connection connection = null;
public DatabaseConnection() throws ClassNotFoundException {
Class.forName(DRIVER);
}
public Connection getConnection() throws SQLException {
if (connection == null) {
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
return connection;
}
public void closeConnection() throws SQLException {
if (connection != null) {
connection.close();
}
}
}
public class UserDAO {
private static final String TABLE_NAME = "user";
private static final String INSERT_SQL = "INSERT INTO " + TABLE_NAME + " (name, age) VALUES (?, ?)";
private static final String UPDATE_SQL = "UPDATE " + TABLE_NAME + " SET name=?, age=? WHERE id=?";
private static final String DELETE_SQL = "DELETE FROM " + TABLE_NAME + " WHERE id=?";
private static final String SELECT_SQL = "SELECT * FROM " + TABLE_NAME;
private Connection connection = null;
public UserDAO() throws ClassNotFoundException, SQLException {
DatabaseConnection dbConnection = new DatabaseConnection();
connection = dbConnection.getConnection();
}
public void addUser(User user) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_SQL);
preparedStatement.setString(1, user.getName());
preparedStatement.setInt(2, user.getAge());
preparedStatement.executeUpdate();
}
public void updateUser(User user) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_SQL);
preparedStatement.setString(1, user.getName());
preparedStatement.setInt(2, user.getAge());
preparedStatement.setInt(3, user.getId());
preparedStatement.executeUpdate();
}
public void deleteUser(int id) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(DELETE_SQL);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
}
public ListgetUsers() throws SQLException {
Listusers = new ArrayList<>();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SELECT_SQL);
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setAge(resultSet.getInt("age"));
users.add(user);
}
return users;
}
public void closeConnection() throws SQLException {
if (connection != null) {
connection.close();
}
}
}

以上代碼示例中,首先是一個用于連接MySQL數(shù)據(jù)庫的Java類DatabaseConnection,它使用了JDBC來連接數(shù)據(jù)庫。然后是一個用于操作MySQL數(shù)據(jù)庫表的Java類UserDAO,其中包含了增加、刪除、修改、查詢等基本操作的方法。這些方法使用JDBC的PreparedStatement和Statement類來執(zhí)行SQL語句,實現(xiàn)對MySQL數(shù)據(jù)庫表的操作。當(dāng)然,在實際應(yīng)用中,這些代碼還需要進(jìn)行更細(xì)致的處理,例如異常處理、資源關(guān)閉等。