java课程设计 成绩_java课程设计 学生成绩管理
展開全部
/**
* 學員管理系統
* 版本號1.0
*/
/**
* 這是一個類,62616964757a686964616fe58685e5aeb931333262343038
* 用于演示學員的基本信息操作
* 創建人: csx
* 創建時間:2008年3月25日21:51:27
*/
package com.student.csx;
/**
* 導入相關的包
*/
// 導入加載驅動程序包
import java.sql.DriverManager; // 導入連接包
import java.sql.Connection; // 導入獲得語句包
import java.sql.Statement; // 導入執行語句包
import java.sql.ResultSet; // 導入SQL異常包
import java.sql.SQLException; // 導入SQL異常包
import java.util.Scanner;// 導入接收輸入包
public class Student {
/**
* 構造方法
*/
public Student() {
}
/**
* 這是一個方法 用于顯示用戶需要的菜單
*/
public void meau() {
System.out.println("請選擇功能");
System.out.println("1.查詢學員的基本信息");
System.out.println("2.添加學員的基本信息");
System.out.println("3.刪除學員的基本信息");
System.out.println("4.排序學員的基本信息");
System.out.println("5.修改學員的基本信息");
System.out.println("6.退出使用");
}
/**
* 這是一個方法 用于顯示學員的信息
*/
public void showStudentInfo() {
// 捕獲異常
try {
/**
* 第一步: 加載驅動程序---橋連接
*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();// 打印出異常的詳細信息
}
// 捕獲異常
try {
/**
* 第二步: 創建連接
*/
Connection conn = DriverManager.getConnection("jdbc:odbc:student",
"sa", "sa");
/**
* 第三步: 獲取語句對象
*/
Statement stmt = conn.createStatement();
/**
* 第四步: 執行語句
*/
ResultSet rs = stmt.executeQuery("select * from student");
/**
* 第五步: 打印出結果
*/
while (rs.next()) {
System.out.print(rs.getString(1));
System.out.print(rs.getString(2));
System.out.print(rs.getString(3));
System.out.println(rs.getInt(4));
System.out.println(rs.getFloat(5));
}
/**
* 第六步: 釋放資源
*/
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 這是一個方法 用于添加學員的信息
*/
public void appendStudentInfo() {
// 準備接收輸入
Scanner input = new Scanner(System.in);
/**
* 提示用戶輸入
*/
System.out.println("請輸入姓名:");
/**
* 用于接收用戶輸入的信息
*/
String name = input.nextLine();
/**
* 提示用戶輸入
*/
System.out.println("請輸入學號:");
/**
* 用于接收用戶輸入的信息
*/
String id = input.nextLine();
/**
* 提示用戶輸入
*/
System.out.println("請輸入性別:");
/**
* 用于接收用戶輸入的信息
*/
String sex = input.nextLine();
/**
* 提示用戶輸入
*/
System.out.println("請輸入年齡:");
/**
* 用于接收用戶輸入的信息
*/
int age = input.nextInt();
/**
* 提示用戶輸入
*/
System.out.println("請輸入平均成績:'");
/**
* 用于接收用戶輸入的信息
*/
Float score = input.nextFloat();
/**
* 捕獲異常
*/
try {
/**
* 第一步: 加載驅動程序---橋連接
*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
/**
* 第二步:
* 創建連接
*/
Connection conn = DriverManager.getConnection("jdbc:odbc:student",
"sa", "sa");
/**
* 第三步:
* 獲得語句
*/
Statement stmt = conn.createStatement();
/**
* 第四步:
* 執行語句塊
*/
String sql = "insert into student(name,id,sex,age)" + "values('"
+ name + "','" + id + "','" + sex + "'," + age + ",'"
+ score + "')";
/** 執行語句,并返回一個數字(數字是指影響了幾行) */
int updateRow = stmt.executeUpdate(sql);
/** 判斷是否插入成功 */
if (updateRow > 0) {
System.out.println("插入成功");
} else {
System.out.println("插入失敗");
}
/**
* 最后一步:
* 釋放資源
*/
stmt.close();
conn.close();
} catch (Exception e) {
/**
* 打印異常的詳細信息
*/
e.getStackTrace();
}
}
/**
* 這是一個方法 用于刪除學員的信息
*/
public void deleteStudentInfo() {
try {
/**
* 第一步:
* 加載驅動程序
*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//準備接收
Scanner input = new Scanner(System.in);
//創建連接
Connection conn = DriverManager.getConnection("jdbc:odbc:student",
"sa", "sa");
//獲取語句
Statement stmt = conn.createStatement();
//提示用戶輸入
System.out.println("請輸入你要刪除學員的姓名:");
//接收用戶輸入的信息
String name = input.nextLine();
//執行語句
String sql = "delete from student where name = '" + name + "'";
int updateRow = stmt.executeUpdate(sql);
/** 判斷是否刪除成功 */
if (updateRow > 0) {
System.out.println("刪除成功");
} else {
System.out.println("刪除失敗");
}
//釋放資源
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 這是一個方法 用于將學員的信息進行排序
*/
public void compositorStudentInfo() {
// 捕獲異常
try {
/**
* 第一步: 加載驅動程序---橋連接
*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();// 打印出異常的詳細信息
}
// 捕獲異常
try {
/**
* 第二步: 創建連接
*/
Connection conn = DriverManager.getConnection("jdbc:odbc:student",
"sa", "sa");
/**
* 第三步: 獲取語句對象
*/
Statement stmt = conn.createStatement();
/**
* 第四步: 執行語句
*/
ResultSet rs = stmt
.executeQuery("select * from student order by age");
/**
* 第五步: 打印出結果
*/
System.out.print("按年齡排序為:\n");
while (rs.next()) {
System.out.print(rs.getString(1));
System.out.print(rs.getString(2));
System.out.print(rs.getString(3));
System.out.println(rs.getInt(4));
System.out.println(rs.getFloat(5));
}
/**
* 第六步: 釋放資源
*/
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 這是一個方法 用于修改學員的信息
*/
public void amendStudentInfo() {
// 捕獲異常
try {
/**
* 第一步: 加載驅動程序---橋連接
*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();// 打印出異常的詳細信息
}
// 捕獲異常
try {
/**
* 第二步: 創建連接
*/
Connection conn = DriverManager.getConnection("jdbc:odbc:student",
"sa", "sa");
/**
* 第三步: 獲取語句對象
*/
Statement stmt = conn.createStatement();
//準備接收輸入
Scanner input = new Scanner(System.in);
//提示用戶輸入
System.out.println("請輸入你要修改的學員的姓名:");
//接收用戶輸入的信息
String name = input.nextLine();
//提示輸入
System.out.println("請輸入你修改后學員的成績");
//接收用戶輸入的信息
Float score = input.nextFloat();
/**
* 第四步: 執行語句
*/
String sql = "update student set score = '" + score
+ "' where name = '" + name + "'";
int updateRow = stmt.executeUpdate(sql);
/** 判斷是否刪除成功 */
if (updateRow > 0) {
System.out.println("修改成功");
} else {
System.out.println("修改失敗");
}
//stmt.close();
//conn.close();
/**
* 第六步: 釋放資源
*/
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
下面這是一個場景類:
/**
* 這是一個測試類
* 用于測試
* 創建人:CSX * 創建時間:2008年3月25日22:03:10
* 版本號:1.0
*/
//這是一個包
package com.student.csx;
//導入接收包
import java.util.Scanner;
public class Action {
/**
* 這是程序的入口點
* @param args
*/
public static void main(String[] args) {
Student stu = new Student();//聲明一個對象
stu.meau();//調用菜單
int elect;//聲明一個變量,用于接收用戶的選擇
while (true) {
/**準備接收輸入*/
Scanner input = new Scanner(System.in);
//System.out.println("");
/**接收*/
elect = input.nextInt();
//switch分支結構來判斷
switch (elect) {
case 1:
//調用顯示方法
stu.showStudentInfo();
break;
case 2:
//調用添加方法
stu.appendStudentInfo();
break;
case 3:
//調用刪除方法
stu.deleteStudentInfo();
break;
case 4://調用排序方法
stu.compositorStudentInfo();
break;
case 5:
//調用修改方法
stu.amendStudentInfo();
break;
case 6:
//退出使用
System.exit(0);
}
}
}
}
本回答由電腦網絡分類達人 董輝推薦
已贊過
已踩過<
你對這個回答的評價是?
評論
收起
總結
以上是生活随笔為你收集整理的java课程设计 成绩_java课程设计 学生成绩管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java set类_java中set类型
- 下一篇: java rabbitmq 工具类_Ra