以下为学生成绩绩点表: 绩点值,下限分,上限分,等级 4 , 90 , 100, A(优) 3.7 , 87 , 89.9, A- 3.3 , 84 , 86.9 ,B+ 3 , 81 , 83.9 ,B(良) 2.7 , 78 , 80.9 ,B- 2.3 , 75 , 77.9 ,C+ 2 , 72 , 74.9 ,C(中) 1.7 , 69 , 71.9 ,C- 1.3 , 66 , 68.9 ,D+ 1 , 60 , 65.9 ,D 0 , 0 , 59.9 ,F(不及格) 该数据存储在mysql服务器127.0.0.1的test数据库中,其表结构如下: create table JDB( jdh float primary key, xxf numeric(4,1) , sxf numeric(4,1) , bz char(20) ) 服务器的访问账号为root,登录密码为abcd,要求使用java连接mysql数据库中的jdb,编写菜单方式对该绩点表的增加、删除、修改、查询功能
时间: 2024-01-19 22:03:12 浏览: 240
学生成绩平均绩点的计算.exe, C语言实现
好的,我会尽力回答你的问题。首先,我需要确认一下你的问题是关于学生成绩绩点表的操作。如果是的话,以下是一个基本的Java程序,可以连接到MySQL服务器并执行增加、删除、修改和查询操作:
```java
import java.sql.*;
public class JDBDemo {
private static final String DB_URL = "jdbc:mysql://127.0.0.1/test";
private static final String USERNAME = "root";
private static final String PASSWORD = "abcd";
private static Connection conn = null;
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
while (true) {
System.out.println("请选择你要进行的操作:");
System.out.println("1. 增加记录");
System.out.println("2. 删除记录");
System.out.println("3. 修改记录");
System.out.println("4. 查询记录");
System.out.println("5. 退出程序");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String choice = br.readLine();
switch (choice) {
case "1":
addRecord();
break;
case "2":
deleteRecord();
break;
case "3":
updateRecord();
break;
case "4":
queryRecord();
break;
case "5":
conn.close();
System.exit(0);
break;
default:
System.out.println("输入错误,请重新输入。");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void addRecord() {
try {
System.out.println("请输入绩点值:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
float jdh = Float.parseFloat(br.readLine());
System.out.println("请输入下限分数:");
float xxf = Float.parseFloat(br.readLine());
System.out.println("请输入上限分数:");
float sxf = Float.parseFloat(br.readLine());
System.out.println("请输入等级:");
String bz = br.readLine();
String sql = "INSERT INTO JDB(jdh, xxf, sxf, bz) VALUES(?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setFloat(1, jdh);
pstmt.setFloat(2, xxf);
pstmt.setFloat(3, sxf);
pstmt.setString(4, bz);
pstmt.executeUpdate();
System.out.println("添加成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
private static void deleteRecord() {
try {
System.out.println("请输入要删除的绩点值:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
float jdh = Float.parseFloat(br.readLine());
String sql = "DELETE FROM JDB WHERE jdh = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setFloat(1, jdh);
pstmt.executeUpdate();
System.out.println("删除成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
private static void updateRecord() {
try {
System.out.println("请输入要修改的绩点值:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
float jdh = Float.parseFloat(br.readLine());
System.out.println("请输入新的下限分数:");
float xxf = Float.parseFloat(br.readLine());
System.out.println("请输入新的上限分数:");
float sxf = Float.parseFloat(br.readLine());
System.out.println("请输入新的等级:");
String bz = br.readLine();
String sql = "UPDATE JDB SET xxf = ?, sxf = ?, bz = ? WHERE jdh = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setFloat(1, xxf);
pstmt.setFloat(2, sxf);
pstmt.setString(3, bz);
pstmt.setFloat(4, jdh);
pstmt.executeUpdate();
System.out.println("修改成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
private static void queryRecord() {
try {
System.out.println("请输入要查询的绩点值:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
float jdh = Float.parseFloat(br.readLine());
String sql = "SELECT * FROM JDB WHERE jdh = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setFloat(1, jdh);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
float xxf = rs.getFloat("xxf");
float sxf = rs.getFloat("sxf");
String bz = rs.getString("bz");
System.out.println("绩点值:" + jdh);
System.out.println("下限分数:" + xxf);
System.out.println("上限分数:" + sxf);
System.out.println("等级:" + bz);
} else {
System.out.println("未找到记录。");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
这个程序使用了Java的JDBC API来连接到MySQL服务器,并提供了菜单选项来执行增加、删除、修改和查询操作。你可以根据你的具体需求来修改这个程序,例如添加更多的菜单选项或者修改表结构。
另外需要注意的是,这个程序在使用前需要先导入MySQL的JDBC驱动程序,你可以在这里下载MySQL的JDBC驱动程序:https://dev.mysql.com/downloads/connector/j/。下载后,将驱动程序的JAR文件添加到你的Java项目的classpath中即可。
阅读全文