此次使用Java JDBC+MySQL數(shù)據(jù)庫實現(xiàn)一個簡易的學(xué)生管理系統(tǒng)(沒有前端界面)。
前言
Java數(shù)據(jù)庫連接,(Java Database Connectivity,簡稱JDBC)是Java語言中用來規(guī)范客戶端程序如何來訪問數(shù)據(jù)庫的應(yīng)用程序接口,提供了諸如查詢和更新數(shù)據(jù)庫中數(shù)據(jù)的方法。JDBC也是Sun Microsystems的商標(biāo)。我們通常說的JDBC是面向關(guān)系型數(shù)據(jù)庫的。摘自百度百科–jdbc
現(xiàn)使用JDBC+MySQL實現(xiàn)簡易的學(xué)生信息管理系統(tǒng),主要設(shè)計學(xué)生信息查詢、添加學(xué)生、修改學(xué)生信息、刪除學(xué)生等功能。提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、數(shù)據(jù)庫設(shè)計
在StuMIS數(shù)據(jù)庫中創(chuàng)建如下表格:
SQL代碼如下:
create database StuIMS default CHARACTER set utf8mb4;-- 創(chuàng)建數(shù)據(jù)庫
use StuIMS;
-- 創(chuàng)建學(xué)生表
create table Student(
studentID char(8) not null primary key, -- 學(xué)號
studentName varchar(30) not null, -- 姓名
studentSex char(4) not null default '男', -- 性別
studentBirthday date not null default '2002-1-1', -- 出生日期
credit int not null default 0, -- 學(xué)分
studentClass char(6) not null -- 班級編號
)
數(shù)據(jù)庫創(chuàng)建完成后,向數(shù)據(jù)表中添加部分測試數(shù)據(jù),代碼如下:
insert into Student values('20210101','張三',default,default,0,'202101'),
('20210122','李明',default,'2003-9-15',0,'202101'),
('20210203','王敏','女','2003-6-25',0,'202102'),
('20210218','劉洋',default,'2002-7-08',0,'202102'),
('20210310','劉洋','女','2003-1-29',0,'202103'),
('20210405','江民',default,'2000-12-29',0,'202104'),
('20210436','王軍',default,'2002-10-10',0,'202104'),
('20210501','李玉軍',default,default,0,'202105'),
('20210502','王紅娟','女','2004-1-1',0,'202105');
二、Java代碼編寫實現(xiàn)
1.創(chuàng)建項目,引入JDBC的.jar包
下載數(shù)據(jù)庫的驅(qū)動包,可以到官網(wǎng)下載:驅(qū)動下載
下載完成后,將壓縮包解壓到桌面。
使用eclipse創(chuàng)建一個名為Student的java項目,點擊項目右鍵,找到Build Path—>>Configure Path…
點擊添加.jar包,找到剛剛解壓的文件,選擇拓展名為(.jar)的文件。
點擊添加,應(yīng)用即可。
添加完成后,在項目中新建MainTest類,用于編寫Java代碼。
2.創(chuàng)建連接驅(qū)動方法
在MainTest中創(chuàng)建public Connection getConnection()方法,用于創(chuàng)建數(shù)據(jù)庫的鏈接驅(qū)動。(信息的添加、刪除、修改、查詢都需要連接數(shù)據(jù)庫,創(chuàng)建連接方法,可以減少代碼的冗余。)
代碼如下(示例):
public Connection getConnection() throws SQLException, ClassNotFoundException {
String Driver = "com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/StuIMS";
String user="root";
String pwd="123456";
Class.forName(Driver);//加載驅(qū)動
Connection con = DriverManager.getConnection(url,user,pwd);//建立連接
if(con.isClosed()) {
System.err.println("數(shù)據(jù)庫連接失敗。");
return null;
}else {
System.out.println("連接成功。");
return con;
}
}
該處使用的url網(wǎng)絡(luò)請求的數(shù)據(jù)。
3、查詢所有數(shù)據(jù)
定義SelectAll()方法查詢所有學(xué)生數(shù)據(jù)。
代碼示例如下:
public void SelectAll() throws ClassNotFoundException, SQLException {
Connection con = getConnection();//調(diào)用getConnection()方法獲取數(shù)據(jù)庫連接對象
PreparedStatement ps = con.prepareStatement("select * from Student");//創(chuàng)建預(yù)處理對象執(zhí)行SQL查詢語句
ResultSet rs = ps.executeQuery();//查詢結(jié)果集
System.out.println(" 學(xué)號\t\t姓名\t 性別\t 生日\t\t學(xué)分");
while(rs.next()) {//遍歷結(jié)果集
//使用ResultSet的get方法獲取集合中的值,參數(shù)為數(shù)據(jù)庫中的字段名
System.out.println(rs.getString("studentID")+"\t"+rs.getString("studentName")+"\t "+rs.getString("studentSex")+"\t "
+rs.getString("studentBirthday")+"\t"+rs.getString("credit")+"\t");
}
}
4、插入學(xué)生數(shù)據(jù)
定義public int insertStudent(int num)方法向數(shù)據(jù)庫中添加數(shù)據(jù)。int num為一次添加的學(xué)生的數(shù)量。
實例代碼如下:
public int insertStudent(int num) throws ClassNotFoundException, SQLException {
Connection con = getConnection();
String sql = "insert into Student values(?,?,?,?,0,'202105')";//定義SQl語句,班級編號和學(xué)分不需手動插入。
PreparedStatement ps = con.prepareStatement(sql);//執(zhí)行SQL語句
Scanner sc = new Scanner(System.in);
int count=0;//定義變量保存修改的行數(shù)
for(int i=1;i<=num;i++) {
System.out.println("請輸入第"+i+"個學(xué)生的學(xué)號:");
String ID = sc.next();
System.out.println("請輸入第"+i+"個學(xué)生的姓名:");
String name = sc.next();
System.out.println("請輸入第"+i+"個學(xué)生的性別:");
String sex = sc.next();
System.out.println("請輸入第"+i+"個學(xué)生的生日:");
String birthday = sc.next();
//將輸入的值傳入,執(zhí)行SQL,添加數(shù)據(jù)
ps.setString(1, ID);
ps.setString(2, name);
ps.setString(3, sex);
ps.setString(4, birthday);
//executeUpdate()方法的返回值為受影響的行數(shù)(插入的數(shù)據(jù)行數(shù)),如果返回值為1,則表示數(shù)據(jù)插入成功(一次循環(huán)執(zhí)行一次插入)
if(ps.executeUpdate()==1) {//
count++;//插入成功,將count值加一
}else {
System.out.println("數(shù)據(jù)插入失敗。");
break;
}
}
return count;//返回受影響的行數(shù)
}
5、修改學(xué)生信息
定義public int updateStudent(String ID)方法,根據(jù)學(xué)號(唯一,不可重復(fù))修改學(xué)生數(shù)據(jù)信息。
示例代碼如下:
public int updateStudent(String ID) throws ClassNotFoundException, SQLException {
Connection con = getConnection();
String sql="update Student set studentName=?,studentSex=?,studentBirthday=? where studentID="+ID;
//定義SQL語句修改信息,允許修改的字段值為姓名、性別、出生日期,學(xué)號不允許修改。
PreparedStatement ps = con.prepareStatement(sql);
Scanner sc = new Scanner(System.in);
int count=0;
System.out.println("請輸入學(xué)生的姓名:");
String name = sc.next();
System.out.println("請輸入學(xué)生的性別:");
String sex = sc.next();
System.out.println("請輸入學(xué)生的生日:");
String birthday = sc.next();
ps.setString(1, name);
ps.setString(2, sex);
ps.setString(3, birthday);
count = ps.executeUpdate();
return count;//返回受影響的行數(shù)
}
6、刪除學(xué)生數(shù)據(jù)
定義public int deleteStudentByID(String ID)方法刪除學(xué)生。根據(jù)學(xué)號執(zhí)行刪除操作。
示例代碼如下:
public int deleteStudentByID(String ID) throws ClassNotFoundException, SQLException {
Connection con = getConnection();
Scanner sc = new Scanner(System.in);
PreparedStatement psS = con.prepareStatement("select * from Student where studentID="+ID);
ResultSet rs = psS.executeQuery();
//根據(jù)學(xué)號查詢需要刪除的學(xué)生的信息
System.out.println("即將刪除的學(xué)生的信息:");
System.out.println(" 學(xué)號\t\t姓名\t 性別\t 生日\t\t學(xué)分");
while(rs.next()) {
System.out.println(rs.getString("studentID")+"\t"+rs.getString("studentName")+"\t "+rs.getString("studentSex")+"\t "
+rs.getString("studentBirthday")+"\t"+rs.getString("credit")+"\t");
}
System.out.println("請確認信息是否無誤(y/Y)?");
char ch1 = sc.next().charAt(0);
//
if(ch1=='Y'||ch1=='y') {
System.out.println("請確認是否刪除(y/Y)?");
char ch2 = sc.next().charAt(0);
if(ch2=='y'||ch2=='Y') {//確認刪除后,執(zhí)行刪除操作
PreparedStatement psD = con.prepareStatement("delete from Student where studentID="+ID);
int count = psD.executeUpdate();
return count;//返回受影響的行數(shù)
}else {
System.out.println("刪除取消。");
return 0;
}
}else {
System.out.println("信息有誤,請重新選擇......");
return -1;
}
}
7、menu方法
定義public int menu()方法打印菜單選項。
示例代碼如下:文章來源:http://www.zghlxwxcb.cn/news/detail-450806.html
public int menu() {
Scanner sc = new Scanner(System.in);
System.out.println("******************************歡迎使用學(xué)生信息管理系統(tǒng)******************************");
System.out.println("\t\t功能列表如下:");
System.out.println("\t\t1、查詢學(xué)生信息。");
System.out.println("\t\t2、添加學(xué)生信息。");
System.out.println("\t\t3、修改學(xué)生信息。");
System.out.println("\t\t4、刪除學(xué)生信息。");
System.out.println("\t\t5、退出系統(tǒng)。");
System.out.println("請選擇你需要的功能:");
return sc.nextInt();
}
8、main方法
public static void main(String[] args) throws ClassNotFoundException, SQLException {
MainTest m = new MainTest();//實例化當(dāng)前類的對象,調(diào)用其他成員方法
Scanner sc = new Scanner(System.in);
while(true) {
int ch = m.menu();//調(diào)用菜單選項,獲取用戶的選擇
switch(ch) {//使用Switch結(jié)構(gòu)根據(jù)用戶輸入執(zhí)行不同功能
case 1:m.SelectAll();break;//查詢所有信息
case 2:
System.out.println("請輸入需要添加的學(xué)生的數(shù)量:");
int n = sc.nextInt();
int countInsert = m.insertStudent(n);//獲取插入成功的數(shù)據(jù)行數(shù)
//如果插入成功行數(shù)與用戶輸入相等,則表示成功。還有事務(wù)回滾和提交操作沒有實現(xiàn),感興趣的朋友可以自行添加實現(xiàn)這個功能。
if(countInsert==n) {
System.out.println("學(xué)生信息添加成功!");
}else {
System.out.println("信息輸入有誤,學(xué)生添加失敗。");
}
break;
case 3:
System.out.println("請輸入需要修改的學(xué)生的學(xué)號:");
String IDUpdate= sc.next();
int countUpdate = m.updateStudent(IDUpdate);
if(countUpdate==1) {//學(xué)號唯一,一個學(xué)號對應(yīng)一條數(shù)據(jù)
System.out.println("學(xué)生信息修改成功。");
}
break;
case 4:
System.out.println("請輸入需要刪除的學(xué)生的學(xué)號:");
String IDDelete = sc.next();
int countDelete = m.deleteStudentByID(IDDelete);
if(countDelete==1) {
System.out.println("刪除成功。");
}else {
System.out.println("刪除失敗。");
}
break;
case 5:
System.out.println("感謝使用,系統(tǒng)退出中......");
System.exit(0);//退出執(zhí)行
default:System.err.println("請選擇相應(yīng)的功能......");
}
System.out.println("請輸入回車鍵繼續(xù)......");
sc.nextLine();
}
}
總結(jié)
以上就是學(xué)生管理系統(tǒng)的全部內(nèi)容,其中有部分功能并未完全實現(xiàn),例如插入學(xué)生數(shù)據(jù)時,并未控制事務(wù)的提交,某一條數(shù)據(jù)插入失敗時,之前的數(shù)據(jù)也會自動提交,邏輯上還不是很完整,如果有感興趣的朋友可以自行進行完善。刪除數(shù)據(jù)和修改數(shù)據(jù)時邏輯也還有欠缺。
碼字不易,感謝閱讀。
完整代碼奉上:文章來源地址http://www.zghlxwxcb.cn/news/detail-450806.html
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
@SuppressWarnings("resource")
public class MainTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
MainTest m = new MainTest();
Scanner sc = new Scanner(System.in);
while(true) {
int ch = m.menu();
switch(ch) {
case 1:m.SelectAll();break;
case 2:
System.out.println("請輸入需要添加的學(xué)生的數(shù)量:");
int n = sc.nextInt();
int countInsert = m.insertStudent(n);
if(countInsert==n) {
System.out.println("學(xué)生信息添加成功!");
}else {
System.out.println("信息輸入有誤,學(xué)生添加失敗。");
}
break;
case 3:
System.out.println("請輸入需要修改的學(xué)生的學(xué)號:");
String IDUpdate= sc.next();
int countUpdate = m.updateStudent(IDUpdate);
if(countUpdate>0) {
System.out.println("學(xué)生信息修改成功。");
}
break;
case 4:
System.out.println("請輸入需要刪除的學(xué)生的學(xué)號:");
String IDDelete = sc.next();
int countDelete = m.deleteStudentByID(IDDelete);
if(countDelete>0) {
System.out.println("刪除成功。");
}else {
System.out.println("刪除失敗。");
}
break;
case 5:System.out.println("感謝使用,系統(tǒng)退出中......");;System.exit(0);
default:System.err.println("請選擇相應(yīng)的功能......");
}
System.out.println("請輸入回車鍵繼續(xù)......");
sc.nextLine();
}
}
public int menu() {
Scanner sc = new Scanner(System.in);
System.out.println("******************************歡迎使用學(xué)生信息管理系統(tǒng)******************************");
System.out.println("\t\t功能列表如下:");
System.out.println("\t\t1、查詢學(xué)生信息。");
System.out.println("\t\t2、添加學(xué)生信息。");
System.out.println("\t\t3、修改學(xué)生信息。");
System.out.println("\t\t4、刪除學(xué)生信息。");
System.out.println("\t\t5、退出系統(tǒng)。");
System.out.println("請選擇你需要的功能:");
return sc.nextInt();
}
// 連接數(shù)據(jù)庫
public Connection getConnection() throws SQLException, ClassNotFoundException {
String Driver = "com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/StuIMS";
String user="root";
String pwd="123456";
Class.forName(Driver);
Connection con = DriverManager.getConnection(url,user,pwd);
if(con.isClosed()) {
System.err.println("數(shù)據(jù)庫連接失敗。");
return null;
}else {
System.out.println("連接成功。");
return con;
}
}
// 查詢所有數(shù)據(jù)
public void SelectAll() throws ClassNotFoundException, SQLException {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("select * from Student");
ResultSet rs = ps.executeQuery();
System.out.println(" 學(xué)號\t\t姓名\t 性別\t 生日\t\t學(xué)分");
while(rs.next()) {
System.out.println(rs.getString("studentID")+"\t"+rs.getString("studentName")+"\t "+rs.getString("studentSex")+"\t "
+rs.getString("studentBirthday")+"\t"+rs.getString("credit")+"\t");
}
}
// 插入數(shù)據(jù)
public int insertStudent(int num) throws ClassNotFoundException, SQLException {
Connection con = getConnection();
String sql = "insert into Student values(?,?,?,?,?,'202105')";
PreparedStatement ps = con.prepareStatement(sql);
Scanner sc = new Scanner(System.in);
int count=0;
for(int i=1;i<=num;i++) {
System.out.println("請輸入第"+i+"個學(xué)生的學(xué)號:");
String ID = sc.next();
System.out.println("請輸入第"+i+"個學(xué)生的姓名:");
String name = sc.next();
System.out.println("請輸入第"+i+"個學(xué)生的性別:");
String sex = sc.next();
System.out.println("請輸入第"+i+"個學(xué)生的生日:");
String birthday = sc.next();
System.out.println("請輸入第"+i+"個學(xué)生的學(xué)分:");
int credit = sc.nextInt();
ps.setString(1, ID);
ps.setString(2, name);
ps.setString(3, sex);
ps.setString(4, birthday);
ps.setInt(5, credit);
if(ps.executeUpdate()>0) {
count++;
}else {
System.out.println("數(shù)據(jù)插入失敗。");
break;
}
}
return count;
}
// 修改數(shù)據(jù)
public int updateStudent(String ID) throws ClassNotFoundException, SQLException {
Connection con = getConnection();
String sql="update Student set studentName=?,studentSex=?,studentBirthday=? where studentID="+ID;
PreparedStatement ps = con.prepareStatement(sql);
Scanner sc = new Scanner(System.in);
int count=0;
System.out.println("請輸入學(xué)生的姓名:");
String name = sc.next();
System.out.println("請輸入學(xué)生的性別:");
String sex = sc.next();
System.out.println("請輸入學(xué)生的生日:");
String birthday = sc.next();
// System.out.println("請輸入學(xué)生的學(xué)分:");
// int credit = sc.nextInt();
ps.setString(1, name);
ps.setString(2, sex);
ps.setString(3, birthday);
// ps.setInt(4, credit);
count = ps.executeUpdate();
return count;
}
public int deleteStudentByID(String ID) throws ClassNotFoundException, SQLException {
Connection con = getConnection();
Scanner sc = new Scanner(System.in);
PreparedStatement psS = con.prepareStatement("select * from Student where studentID="+ID);
ResultSet rs = psS.executeQuery();
System.out.println("即將刪除的學(xué)生的信息:");
System.out.println(" 學(xué)號\t\t姓名\t 性別\t 生日\t\t學(xué)分");
while(rs.next()) {
System.out.println(rs.getString("studentID")+"\t"+rs.getString("studentName")+"\t "+rs.getString("studentSex")+"\t "
+rs.getString("studentBirthday")+"\t"+rs.getString("credit")+"\t");
}
System.out.println("請確認信息是否無誤(y/Y)?");
char ch1 = sc.next().charAt(0);
if(ch1=='Y'||ch1=='y') {
System.out.println("請確認是否刪除(y/Y)?");
char ch2 = sc.next().charAt(0);
if(ch2=='y'||ch2=='Y') {
PreparedStatement psD = con.prepareStatement("delete from Student where studentID="+ID);
int count = psD.executeUpdate();
return count;
}else {
System.out.println("刪除取消。");
return -1;
}
}else {
System.out.println("信息有誤,請重新選擇......");
return -1;
}
}
}
到了這里,關(guān)于【Java:JDBC+MySQL實現(xiàn)學(xué)生信息管理系統(tǒng)】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!