学生管理系统JDBC
这个的学生管理系统包括以下几个部分
然后打开Navicat,链接上本地数据库,打开数据库
这是一张student表,八个属性,除了age是int,sex是char(3),grade是char(6),其他都是varchar。ID是主键。
2. JAVA部分
此部分我只创建了两个类,主类和loadin(登陆类),JDBC的操作全是在loadin中完成,主程序类比较清爽,用户只有一个bb,密码123456。以下是主程序的截图和测试的一个结果,由于测试过程比较多,就不一一贴图了。
这是本次程序的要求,借助JDBC,可以全部完成
本次实验代码为一个包下的两个类
//main程序
package management;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
loadin load = new loadin(); //创建一个登陆类
load.loading();
int i = input.nextInt();
int choice;
int flag=0; //用于判断密码
switch (i){
case 1:
flag = load.loadinguser(flag);
if(flag == 1){ //判断用户名密码正确进入操作界面
load.loading1(); //用户选择操作
choice = input.nextInt();
do{
switch (choice){
case 1: //进入查询学生信息界面
load.loadingselect();
break;
case 2: //进入添加学生界面
load.loadingadd();
break;
case 3: //进入删除学生界面
load.loadingdel();
break;
case 4: //进入修改学生界面
load.loadalter();
break;
case 5: //退出
load.loadingexit();
break;
}
load.loading1();
choice =input.nextInt();
}while(choice != 100);
}else{ //若用户名或密码不正确则退出
break;
}
break;
case 2:
load.loadingexit();
break;
}
}
}
//loadin类
package management;
import java.sql.*;
import java.util.Scanner;
public class loadin{
Scanner input = new Scanner(System.in);
public void loading(){
System.out.println("***********欢迎登陆学生信息管理系统***********");
System.out.println("*1.登陆 2.退出 *");
System.out.println("********************************************");
System.out.println("请选择:");
}
public int loadinguser(int flag){
System.out.println("**************欢迎登陆**************");
System.out.print("*请输入用户名:");
String username = input.next();
System.out.print("*请输入密码:");
String userpassword = input.next();
if("bb".equals(username) && "123456".equals(userpassword)){
System.out.println("******************************登陆成功******************************");
flag = 1;
return flag;
}else{
System.out.println("********************用户名或密码错误********************");
flag = 0;
return flag;
}
}
//登录界面
public void loading1(){
System.out.println("**********************请选择要操作信息对应的数字*********************");
System.out.println("*1.查看学生信息 2.添加学生信息 3.删除学生信息 4.修改学生信息 5.退出*");
System.out.println("*******************************************************************");
System.out.println("请选择:");
}
//查找学生界面
public void loadingselect(){
System.out.println("**********************请选择要操作信息对应的数字*********************");
System.out.println("*1.查看全部学生信息 2.根据ID查看学生信息 3.根据姓名查看学生ID 4.返回*");
System.out.println("*******************************************************************");
int choice = input.nextInt();
switch (choice){
case 1: //查看全部学生的信息
try {
Class.forName("com.mysql.jdbc.Driver"); //注册驱动
} catch (ClassNotFoundException var14) {
System.out.println("注册驱动失败!");
var14.printStackTrace();
return;
}
String url = "jdbc:mysql://localhost:3306/mysql";
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "123456");
Statement stmt = conn.createStatement();
System.out.println("创建Statement成功!");
ResultSet rs = stmt.executeQuery("select ID, name, age ,sex,grade,tel,email,address from student ");
while(rs.next()) {
System.out.println(
rs.getString(1) + "," + rs.getString(2) + "," + rs.getInt(3)+","
+rs.getString(4)+","+rs.getString(5)+","+rs.getString("tel")+","
+rs.getString("email")+","+rs.getString("address")
);
}
rs.close();
stmt.close();
} catch (SQLException var15) {
var15.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException var13) {
var13.printStackTrace();
}
}
System.out.println("查询完毕退回操作界面......");
break;
case 2:
try {
Class.forName("com.mysql.jdbc.Driver"); //注册驱动
} catch (ClassNotFoundException var14) {
System.out.println("注册驱动失败!");
var14.printStackTrace();
return;
}
System.out.println("请输入学生ID:");
String stuID = input.next();
Connection conn2 = null;
try {
conn2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "123456");
Statement stmt = conn2.createStatement();
System.out.println("创建Statement成功!");
ResultSet rs = stmt.executeQuery("select ID, name, age ,sex,grade,tel,email,address from student where ID ="+"'"+stuID+"'");
while(rs.next()) {
System.out.println(
rs.getString(1) + "," + rs.getString(2) + "," + rs.getInt(3)+","
+rs.getString(4)+","+rs.getString(5)+","+rs.getString("tel")+","
+rs.getString("email")+","+rs.getString("address")
);
}
rs.close();
stmt.close();
} catch (SQLException var15) {
var15.printStackTrace();
} finally {
try {
if (conn2 != null) {
conn2.close();
}
} catch (SQLException var13) {
var13.printStackTrace();
}
}
System.out.println("查询完毕退回操作界面......");
break;
case 3:
try {
Class.forName("com.mysql.jdbc.Driver"); //注册驱动
} catch (ClassNotFoundException var14) {
System.out.println("注册驱动失败!");
var14.printStackTrace();
return;
}
System.out.println("请输入学生姓名:");
String stuname = input.next();
Connection conn3 = null;
try {
conn3 = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "123456");
Statement stmt = conn3.createStatement();
System.out.println("创建Statement成功!");
ResultSet rs = stmt.executeQuery("select ID, name from student where name ="+"'"+stuname+"'");
while(rs.next()) {
System.out.println(
rs.getString(1) + "," + rs.getString(2)
);
}
rs.close();
stmt.close();
} catch (SQLException var15) {
var15.printStackTrace();
} finally {
try {
if (conn3 != null) {
conn3.close();
}
} catch (SQLException var13) {
var13.printStackTrace();
}
}
System.out.println("查询完毕退回操作界面......");
break;
case 4:
System.out.println("查询完毕退回操作界面......");
break;
}
}
//添加学生界面
public void loadingadd(){
System.out.println("**********************请选择要操作信息对应的数字*********************");
System.out.println("请输入学生ID:");
String ID = input.next();
System.out.println("请输入学生姓名:");
String name = input.next();
System.out.println("请输入学生年龄:");
int age = input.nextInt();
System.out.println("请输入学生性别:");
String sex = input.next();
System.out.println("请输入学生年级:");
String grade = input.next();
System.out.println("请输入学生电话:");
String tel = input.next();
System.out.println("请输入学生邮箱:");
String email = input.next();
System.out.println("请输入学生地址:");
String address = input.next();
try {
Class.forName("com.mysql.jdbc.Driver"); //注册驱动
} catch (ClassNotFoundException var14) {
System.out.println("注册驱动失败!");
var14.printStackTrace();
return;
}
String url = "jdbc:mysql://localhost:3306/mysql";
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "123456");
Statement stmt = conn.createStatement();
System.out.println("创建Statement成功!");
boolean rs = stmt.execute("insert into student values('"+ID+"','"+name+"',"+age+",'"+sex+"','"+grade+"','"+tel+"','"+email+"','"+address+"')");
//关闭通道
stmt.close();
} catch (SQLException var15) {
var15.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException var13) {
var13.printStackTrace();
}
}
System.out.println("添加成功退回操作界面......");
}
//删除学生界面
public void loadingdel(){
System.out.println("**********************请选择要操作信息对应的数字*********************");
System.out.println("请输入要删除的学生ID:");
String ID = input.next();
try {
Class.forName("com.mysql.jdbc.Driver"); //注册驱动
} catch (ClassNotFoundException var14) {
System.out.println("注册驱动失败!");
var14.printStackTrace();
return;
}
String url = "jdbc:mysql://localhost:3306/mysql";
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "123456");
Statement stmt = conn.createStatement();
System.out.println("创建Statement成功!");
boolean rs = stmt.execute("delete from student where ID = "+"'"+ID+"'"+"");
//关闭通道
stmt.close();
} catch (SQLException var15) {
var15.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException var13) {
var13.printStackTrace();
}
}
System.out.println("删除成功退回操作界面......");
}
//修改学生界面
public void loadalter(){
System.out.println("**********************请选择要操作信息对应的数字*********************");
System.out.println("请输入要修改的ID:");
String ID = input.next();
System.out.println("请输入要修改的属性:");
String porperty = input.next();
System.out.println("请输入要修改的内容:");
String things = input.next();
try {
Class.forName("com.mysql.jdbc.Driver"); //注册驱动
} catch (ClassNotFoundException var14) {
System.out.println("注册驱动失败!");
var14.printStackTrace();
return;
}
String url = "jdbc:mysql://localhost:3306/mysql";
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "123456");
Statement stmt = conn.createStatement();
System.out.println("创建Statement成功!");
boolean rs1 = stmt.execute("update student set "+porperty+" = "+"'"+things+"'"+" where ID ="+"'"+ID+"'"+"");
stmt.close();
} catch (SQLException var15) {
var15.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException var13) {
var13.printStackTrace();
}
}
System.out.println("修改成功退回操作界面......");
}
public void loadingexit(){
System.out.println("走好不送");
}
}