pstmt.setInt(1,book. getId()); pstmt.setString(2, book.getName ());
时间: 2024-03-31 22:38:10 浏览: 22
这是 PreparedStatement 对象的方法调用,用于将具体的参数值设置到 SQL 语句中。例如,`pstmt.setInt(1, book.getId())` 将第一个问号的值设置为 `book.getId()`,`pstmt.setString(2, book.getName())` 将第二个问号的值设置为 `book.getName()`。
具体来说,`setInt(int parameterIndex, int x)` 方法将 SQL 语句中指定位置的占位符替换为整数类型的值 `x`,`setString(int parameterIndex, String x)` 方法将 SQL 语句中指定位置的占位符替换为字符串类型的值 `x`。其中,`parameterIndex` 表示占位符的位置,从 1 开始计数。
通过使用 PreparedStatement 对象的参数设置方法,可以灵活地设置 SQL 语句中的参数值,而不需要拼接 SQL 字符串,避免了 SQL 注入攻击,同时也提高了 SQL 语句的执行效率。
相关问题
Java宿舍管理系统hsqldb:1.宿舍楼的增删改查和浏览;2.宿舍的增删改查和浏览;3.学生入学宿舍入住和退住;4.学生调换宿舍;5.根据学生信息查询这些功能的代码实现
好的,这是一个比较复杂的问题。我会尽可能地简洁明了地回答你。
我们可以使用Java来开发宿舍管理系统,同时使用hsqldb作为数据库。下面是实现这些功能的代码示例:
1. 宿舍楼的增删改查和浏览
添加宿舍楼:
```java
public void addBuilding(Building building) {
String sql = "INSERT INTO building (building_name, building_address) VALUES (?, ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, building.getName());
pstmt.setString(2, building.getAddress());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
删除宿舍楼:
```java
public void deleteBuilding(int id) {
String sql = "DELETE FROM building WHERE building_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
更新宿舍楼:
```java
public void updateBuilding(Building building) {
String sql = "UPDATE building SET building_name=?, building_address=? WHERE building_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, building.getName());
pstmt.setString(2, building.getAddress());
pstmt.setInt(3, building.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
查询宿舍楼:
```java
public List<Building> getBuildings() {
List<Building> buildings = new ArrayList<>();
String sql = "SELECT * FROM building";
try (Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
Building building = new Building();
building.setId(rs.getInt("building_id"));
building.setName(rs.getString("building_name"));
building.setAddress(rs.getString("building_address"));
buildings.add(building);
}
} catch (SQLException e) {
e.printStackTrace();
}
return buildings;
}
```
2. 宿舍的增删改查和浏览
添加宿舍:
```java
public void addDormitory(Dormitory dormitory) {
String sql = "INSERT INTO dormitory (building_id, dormitory_name, capacity) VALUES (?, ?, ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, dormitory.getBuildingId());
pstmt.setString(2, dormitory.getName());
pstmt.setInt(3, dormitory.getCapacity());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
删除宿舍:
```java
public void deleteDormitory(int id) {
String sql = "DELETE FROM dormitory WHERE dormitory_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
更新宿舍:
```java
public void updateDormitory(Dormitory dormitory) {
String sql = "UPDATE dormitory SET building_id=?, dormitory_name=?, capacity=? WHERE dormitory_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, dormitory.getBuildingId());
pstmt.setString(2, dormitory.getName());
pstmt.setInt(3, dormitory.getCapacity());
pstmt.setInt(4, dormitory.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
查询宿舍:
```java
public List<Dormitory> getDormitories() {
List<Dormitory> dormitories = new ArrayList<>();
String sql = "SELECT * FROM dormitory";
try (Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
Dormitory dormitory = new Dormitory();
dormitory.setId(rs.getInt("dormitory_id"));
dormitory.setBuildingId(rs.getInt("building_id"));
dormitory.setName(rs.getString("dormitory_name"));
dormitory.setCapacity(rs.getInt("capacity"));
dormitories.add(dormitory);
}
} catch (SQLException e) {
e.printStackTrace();
}
return dormitories;
}
```
3. 学生入学宿舍入住和退住
添加学生入住记录:
```java
public void addCheckInRecord(CheckInRecord checkInRecord) {
String sql = "INSERT INTO check_in_record (student_id, dormitory_id, check_in_time) VALUES (?, ?, ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, checkInRecord.getStudentId());
pstmt.setInt(2, checkInRecord.getDormitoryId());
pstmt.setTimestamp(3, new Timestamp(checkInRecord.getCheckInTime().getTime()));
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
添加学生退住记录:
```java
public void addCheckOutRecord(CheckOutRecord checkOutRecord) {
String sql = "INSERT INTO check_out_record (student_id, dormitory_id, check_out_time) VALUES (?, ?, ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, checkOutRecord.getStudentId());
pstmt.setInt(2, checkOutRecord.getDormitoryId());
pstmt.setTimestamp(3, new Timestamp(checkOutRecord.getCheckOutTime().getTime()));
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
4. 学生调换宿舍
更新学生入住记录:
```java
public void updateCheckInRecord(CheckInRecord checkInRecord) {
String sql = "UPDATE check_in_record SET dormitory_id=?, check_in_time=? WHERE student_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, checkInRecord.getDormitoryId());
pstmt.setTimestamp(2, new Timestamp(checkInRecord.getCheckInTime().getTime()));
pstmt.setInt(3, checkInRecord.getStudentId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
5. 根据学生信息查询
根据学生ID查询学生入住记录:
```java
public List<CheckInRecord> getCheckInRecordsByStudentId(int studentId) {
List<CheckInRecord> checkInRecords = new ArrayList<>();
String sql = "SELECT * FROM check_in_record WHERE student_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, studentId);
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
CheckInRecord checkInRecord = new CheckInRecord();
checkInRecord.setStudentId(rs.getInt("student_id"));
checkInRecord.setDormitoryId(rs.getInt("dormitory_id"));
checkInRecord.setCheckInTime(rs.getTimestamp("check_in_time"));
checkInRecords.add(checkInRecord);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return checkInRecords;
}
```
根据学生ID查询学生退住记录:
```java
public List<CheckOutRecord> getCheckOutRecordsByStudentId(int studentId) {
List<CheckOutRecord> checkOutRecords = new ArrayList<>();
String sql = "SELECT * FROM check_out_record WHERE student_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, studentId);
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
CheckOutRecord checkOutRecord = new CheckOutRecord();
checkOutRecord.setStudentId(rs.getInt("student_id"));
checkOutRecord.setDormitoryId(rs.getInt("dormitory_id"));
checkOutRecord.setCheckOutTime(rs.getTimestamp("check_out_time"));
checkOutRecords.add(checkOutRecord);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return checkOutRecords;
}
```
以上就是一个简单的宿舍管理系统的实现,代码示例仅供参考。如果你需要更详细的代码实现,可以参考一些开源的宿舍管理系统,如Dormitory Management System。
javaweb增删改查代码
这是一个简单的 JavaWeb 增删改查代码示例,主要使用了 JDBC 连接数据库实现:
1. 添加数据:
```java
public void add(Student student) throws SQLException {
Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO student(name, age, gender) VALUES(?,?,?)");
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setString(3, student.getGender());
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
```
2. 删除数据:
```java
public void delete(int id) throws SQLException {
Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM student WHERE id=?");
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
```
3. 修改数据:
```java
public void update(Student student) throws SQLException {
Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement("UPDATE student SET name=?, age=?, gender=? WHERE id=?");
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setString(3, student.getGender());
pstmt.setInt(4, student.getId());
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
```
4. 查询数据:
```java
public List<Student> query() throws SQLException {
Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM student");
ResultSet rs = pstmt.executeQuery();
List<Student> list = new ArrayList<>();
while(rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
list.add(student);
}
rs.close();
pstmt.close();
conn.close();
return list;
}
```
注意:以上代码仅供参考,实际使用时需要根据具体情况进行修改。同时需要注意数据库连接的释放和异常处理。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)