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

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

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

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("------------------------------------------------------");
}
}
}
}

posted @ 2016-08-13 15:35  G-&-D  阅读(2967)  评论(0编辑  收藏  举报