文章目录
- 前提
- 一、运行效果
- 二、Text实现类
- 三、Manage选择类
- 四、StudentWay学生方法类
- 五、StudnetSql数据库类
前题
例1为无使用GUI图形界面,例2使用GUI图形界面!
首先自己的JDBC驱动已经接好了,连接自己的数据库没有问题。连接数据库可以看其他博主教程,不是很难。
其次我用的是idea+SQL Server数据库。
最后我的数据的库名称为stu,表名称为student,可以自己创建一张,内容如下:
一、运行效果
所需四个类如下:
二、Text实现类
Text为实现所有类,含有main方法,源代码如下:
package 自我课设;
public class Text {
public static void main(String[] args) {
Manage manage = new Manage();
manage.manager();
}
}
三、Manage选择类
用于选择6种方法,运用case来选择,源代码如下:
package 自我课设;
import java.util.*;
public class Manage {
public void manager(){
StudentWay studentWay = new StudentWay();
Scanner in = new Scanner(System.in);
int flag = 0;
while(true) {
studentWay.printManage();
System.out.println(" 请输入要执行的操作:");
flag = in.nextInt();
in.nextLine();
if(flag == 6) break;
else {
switch (flag) {
case 1://查询全部
studentWay.selectStudent();
break;
case 2://添加信息
studentWay.addStudent();
break;
case 3://删除学生信息
studentWay.deleteStudent();
break;
case 4://修改学生信息
studentWay.updateStudent();
break;
case 5://查询相关信息
studentWay.selectOther();
break;
}
}
}
}
}
四、StudentWay学生方法类
写好增删查改的方法给到Manage去调用,源代码如下:
package 自我课设;
import java.util.Scanner;
public class StudentWay {
StudentSql sql = new StudentSql();
Scanner in = new Scanner(System.in);
String sno = "", sname = " ", sex = " ", sage = " ", dept = " ";
//输出界面图
public void printManage() {
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.退出 ");
System.out.println("--------------------------------------------------------");
}
//查询学生信息
public void selectStudent() {
System.out.println("查询全体学生信息:");
System.out.print(sql.QuerySQL());
}
//删除学生信息
public void deleteStudent() {
System.out.println("请输入要删除的学生的学号:");
sno = in.nextLine();
System.out.print(sql.delete_student(sno));
}
//添加学生信息
public void addStudent() {
System.out.println("请输入要添加的学生的信息(以单个空格隔开):");
String str = in.nextLine();
String[] S = str.split(" ");
sno = S[0];
sname = S[1];
sex = S[2];
sage = S[3];
dept = S[4];
System.out.print(StudentSql.insert_student(sno, sname, sex, sage, dept));
}
//修改学生信息
public void updateStudent() {
System.out.println("请输入要修改的学生的学号:");
sno = in.nextLine();
System.out.println("请输入学生姓名、性别、年龄、学院,以单个空格隔开(学号不可修改):");
String str2 = in.nextLine();
String[] S2 = str2.split(" ");
sname = S2[0];
sex = S2[1];
sage = S2[2];
dept = S2[3];
StudentSql.update_student(sno, sname, sex, sage, dept);
System.out.println("修改成功!");
}
//查询分类的学生信息
public void selectOther() {
System.out.println(" 1.按学院查询 ");
System.out.println(" 2.按学号查询 ");
System.out.println(" 3.按性别查询 ");
int FLG = Integer.parseInt(in.nextLine());
//in.nextInt();
switch (FLG) {
case 1://按学院查询
System.out.println("要查询的学院:");
String temp_xueyuan = in.nextLine();
System.out.print(sql.QuerySQL_dept(temp_xueyuan));
break;
case 2://按学号查询
System.out.println("要查询学生的学号:");
String temp_sno = in.nextLine();
System.out.print(sql.QuerySQL_sno(temp_sno));
break;
case 3://按性别查询
System.out.println("要查询的性别:");
String temp_sex = in.nextLine();
System.out.print(sql.QuerySQL_sex(temp_sex));
break;
}
}
}
五、StudnetSql数据库类
StudentSql类写数据库的语句对数据库直接进行操作,然后给StudnetWay调用,源代码如下:
package 自我课设;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class StudentSql
{
//连接数据库
private static Connection getSQLConnection(String db,String user, String pwd)
{
Connection con = null;
try
{
//Class.forName("net.sourceforge.jtds.jdbc.Driver"); 查看驱动器JDBC是否导入成功
//con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + ip + ":1433/" + db + ";charset=utf8", user, pwd);
//jdbc:jtds:sqlserver://localhost:1433/dbname
//解决输出中文乱码
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;encrypt=false;DatabaseName="+ db , user, pwd);
}catch (SQLException e) {
e.printStackTrace();
}
return con;
}
//查询学生信息
public static String QuerySQL()
{
String result = "";
try
{
Connection conn = getSQLConnection("stu","sa","123456");
String sql = "select *from student";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
System.out.println("学号 姓名 性别 年龄 学院");
//System.out.println(rs);
while (rs.next())
{//学号、姓名、性别、年龄、学院
String s1 = rs.getString("sno");
String s2 = rs.getString("sname");
String s3 = rs.getString("ssex");
String s4 = rs.getString("sage");
String s5 = rs.getString("dept");
result += s1 + " " + s2 + " " + s3 + " " + s4+ " " + s5+" "+"\n";
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
result += "数据输入异常!" + e.getMessage()+"\n";
}
return result;
}
//添加学生信息
public static String insert_student(String sno, String name, String sex, String sage,String dept)
{//学号、姓名、性别、年龄、学院
String result = "";
try
{
Connection conn = getSQLConnection("stu","sa","123456");
String sql = "insert into student values ('"+sno+"','"+name+"','"+sex+"','"+sage+"','"+dept+"');";
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
result+="添加成功!"+"\n";
} catch (SQLException e)
{
e.printStackTrace();
result += "数据输入异常!" + e.getMessage()+"\n";
}
return result;
}
//删除学生信息
public static String delete_student(String sno)
{//学号、姓名、性别、年龄、学院
String result = "";
try
{
Connection conn = getSQLConnection("stu","sa","123456");
String sql = "delete student where sno = " + sno;
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
result+="删除成功!\n";
} catch (SQLException e)
{
e.printStackTrace();
result += "数据输入异常!" + e.getMessage()+"\n";
}
return result;
}
//按学号修改
public static String update_student(String sno, String name, String sex, String sage,String dept){
//学号、姓名、性别、年龄、学院
String result = "";
try
{
Connection conn = getSQLConnection("stu","sa","123456");
String sql = "update student set sname='"+name+"',ssex='"+sex+"',sage='"+sage+"',dept='"+dept+"'" +
" where sno='"+sno+"' ";
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
result+="修改成功!"+"\n";
} catch (SQLException e)
{
e.printStackTrace();
result += "数据输入异常!" + e.getMessage()+"\n";
}
return result;
}
//按学号查询
public static String QuerySQL_sno(String sno)
{
String result = "";
try
{
Connection conn = getSQLConnection("stu","sa","123456");
String sql = "select *from student where sno = '"+ sno+"';";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next())
{//学号、姓名、性别、年龄、学院
String s1 = rs.getString("sno");
String s2 = rs.getString("sname");
String s3 = rs.getString("ssex");
String s4 = rs.getString("sage");
String s5 = rs.getString("dept");
result += s1 + " " + s2 + " " + s3 + " " + s4+ " " + s5+" "+"\n";
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
result += "数据输入异常!" + e.getMessage()+"\n";
}
return result;
}
//按性别查询
public static String QuerySQL_sex(String sex)
{
String result = "";
try
{
Connection conn = getSQLConnection("stu","sa","123456");
String sql = "select * from student where ssex = '"+sex+"';";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next())
{//学号、姓名、性别、年龄、学院
String s1 = rs.getString("sno");
String s2 = rs.getString("sname");
String s3 = rs.getString("ssex");
String s4 = rs.getString("sage");
String s5 = rs.getString("dept");
result += s1 + " " + s2 + " " + s3 + " " + s4+ " " + s5+" "+"\n";
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
result += "数据输入异常!" + e.getMessage()+"\n";
}
return result;
}
//按学院查询
public static String QuerySQL_dept(String dept)
{
String result = "";
try
{
Connection conn = getSQLConnection("stu","sa","123456");
String sql = "select * from student where dept = '"+ dept +"';";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
//System.out.println(rs);
while (rs.next())
{//学号、姓名、性别、年龄、学院
String s1 = rs.getString("sno");
String s2 = rs.getString("sname");
String s3 = rs.getString("ssex");
String s4 = rs.getString("sage");
String s5 = rs.getString("dept");
result += s1 + " " + s2 + " " + s3 + " " + s4+ " " + s5+" "+"\n";
// System.out.println(s1 + " - " + s2);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
result += "数据输入异常!" + e.getMessage()+"\n";
}
return result;
}
}