學生信息管理系統(連接數據庫,面向對象的方法實現學生信息的增刪改查操作)


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

必要的四個連接數據庫的字符串:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM