IDEA增删改查登录注册代码
时间: 2023-07-24 13:11:59 浏览: 52
以下是使用Java语言和MySQL数据库实现一个简单的IDEA增删改查登录注册功能的示例代码:
1. 创建数据库表
```sql
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
)
```
2. 注册功能代码
```java
public void register(String username, String password) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
String sql = "INSERT INTO user (username, password) VALUES (?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, null);
}
}
```
3. 登录功能代码
```java
public boolean login(String username, String password) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = 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();
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, rs);
}
return false;
}
```
4. 修改功能代码
```java
public void update(String username, String password) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
String sql = "UPDATE user SET password = ? WHERE username = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, password);
pstmt.setString(2, username);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, null);
}
}
```
5. 删除功能代码
```java
public void delete(String username) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
String sql = "DELETE FROM user WHERE username = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, null);
}
}
```
6. 连接数据库和关闭资源代码
```java
private Connection getConnection() throws SQLException {
String url = "jdbc:mysql://localhost:3306/ideadb?useSSL=false&serverTimezone=UTC";
String username = "root";
String password = "123456";
return DriverManager.getConnection(url, username, password);
}
private void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
```
注意:这只是一个简单的示例代码,实际应用中需要注意安全性和性能方面的问题。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)