帮我写出对应java代码
时间: 2024-12-14 15:03:16 浏览: 6
好的,以下是针对《实验24.doc》中的各个编程题的Java代码示例:
### 基础训练
#### 调试教材范例
假设教材范例已经调试通过,这里不再重复。
### 编程题
#### 1. 设计字符菜单
```java
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("请选择操作:");
System.out.println("A: 连接数据库");
System.out.println("B: 插入数据");
System.out.println("C: 更新数据");
System.out.println("D: 删除数据");
System.out.println("Q: 退出");
String choice = scanner.nextLine();
switch (choice.toUpperCase()) {
case "A":
connectDatabase();
break;
case "B":
insertData(scanner);
break;
case "C":
updateData(scanner);
break;
case "D":
deleteData(scanner);
break;
case "Q":
exit = true;
break;
default:
System.out.println("无效的选择,请重新选择!");
}
}
scanner.close();
}
}
```
#### 2. 设计静态方法 `connect`
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseUtils {
private static final String URL = "jdbc:mysql://210.41.166.73:3306/student";
private static final String USER = "student";
private static final String PASSWORD = "student";
public static void connectDatabase() {
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
System.out.println("数据库连接成功!");
} catch (SQLException e) {
System.out.println("数据库连接失败:" + e.getMessage());
}
}
}
```
#### 3. 设计静态方法 `insert`
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class DatabaseUtils {
// ... (其他方法)
public static void insertData(Scanner scanner) {
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
String sql = "INSERT INTO users (name, password) VALUES (?, ?)";
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, name);
statement.setString(2, password);
int rowsAffected = statement.executeUpdate();
if (rowsAffected > 0) {
System.out.println("数据插入成功!");
} else {
System.out.println("数据插入失败!");
}
} catch (SQLException e) {
System.out.println("数据插入失败:" + e.getMessage());
}
}
}
```
#### 4. 设计静态方法 `update`
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class DatabaseUtils {
// ... (其他方法)
public static void updateData(Scanner scanner) {
System.out.print("请输入新的邮箱:");
String email = scanner.nextLine();
System.out.print("请输入新的生日:");
String birthday = scanner.nextLine();
System.out.print("请输入要更新的用户名:");
String name = scanner.nextLine();
String sql = "UPDATE users SET email = ?, birthday = ? WHERE name = ?";
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, email);
statement.setString(2, birthday);
statement.setString(3, name);
int rowsAffected = statement.executeUpdate();
if (rowsAffected > 0) {
System.out.println("数据更新成功!");
} else {
System.out.println("数据更新失败!");
}
} catch (SQLException e) {
System.out.println("数据更新失败:" + e.getMessage());
}
}
}
```
#### 5. 设计静态方法 `delete`
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class DatabaseUtils {
// ... (其他方法)
public static void deleteData(Scanner scanner) {
System.out.print("请输入要删除的用户名:");
String name = scanner.nextLine();
String sql = "DELETE FROM users WHERE name = ?";
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, name);
int rowsAffected = statement.executeUpdate();
if (rowsAffected > 0) {
System.out.println("数据删除成功!");
} else {
System.out.println("数据删除失败!");
}
} catch (SQLException e) {
System.out.println("数据删除失败:" + e.getMessage());
}
}
}
```
### 主类整合
将所有方法整合到主类中:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("请选择操作:");
System.out.println("A: 连接数据库");
System.out.println("B: 插入数据");
System.out.println("C: 更新数据");
System.out.println("D: 删除数据");
System.out.println("Q: 退出");
String choice = scanner.nextLine();
switch (choice.toUpperCase()) {
case "A":
DatabaseUtils.connectDatabase();
break;
case "B":
DatabaseUtils.insertData(scanner);
break;
case "C":
DatabaseUtils.updateData(scanner);
break;
case "D":
DatabaseUtils.deleteData(scanner);
break;
case "Q":
exit = true;
break;
default:
System.out.println("无效的选择,请重新选择!");
}
}
scanner.close();
}
}
```
这些代码实现了《实验24.doc》中所要求的功能。希望对你有帮助!如果有任何问题,请随时提问。
阅读全文