在创建一个类,实现数据库链接存储功能
时间: 2024-03-14 10:45:02 浏览: 14
好的,下面是一个简单的Java代码示例,实现了一个数据库连接和存储功能:
```java
import java.sql.*;
public class Database {
private static final String URL = "jdbc:mysql://localhost:3306/game";
private static final String USER = "root";
private static final String PASSWORD = "root";
private Connection connection;
public Database() {
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 建立数据库连接
connection = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public boolean register(String username, String password) {
try {
// 创建PreparedStatement对象
PreparedStatement statement = connection.prepareStatement("INSERT INTO user(username, password) VALUES (?, ?)");
// 设置参数
statement.setString(1, username);
statement.setString(2, password);
// 执行SQL语句
int result = statement.executeUpdate();
// 关闭PreparedStatement对象
statement.close();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public boolean login(String username, String password) {
try {
// 创建PreparedStatement对象
PreparedStatement statement = connection.prepareStatement("SELECT * FROM user WHERE username = ? AND password = ?");
// 设置参数
statement.setString(1, username);
statement.setString(2, password);
// 执行SQL查询
ResultSet resultSet = statement.executeQuery();
// 判断是否查询到结果
boolean hasResult = resultSet.next();
// 关闭ResultSet和PreparedStatement对象
resultSet.close();
statement.close();
return hasResult;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public boolean saveScore(String username, int score) {
try {
// 创建PreparedStatement对象
PreparedStatement statement = connection.prepareStatement("INSERT INTO score(username, score) VALUES (?, ?)");
// 设置参数
statement.setString(1, username);
statement.setInt(2, score);
// 执行SQL语句
int result = statement.executeUpdate();
// 关闭PreparedStatement对象
statement.close();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public ResultSet getTopScores(int limit) {
try {
// 创建PreparedStatement对象
PreparedStatement statement = connection.prepareStatement("SELECT * FROM score ORDER BY score DESC LIMIT ?");
// 设置参数
statement.setInt(1, limit);
// 执行SQL查询
ResultSet resultSet = statement.executeQuery();
return resultSet;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void close() {
try {
// 关闭数据库连接
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
在上面的代码中,我们创建了一个Database类,实现了数据库连接和存储功能。代码中包括了注册、登录、保存成绩、获取排行榜等方法,可以根据需要进行调用。需要注意的是,代码中的数据库用户名、密码、数据库名等需要根据实际情况进行修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)