-------------------------------------------------------------------------------------------------------------------------------------------------------------

必要的四个连接数据库的字符串:

driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/stu
user=root
password=lxn123

#driver=oracle.jdbc.driver.OricerDriver
#jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
#user=system
#password=lxn123

-------------------------------------------------------------------------------------------------------------------------------------------------------------

person类:

package com.atguigu.javatest;
/*
* FlowID:int,流水号
* type:int ,英语四六级
* IDcard:varchar(18),身份证号码
* examcard:varchar(15),考试证号
* studentname:varchar(20),学生姓名
* localtion:varchar(20),区域
* grade:int,成绩
*/
public class Person {
private int flowId;
private int type;
private String idCard;
private String examCard;
private String studentName;
private String localtion;
private int grade;

public Person() {
  super();
}

public Person(int flowId, int type, String idCard, String examCard, String studentName, String localtion,
  int grade) {
  super();
  this.flowId = flowId;
  this.type = type;
  this.idCard = idCard;
  this.examCard = examCard;
  this.studentName = studentName;
  this.localtion = localtion;
  this.grade = grade;
}

public int getFlowId() {
return flowId;
}
public void setFlowId(int flowId) {
this.flowId = flowId;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getExamCard() {
return examCard;
}
public void setExamCard(String examCard) {
this.examCard = examCard;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getLocaltion() {
return localtion;
}
public void setLocaltion(String localtion) {
this.localtion = localtion;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}

@Override
public String toString() {
return "Person [flowId=" + flowId + ", type=" + type + ", idCard=" + idCard + ", examCard=" + examCard
+ ", studentName=" + studentName + ", localtion=" + localtion + ", grade=" + grade + "]";
}

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------

主页面及实现的功能:

package com.atguigu.javatest;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import java.util.Scanner;

import javax.sound.midi.SysexMessage;

import org.junit.Test;

/*连接数据库,并实现增删改查操作的习题
* 首先导包,然后创建文件jdbc.properties,创建数据库lxn,表test,输入数据
* FlowID:int,流水号
* type:int ,英语四六级
* IDcard:varchar(18),身份证号码
* examcard:varchar(15),考试证号
* studentname:varchar(20),学生姓名
* location:varchar(20),区域
* grade:int,成绩
* */
public class TestConnection {
static Scanner input=new Scanner(System.in);
static boolean bb=false;

********************************************************************
//连接数据库方法
public static Connection getConnection() throws Exception{
//四连接数据必不可少的
String driverClass=null;
String jdbcUrl=null;
String user=null;
String password=null;

InputStream in=
TestConnection.class.getClassLoader().getResourceAsStream("jdbc.properties");
//其中getClass与TestConnection.classh互换使用
Properties properties=new Properties();
properties.load(in);

driverClass=properties.getProperty("driver");
jdbcUrl=properties.getProperty("jdbcUrl");
user=properties.getProperty("user");
password=properties.getProperty("password");

// System.out.println(driverClass+jdbcUrl+user+password);
Driver driver=(Driver)Class.forName(driverClass).newInstance();
Properties info=new Properties();
info.put("user", "root");
info.put("password", "lxn123");
Connection connection=driver.connect(jdbcUrl, info);
return connection;
}
//测试类
public static void testGetConn() throws Exception{
System.out.println(getConnection());
}
*****************************************************************************
//读取数据库数据,可实现增删改,
public static void testStatement(String sql) throws Exception{
//String sql为插入的sql语句
Connection connection=null;
Statement statement=null;
try {
//获取数据库连接
// String sql="insert into test(studentName,type,idCard,examCard,localtion,grade) "
// + "values('studentName','4','idCard','examCard','localtion','93')";
connection=getConnection();
statement=connection.createStatement();
statement.executeUpdate(sql);

} catch (Exception e) {
e.printStackTrace();
}finally{
if(statement!=null){
statement.close();
}
if(connection!=null){
connection.close();
}
}
}

***************************************************************************

//读取数据库数据,实现查找功能、
public static void testResultSet(String sql) throws Exception{
Connection connection=null;
Statement statement=null;
ResultSet resultset=null;

try {
connection=getConnection();

statement=connection.createStatement();
// String sql="select flowId,type,idCard,examCard,studentName,localtion,grade from test";
resultset=statement.executeQuery(sql);
while(resultset.next()){
//面向对象的方法实现
Person person =new Person(resultset.getInt(1),
resultset.getInt(2),
resultset.getString(3),
resultset.getString(4),
resultset.getString(5),
resultset.getString(6),
resultset.getInt(7));
System.out.println(person.getFlowId()
+"\t"+person.getType()
+"\t"+person.getIdCard()
+"\t"+person.getExamCard()
+"\t"+person.getStudentName()
+"\t"+person.getLocaltion()
+"\t"+person.getGrade());
bb=true;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
testClose(connection,statement,resultset);
}
}
//Connection,Statement,ResultSet关闭数据库的方法
public static void testClose(Connection connection,Statement statement,ResultSet resultset) throws Exception{
if(resultset!=null){
resultset.close();
}
if(statement!=null){
statement.close();
}
if(connection!=null){
connection.close();
}
}

**********************************************************************************
//插入一个新的学生信息
public static void insert() throws Exception{
Person person=new Person();
System.out.println("请输入流水号:");
int flowId=input.nextInt();
person.setFlowId(flowId);
//person.setFlowId(input.nextInt());

System.out.println("请输入英语四六级类型:");
int type=input.nextInt();
person.setType(type);
//person.setType(input.nextInt());

System.out.println("请输入身份证号码:");
String idCard=input.next();
person.setIdCard(idCard);
//person.setIdCard(input.next());

System.out.println("请输入考试证号:");
String examCard=input.next();
person.setExamCard(examCard);
//person.setExamCard(input.next());

System.out.println("请输入学生姓名:");
String studentName=input.next();
person.setStudentName(studentName);
//person.setStudentName(input.next());

System.out.println("请输入学生住宅所在地:");
String localtion=input.next();
person.setLocaltion(localtion);
//person.setLocaltion(input.next());

System.out.println("请输入学生成绩:");
int grade=input.nextInt();
person.setGrade(grade);
//person.setGrade(input.nextInt());

//get方法把输入的数据获取
String sql="insert into test "
+ "values("+person.getFlowId()
+","+person.getType()
+",'"+person.getIdCard()
+"','"+person.getExamCard()
+"','"+person.getStudentName()
+"','"+person.getLocaltion()
+"',"+person.getGrade()+")";
//数据中的int类型不用,而字符串String类型用' '单引号
TestConnection.testStatement(sql);
System.out.println("该学生信息输入成功!!");
}

//数据库数据的查询:查询指定的学生学号,是否存在
public static void select() throws Exception{
Person person=new Person();
System.out.println("请输入要查询的考试证号:");
String examCard=input.next();
// person.setExamCard(examCard);
String sql="select flowId,type,idCard,examCard,studentName,localtion,grade "
+ "from test where examCard='"+examCard+"'";
System.out.println("FlowID"+"\t"
+"type"+"\t"
+"IDcard"+"\t"
+"examcard"+"\t"
+"studentname"+"\t"
+"location"+"\t"
+"grade");

testResultSet(sql);
if(bb!=true){
System.out.println("输入的学生的学号不存在!!");
}
}

***************************************************************************************
//数据库中数据的删除
public static void delect() throws Exception{
System.out.println("请输入要删除学生的学号:");
String examCard=input.next();
String sql="delete from test where examCard='"+examCard+"'";
String sql1="select flowId,type,idCard,examCard,studentName,localtion,grade"
+ " from test where examCard='"+examCard+"'";

testResultSet(sql1);//判断examCard是否存在
if(bb!=false){
System.out.println("您确定要删除该学生的信息吗??");
System.out.println("1.确定----0.取消");
int n=input.nextInt();
switch(n){
case 1:testStatement(sql);
System.out.println("删除成功!!");
break;
case 0:break;
default :System.out.println("输入错误!!");
}
}
else{
System.out.println("要删除的学生的学号不存在!!!");
}
}

******************************************************************************

//修改数据库中的数据,但是修改的是指定的属性的值
//修改时,以

public static void update() throws Exception{

// System.out.println("0.流水号--1.英语四六级--2.身份证号码--3.考试证号--4.学生姓名--5.区域--6.成绩");
// System.out.println("请输入要按什么属性修改:");
// int m=input.nextInt();
// System.out.println("输入要按该属性修改的值:");
// int m1=input.nextInt();
//
// System.out.println("请输入要修改的属性:");
// int n=input.nextInt();
// System.out.println("请输入要修改的属性的值:");
// int n1=input.nextInt();
// String []str=new String[7];
// str[0]="flowId";str[1]="type";str[2]="idCard";str[3]="examCard";
// str[4]="studentName";str[5]="localtion";str[6]="grade";
//
//

System.out.println("请输入要修改信息学生的学号:");
String examCard=input.next();
System.out.println("请输入要修改属性的值是:");
String idCard=input.next();
String sql1="select flowId,type,idCard,examCard,studentName,localtion,grade"
+ " from test where where str[m]='"+examCard+"'";
//UPDATE test SET idCard='123564' WHERE examCard='qq';
String sql="update test set idCard='"+idCard+"' where examCard='"+examCard+"'";
testResultSet(sql1);//判断输入的examCard是否存在
if(bb!=false){
System.out.println("您确定要修改该学生的信息吗??");
System.out.println("1.确定----0.取消");
int t=input.nextInt();
switch(t){
case 1:testStatement(sql);
System.out.println("修改成功!!");
break;
case 0:break;
default :System.out.println("输入错误!!");
}
}
else{

}
}
******************************************************************************************
public static void main(String[] args) throws Exception {
System.out.println("*****************欢迎进入学生管理系统**********************");
while(true){
System.out.println("1.输入新的学生信息!\n"
+"2.查询学生信息!\n"
+"3.删除学生信息!\n"
+"4.修改学生信息!\n"
+"0.退出系统!");
System.out.println("------------------------------------------------------");
System.out.println("请您输入要操作的选项:");
int n=input.nextInt();
switch(n){
case 1:insert();break;
case 2:select();break;
case 3:delect();break;
case 4:update();break;//功能不够灵活,可以实现,但是比较死板
case 0:System.out.println("系统已经退出了!!!!");System.exit(0);
default :System.out.println("您输入错误,请重新输入!!!!");
System.out.println("------------------------------------------------------");
}
}
}
}

转载于:https://www.cnblogs.com/lxnlxn/p/5768108.html

学生信息管理系统(连接数据库,面向对象的方法实现学生信息的增删改查操作)...相关推荐

  1. java web期末课程设计 学生成绩管理系统(mysql,jstl+el,Javabean)+增删改查,session域+servlet(基础易理解)

    学生成绩管理系统 一.实验目的 为了更好的学习理解JavaWeb,创建了此系统.此系统可以帮助学习Java web基础,特别是javaweb入门,此系统使用大部分Java web基础知识.Java w ...

  2. Python面向对象编程案例:封装数据库增删改查操作

    问题描述:编写一个类,封装对SQLite数据库的增删改查操作,使得数据库操作更加友好,类的使用者不需要了解SQL语句的语法规则,只需要了解类的接口即可. 思考下面的问题,尝试着写一写,然后到达文末查看 ...

  3. 用 Vue 实现学生信息管理系统的增删改查操作,模拟数据库操作(但并没有连接数据库)

    Ⅰ.项目准备与解题思路: 1.项目准备: 其一.要有写 HTML + CSS + JS 的软件: (如:VSCode 工具): 其二.要提前下载好 Vue 环境,因为在项目中会引用 'vue.js' ...

  4. 商品品牌信息的增删改查操作步骤_图书信息管理系统

    不仅仅是图书信息管理系统 基于双链表,采用面向对象编程方法制作的图书管理系统 来源微信公众号: 不仅仅是图书信息管理系统​mp.weixin.qq.com 效果演示 框架结构 数据层:双链表管理 核心 ...

  5. 学生管理系统(Java版)(普通版(增删改查)、增强版(登录、注册、忘记密码))

    普通版需求如下: 学生管理系统分为四个部分:增删改查 主菜单: 增部分:(在增加前,我们要判断id的唯一性,因此我们可以遍历ArrayList数组,再调用String的equals方法,进行判断) 删 ...

  6. SpringMVC-RestfulCRUD || 员工信息表增删改查操作的具体实现

    SpringMVC-RestfulCRUD 利用SpringMVC做一个CRUD(增删改查)符合Rest风格的: C:Create:创建 R:Retrieve:查询 U:Update:更新 D:Del ...

  7. 商品品牌信息的增删改查操作步骤_javaweb09-Servlet增删改查

    学习笔记是参考的how2j 本章笔记的目的是介绍如何与JDBC结合,通过servlet对数据库中的数据进行增.删.改.查. 一.前期准备 1.新建一个Dynamic Web Project 步骤为:f ...

  8. Java实现一个学生成绩管理系统,要求存储学生信息并进行增删改查操作。

    这是我刚学完Java封装继承的时候写的一个小案例,还不会使用集合,就用数组硬写了出来,但数组的缺点是不能改变数组的长度,虽然可以写个方法新建长度加一的数组然后把旧的数组装进去,但是真的好麻烦还没必要. ...

  9. 医院信息管理系统(Python与MySQL数据库的连接与相关增删改查操作)

    题目意义 医院信息管理是一项琐碎.复杂而又十分细致的工作,这关系到医院体系能否运行起来这一关乎国民健康水平的重大问题.我们只有利用好了医院中每个医生.护士的各项资源,才能使得医院系统能够有序而条理的进 ...

最新文章

  1. BDD100K:最经典大规模、多样化的自动驾驶视频数据集
  2. 壕!电子科大计算机学院助理教授年薪50万起步, 100万安家费+150万科研启动金!...
  3. 计算机英语protocols,计算机网络协议词汇Protocols
  4. python replace函数用法_Python实现的从右到左字符串替换方法示例
  5. 使用js冒泡实现点击空白处关闭弹窗
  6. LeetCode Construct Quad Tree(dfs)
  7. LeetCode 188. 买卖股票的最佳时机 IV(动态规划)
  8. 大四阶段的社会实践的主要目的是_大四寒假社会实践报告1500字范文
  9. 基于JAVA+SpringMVC+Mybatis+MYSQL的bbs论坛管理系统
  10. [LCS]LCS应用案例--SipSnoop概述。
  11. 少量数据通过Excel表格数据导入server SqlServer查询
  12. opencv旋转矩形定义以及求交叉面积
  13. Ubuntu16.04/linux系统旋转显示器屏幕即竖屏显示(亲测)
  14. java定义一个周长类三角形_point类 三点的三角形的周长、面积 编程求解矩形和圆面积 java 三角形的定义...
  15. 5个很少被提到但能提高NLP工作效率的Python库
  16. netlogon 服务未启动
  17. ibmx3400M3服务器装系统教程,免费安装系统!IBM x3400 M3不足1万2
  18. img标签无图片或者图片url错误时显示默认图片
  19. 一个非常实用的漂浮广告代码(强烈推荐)
  20. vscode之plantUML流程图

热门文章

  1. 向上滚动tabBar隐藏向下显示
  2. 企业网络推广中关键词“出镜率”高会影响企业网络推广吗?
  3. 浅析网站标题优化该如何进行?
  4. android 取消和svn关联_Android Studio如何取消与SVN的关联
  5. 天线巴伦制作和原理_一种基于LTCC技术的新型Marchand巴伦滤波器
  6. python列表导出csv_将Python列表导出到csv
  7. java+caching+system_浅谈Spring boot cache使用和原理
  8. Vim设置括号自动补全和快速跳出
  9. 使用GAN进行异常检测——可以进行网络流量的自学习哇,哥哥,人家是半监督,无监督的话,还是要VAE,SAE。...
  10. 每秒100W请求,12306秒杀业务,架构如何优化?