用JAVA实现简单学生信息管理系统

用JAVA实现简单学生信息管理系统

第一部分:学生基本信息类 StudentPO.java

package studentList;

class StudentPO {
     
	private String id;
	private String name;
	private String gender;
	private String phone;
	private String email;
	private String college;
	private String major;
	
	public String getId(){
     
		return id;
	}
	
	public void setId(String id){
     
		this.id = id;
	}
	
	public String getName(){
     
		return name;
	}
	
	public void setName(String name){
     
		this.name = name;
	}
	
	public String getGender(){
     
		return gender;
	}
	
	public void setGender(String gender){
     
		this.gender = gender;
	}
	
	public String getPhone(){
     
		return phone;
	}
	
	public void setPhone(String phone){
     
		this.phone = phone;
	}
	
	public String getEmail(){
     
		return email;
	}
	
	public void setEmail(String email){
     
		this.email = email;
	}
	
	public String getCollege(){
     
		return college;
	}
	
	public void setCollege(String college){
     
		this.college = college;
	}
	
	public String getMajor(){
     
		return major;
	}
	
	public void setMajor(String major){
     
		this.major = major;
	}
	
	public String toString(){
      //重写toString方法
		
		return id+"      "+name+"        "+gender+"         "+phone+"         "+email+"         "+college+"         "+major;
		
	}
}

第二部分:学生集合类 StudentSet.java

package studentList;

import java.util.Iterator;
import java.util.TreeSet;

import studentList.StudentComp;

class StudentSet extends TreeSet<StudentPO>{
     
	
	private static final long serialVersionUID = 1L;
	
	public StudentSet(){
     
		super(new StudentComp());
	}
	
	public String[][] list() {
     
		String list[][] = new String[this.size()][7];
		Iterator<StudentPO> itr = this.iterator();
		int i = 0;
		while(itr.hasNext()){
     
			StudentPO student = itr.next();
			list[i][0] = student.getId();
			list[i][1] = student.getName();
			list[i][2] = student.getGender();
			list[i][3] = student.getPhone();
			list[i][4] = student.getEmail();
			list[i][5] = student.getCollege();
			list[i][6] = student.getMajor();
			i++;
		}
		return list;
	}
}

第三部分:实现按学号排序 StudentComp.java

package studentList;

//Use a custom comparator.
import java.util.*;

import studentList.StudentPO;

//A reverse comparator for strings.
class StudentComp implements Comparator<Object>{
     
	public int compare(Object a, Object b) {
     
	    StudentPO aStudent = (StudentPO) a;
	    StudentPO bStudent = (StudentPO) b;
	    return aStudent.getId().compareTo(bStudent.getId());
	}
}

第四部分:数据读写管理 DataAccessManager.java

package studentList;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class DataAccessManager {
     
	private Connection dbConnection = null;
	private String jdbcDriver ;
	private String dbURL;
	private String dbUser;
	private String dbPassword;
	private File excelFile = null;

	private File xmlFile = null;
	private String filePath = "src/studentList/";

	public DataAccessManager() {
     		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
     
			factory.setIgnoringElementContentWhitespace(true);
			DocumentBuilder db = factory.newDocumentBuilder();
			File configFile = new File(filePath + "config.xml");
			Document xmldoc = db.parse(configFile);
			Element root = xmldoc.getDocumentElement();			
			Node attrNode;
			for (int i = 0; i < root.getChildNodes().getLength(); i++) {
     
				attrNode = root.getChildNodes().item(i);
				if (attrNode.getNodeName().equals("jdbcDriver")) {
     
					jdbcDriver=attrNode.getFirstChild().getNodeValue();
				}else if (attrNode.getNodeName().equals("dbURL")) {
     
					dbURL=attrNode.getFirstChild().getNodeValue();
				}else if (attrNode.getNodeName().equals("dbUser")) {
     
					dbUser=attrNode.getFirstChild().getNodeValue();
				}else if (attrNode.getNodeName().equals("dbPassword")) {
     
					dbPassword=attrNode.getFirstChild().getNodeValue();
				}
				
			}

		} catch (ParserConfigurationException error) {
     
			error.printStackTrace();
		} catch (SAXException error2) {
     
			error2.printStackTrace();
		} catch (FileNotFoundException error3) {
     
			error3.printStackTrace();
		} catch (IOException error4) {
     
			error4.printStackTrace();
		}

	}

	public DataAccessManager(String filePath) {
     
		this.filePath = filePath;

	}

	public DataAccessManager(String jdbcDriver, String dbUrl, String dbUser,
			String dbPassword) {
     
		this.jdbcDriver = jdbcDriver;

	}

	public File openExcelFile() {
     
		if (excelFile == null) {
     
			try {
     
				excelFile = new File(filePath + "studentList.xls");

			} catch (Exception e) {
     
				System.out.println("open excel file error:" + e);
			}
		}
		return excelFile;
	}

	public File openXMLFile() {
     
		if (xmlFile == null) {
     
			try {
     
				xmlFile = new File(filePath + "studentList.xml");

			} catch (Exception e) {
     
				System.out.println("open xml file error:" + e);
			}
		}
		return xmlFile;
	}

	public Connection getConnection() {
     
		if (dbConnection == null) {
     
			try {
     
				Class.forName(jdbcDriver).newInstance();
				dbConnection = DriverManager.getConnection(dbURL, dbUser,
						dbPassword);
			} catch (Exception e) {
     
				System.out.println("open databse connection error:" + e);
			}
		}
		return dbConnection;
	}

	public void closeConnection() {
     
		try {
     
			dbConnection.close();
		} catch (Exception e) {
     
			System.out.println("close databse connection error:" + e);
		}
	}

}

第五部分:Excel、Xml、MySQL操作

package studentList;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

class StudentDAO {
     
	private DataAccessManager manager;
	
	//为成员变量赋值
	public StudentDAO(DataAccessManager manager){
     
		this.manager = manager;
	}
	
	//从xml文档中加载Student信息,返回值类型为StudentSet
	public StudentSet loadStudentsFromXMLFile(){
     
		StudentSet students = new StudentSet();
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		try{
     
			factory.setIgnoringElementContentWhitespace(true);
			DocumentBuilder db=factory.newDocumentBuilder();
			Document xmldoc=db.parse(manager.openXMLFile());
			Element root=xmldoc.getDocumentElement();
			StudentPO student;
			Node objNode,attrNode;
			for(int i=0;i<root.getChildNodes().getLength();i++){
     
				objNode=root.getChildNodes().item(i);
				if(objNode.getNodeName().equals("student")){
     				
					student = new StudentPO();					
					for (int j=0;j<objNode.getChildNodes().getLength();j++){
     
						attrNode=objNode.getChildNodes().item(j); 
						if(attrNode.getNodeName().equals("id")){
     
							student.setId(attrNode.getFirstChild().getNodeValue());
						}
						if(attrNode.getNodeName().equals("name")){
     
							student.setName(attrNode.getFirstChild().getNodeValue());
						}
						if(attrNode.getNodeName().equals("gender")){
     
							student.setGender(attrNode.getFirstChild().getNodeValue());
						}
						if(attrNode.getNodeName().equals("phone")){
     
							student.setPhone(attrNode.getFirstChild().getNodeValue());
						}
						if(attrNode.getNodeName().equals("email")){
     
							student.setEmail(attrNode.getFirstChild().getNodeValue());
						}
						if(attrNode.getNodeName().equals("college")){
     
							student.setCollege(attrNode.getFirstChild().getNodeValue());
						}
						if(attrNode.getNodeName().equals("major")){
     
							student.setMajor(attrNode.getFirstChild().getNodeValue());
						}
					}					
					students.add(student);
				}
			}	
		}catch (ParserConfigurationException error){
     
			error.printStackTrace();
		}catch (SAXException error2){
     
			error2.printStackTrace();
		}catch(FileNotFoundException error3)  {
     
			error3.printStackTrace();
		}catch(IOException error4)  {
     
			error4.printStackTrace();
		}
		return students;
	}
	
	
	public void writeStudentsToXmlFile(StudentSet students){
     
		TransformerFactory transFactory=TransformerFactory.newInstance();
		
		try{
     
			Transformer transformer=transFactory.newTransformer();
			transformer.setOutputProperty("indent","yes");
			DOMSource source=new DOMSource();
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
			factory.setIgnoringElementContentWhitespace(true);
			DocumentBuilder db=factory.newDocumentBuilder();
			Document xmldoc=db.newDocument();		
			Element root=xmldoc.createElement("students"); //根节点
			
			Element theStudent,theAttr;
            for (StudentPO student : students){
     
	          //--- 新建一个学生信息 ----
            	theStudent=xmldoc.createElement("student");
            	
	            theAttr=xmldoc.createElement("id");
	            theAttr.setTextContent(student.getId());
	            theStudent.appendChild(theAttr);
	            
	            theAttr=xmldoc.createElement("name");
	            theAttr.setTextContent(student.getName());
	            theStudent.appendChild(theAttr);
	            
	            theAttr=xmldoc.createElement("gender");
	            theAttr.setTextContent(student.getGender());
	            theStudent.appendChild(theAttr);
	            
	            theAttr=xmldoc.createElement("phone");
	            theAttr.setTextContent(student.getPhone());
	            theStudent.appendChild(theAttr);
	            
	            theAttr=xmldoc.createElement("email");
	            theAttr.setTextContent(student.getEmail());
	            theStudent.appendChild(theAttr);
	            
	            theAttr=xmldoc.createElement("college");
	            theAttr.setTextContent(student.getCollege());
	            theStudent.appendChild(theAttr);
	            
	            theAttr=xmldoc.createElement("major");
	            theAttr.setTextContent(student.getMajor());
	            theStudent.appendChild(theAttr);
	            
	            root.appendChild(theStudent);
            }
	        xmldoc.appendChild(root);
			source.setNode(xmldoc);
			StreamResult result=new StreamResult();
			FileOutputStream stream=new FileOutputStream(manager.openXMLFile());
			result.setOutputStream(stream);
			transformer.transform(source,result);
				
			stream.close();
		}catch (ParserConfigurationException error){
     
			error.printStackTrace();
		}catch(TransformerConfigurationException error1){
     
			error1.printStackTrace();
		}catch(TransformerException error2){
     
			error2.printStackTrace();
		}catch(FileNotFoundException error3)  {
     
			error3.printStackTrace();
		}catch(IOException error4)  {
     
			error4.printStackTrace();
		}
	}
	
	
	public StudentSet loadStudentsFromExcelFile(){
     
		StudentSet students = new StudentSet();
		try{
     
			FileInputStream inputStream=new FileInputStream(manager.openExcelFile());
			POIFSFileSystem poifsFileSystem = new POIFSFileSystem(inputStream);
		    HSSFWorkbook hssfWorkbook =  new HSSFWorkbook(poifsFileSystem);
		    HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
		    int rowstart = hssfSheet.getFirstRowNum();
		    int rowEnd = hssfSheet.getLastRowNum();
		    StudentPO student;
		    for(int i=rowstart+1;i<=rowEnd;i++){
     
		        HSSFRow row = hssfSheet.getRow(i);
		        if(null == row) continue; 
		        student = new StudentPO();
		        
		        HSSFCell cell = row.getCell(0);
		        student.setId(cell.getStringCellValue());
		        
		        cell = row.getCell(1);
		        student.setName(cell.getStringCellValue());
		        
		        cell = row.getCell(2);
		        student.setGender(cell.getStringCellValue());
		        
		        cell = row.getCell(3);
		        student.setPhone(cell.getStringCellValue());

		        cell = row.getCell(4);
		        student.setEmail(cell.getStringCellValue());
		        
		        cell = row.getCell(5);
		        student.setCollege(cell.getStringCellValue());
		        
		        cell = row.getCell(6);
		        student.setMajor(cell.getStringCellValue());
		        
		        students.add(student);
		    }
		    inputStream.close();
		}catch(FileNotFoundException error){
     
			System.out.println("excel file cannot find");
		}catch(IOException error){
     
			System.out.println(error.getMessage());
		}
		return students;
	}
	
	
	public void writeStudentsToExcelFile(StudentSet students){
     
		HSSFWorkbook workbook = null;
	    workbook = new HSSFWorkbook();	
	    int columeCount = 7;
	    HSSFSheet sheet = workbook.createSheet("通讯录");	
	    HSSFRow headRow = sheet.createRow(0);
	    String[] titleArray = {
     "学号", "名字","性别","电话","电子邮箱","学院","专业"};
	    
	    for(int m=0;m<=columeCount-1;m++){
     
	        HSSFCell cell = headRow.createCell(m);
	        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
	        sheet.setColumnWidth(m, 7000);
	        HSSFCellStyle style = workbook.createCellStyle();
	        HSSFFont font = workbook.createFont();
	        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
	        short color = HSSFColor.RED.index;
	        font.setColor(color);
	        style.setFont(font);	       
	        cell.setCellStyle(style);
	        cell.setCellValue(titleArray[m]);
	    }
	    
	    int index = 0;
	    for(StudentPO student : students){
     	        
	        HSSFRow row = sheet.createRow(index+1);
	        for(int n=0;n<=columeCount-1;n++)
	            row.createCell(n);
	        row.getCell(0).setCellValue(student.getId());
	        row.getCell(1).setCellValue(student.getName());
	        row.getCell(2).setCellValue(student.getGender());
	        row.getCell(3).setCellValue(student.getPhone());
	        row.getCell(4).setCellValue(student.getEmail());
	        row.getCell(5).setCellValue(student.getCollege());
	        row.getCell(6).setCellValue(student.getMajor());
	        index++;
	    }
	    
	    try {
     
	        FileOutputStream fileOutputStream = new FileOutputStream(manager.openExcelFile());
	        workbook.write(fileOutputStream);
	        fileOutputStream.close();
	    } catch (FileNotFoundException e) {
     
	        e.printStackTrace();
	    } catch (IOException e) {
     
	        e.printStackTrace();
	    }
	}
	
	
	public void addStudent(StudentPO student){
     
		try {
     			
			String sqlStr = "insert into student(id,name,gender,phone,email,college,major) values(?,?,?,?,?,?,?)";
			PreparedStatement prepStmt = manager.getConnection().prepareStatement(sqlStr); // create a statement
			prepStmt.setString(1, student.getId());
			prepStmt.setString(2, student.getName());
			prepStmt.setString(3, student.getGender());
			prepStmt.setString(4, student.getPhone());
			prepStmt.setString(5, student.getEmail());
			prepStmt.setString(6, student.getCollege());
			prepStmt.setString(7, student.getMajor());
			prepStmt.executeUpdate();
			
		} catch (Exception e) {
     
			System.out.println("add student error:" + e);
		}
	}
	
	public void addStudents(StudentSet students){
     
		for (StudentPO student:students){
     
			addStudent(student);
		}
	}
	
	
	public void deleteStudent(StudentPO student){
     
		try {
     
			String sqlStr = "delete from student where name=?";
			PreparedStatement prepStmt = manager.getConnection().prepareStatement(sqlStr); // create a statement
			prepStmt.setString(1, student.getName());
			prepStmt.executeUpdate();
			
		} catch (Exception e) {
     
			System.out.println("deletestudent error:" + e);
		}
	}
	
	public void deleteStudents(StudentSet students){
     
		for (StudentPO student:students){
     
			deleteStudent(student);
		}
	}
	
	
	public void updateStudent(StudentPO student){
     
		try {
     
			String sqlStr = "update student set id=?,gender=?,phone=?,email=?,college=?,major=? "
					+ " where name=?";
			PreparedStatement prepStmt = manager.getConnection().prepareStatement(sqlStr); // create a statement
			prepStmt.setString(1, student.getId());			
			prepStmt.setString(2, student.getGender());
			prepStmt.setString(3, student.getPhone());
			prepStmt.setString(4, student.getEmail());
			prepStmt.setString(5, student.getCollege());
			prepStmt.setString(6, student.getMajor());
			prepStmt.setString(7, student.getName());
			prepStmt.executeUpdate();			
		} catch (Exception e) {
     
			System.out.println("updatestudent error:" + e);
		}
	}
	
	public void updateStudents(StudentSet students){
     
		for (StudentPO student:students){
     
			updateStudent(student);
		}
	}
	
	
	public StudentPO queryStudentByName(String name) {
     
		StudentPO student = null;
		try {
     
			if (manager.getConnection() != null) {
     
				String sqlStr="SELECT * FROM student  where name=?";
				PreparedStatement prepStmt = manager.getConnection().prepareStatement(sqlStr);
				prepStmt.setString(1, name);				
				ResultSet rs = prepStmt.executeQuery();
				if (rs.next()) {
     
					student = new StudentPO();
					student.setId(rs.getString("id"));
					student.setName(rs.getString("name"));
					student.setGender(rs.getString("gender"));
					student.setPhone(rs.getString("phone"));
					student.setEmail(rs.getString("email"));
					student.setCollege(rs.getString("college"));
					student.setMajor(rs.getString("major"));
				}
			}

		} catch (Exception e) {
     
			System.out.println("queryStudent error:" + e);
		}
		return student;
	}
	
	public StudentSet queryStudents() {
     
		StudentSet students = new StudentSet();
		StudentPO student;
		try {
     
			if (manager.getConnection() != null) {
     
				Statement stmt = manager.getConnection().createStatement();
				ResultSet rs = stmt.executeQuery("SELECT * FROM student order by name");
				while (rs.next()) {
     
					student = new StudentPO();
					student.setId(rs.getString("id"));
					student.setName(rs.getString("name"));
					student.setGender(rs.getString("gender"));
					student.setPhone(rs.getString("phone"));
					student.setEmail(rs.getString("email"));
					student.setCollege(rs.getString("college"));
					student.setMajor(rs.getString("major"));
					students.add(student);
				}
			}
		} catch (Exception e) {
     
			System.out.println("queryfriend error:" + e);
		}
		return students;
	}
	
}

第六部分:测试程序 StudentTester.java

package studentList;

import java.sql.*;

class StudentTester {
     
	private DataAccessManager dbManager;
	private StudentDAO dao;
	
	public StudentTester() {
     		
		dbManager=new DataAccessManager();
		dao=new StudentDAO(dbManager);
	}
	
	public void closeConnection(){
     
		dbManager.closeConnection();
	}
	
	private void createTable(){
     
		try{
     
			Statement stmt= dbManager.getConnection().createStatement();		
			stmt.execute("create table student(id varchar(12),name varchar(12) primary key,gender varchar(20),"+"" +
				" phone varchar(20),email varchar(20),college varchar(30),major varchar(30))");
			System.out.println("Created table student");
		}catch(Exception e){
     
			System.out.println("Created table student error:"+e);
		}		
	}
	
	private void dropTable(){
     
		try{
     
			Statement stmt= dbManager.getConnection().createStatement();		
			stmt.execute("drop table student");
			System.out.println("drop table student");
		}catch(Exception e){
     
			System.out.println("drop table student error:"+e);
		}		
	}
	
	private void initExcelFile(){
     
		StudentSet students = new StudentSet();
		StudentPO student;
		
		student = new StudentPO();
		student.setId("20170001");
		student.setName("张三");
		student.setGender("男");
		student.setPhone("13800000000");
		student.setEmail("[email protected]");
		student.setCollege("信息科学与工程学院");
		student.setMajor("自动化");
		students.add(student);
		
		student = new StudentPO();
		student.setId("20160056");
		student.setName("李四");
		student.setGender("男");
		student.setPhone("13800001111");
		student.setEmail("[email protected]");
		student.setCollege("信息科学与工程学院");
		student.setMajor("电气工程与自动化");
		students.add(student);
		
		student = new StudentPO();
		student.setId("20180035");
		student.setName("王五");
		student.setGender("女");
		student.setPhone("13800002222");
		student.setEmail("[email protected]");
		student.setCollege("信息科学与工程学院");
		student.setMajor("自动化");
		students.add(student);
		
		student = new StudentPO();
		student.setId("20193622");
		student.setName("约翰");
		student.setGender("男");
		student.setPhone("13800001234");
		student.setEmail("[email protected]");
		student.setCollege("信息科学与工程学院");
		student.setMajor("测控");
		students.add(student);
	
		dao.writeStudentsToExcelFile(students);	
		System.out.println("=============初始化Excel文件=====================");
		System.out.println("       学号" + "                      姓名  " + "     性别 " + "               电话 " + "                                             电子邮件 "  + "                                                     学院 "  + "                            专业 ");
		System.out.println("--------------------------------------------------------------- ");
	
		if (students != null && students.size() > 0) {
     
			for (StudentPO one : students) {
     
				System.out.println(one.toString());
			}
		}
	}
	
	private StudentSet testLoadStudentsFromExcelFile(){
     
		System.out.println("=============加载Excel文件得到学生信息=====================");
		System.out.println("       学号" + "                      姓名  " + "     性别 " + "               电话 " + "                                             电子邮件 "  + "                                                     学院 "  + "                            专业 ");
		System.out.println("--------------------------------------------------------------- ");
		StudentSet students = dao.loadStudentsFromExcelFile();
		if (students != null && students.size() > 0) {
     
			for (StudentPO one : students) {
     
				System.out.println(one.toString());
			}
		}
		return students;
	}
	
	
	private void testWriteStudentsToXMLFile(StudentSet students){
     
		System.out.println("=============把学生信息写入XML文件=====================");
		System.out.println("       学号" + "                      姓名  " + "     性别 " + "               电话 " + "                                             电子邮件 "  + "                                                     学院 "  + "                            专业 ");
		System.out.println("--------------------------------------------------------------- ");
		
		if (students != null && students.size() > 0) {
     
			for (StudentPO one : students) {
     
				System.out.println(one.toString());
			}
		}
		dao.writeStudentsToXmlFile(students);
	}
	
	private StudentSet testLoadStudentsFromXMLFile(){
     
		System.out.println("=============加载XML文件得到学生信息=====================");
		System.out.println("--------------------------------------------------------------- ");
		
		StudentSet students = dao.loadStudentsFromXMLFile();
		if (students != null && students.size() > 0) {
     
			for (StudentPO one : students) {
     
				System.out.println(one.toString());
			}
		}
		return students;
	}
	
	
	private void testQueryRecords(){
     
		System.out.println("       学号" + "                      姓名  " + "     性别 " + "               电话 " + "                                             电子邮件 "  + "                                                     学院 "  + "                            专业 ");
		System.out.println("--------------------------------------------------------------- ");
		StudentSet students = dao.queryStudents();
		if (students != null && students.size() > 0) {
     
			for (StudentPO one : students) {
     
				System.out.println(one.toString());
			}
		}
	}
	
	private void testInsertRecord(){
     
		StudentPO student = new StudentPO();
		student.setId("20170012");
		student.setName("李六");
		student.setGender("男");
		student.setPhone("13800003333");
		student.setEmail("[email protected]");
		student.setCollege("信息科学与工程学院");
		student.setMajor("自动化");
		dao.addStudent(student);
		System.out.println();
		System.out.println("==========增加李六后的学生信息==========");			
		testQueryRecords();
	}
	
	
	private void testUpdateRecord(){
     
		StudentPO student = dao.queryStudentByName("李六");
		student.setName("李六六");
		dao.updateStudent(student);
		System.out.println();
		System.out.println("==========更新李六后的学生信息==========");			
		testQueryRecords();
	}
	
	
	private void testBactchAddRecords(StudentSet students){
     
		dao.addStudents(students);
		System.out.println();
		System.out.println("=============向数据库中批量增加如下学生信息=====================");
		testQueryRecords();
	}
	
	private void testDeleteRecord(){
     
		StudentPO student = dao.queryStudentByName("王五");
		dao.deleteStudent(student);
		System.out.println();
		System.out.println("==========删除王五后的学生信息==========");
		testQueryRecords();
	}
	
	
	public static void main(String arg[]){
     
		StudentTester daoTester = new StudentTester();
		daoTester.initExcelFile();
		
		StudentSet students = daoTester.testLoadStudentsFromExcelFile();
		daoTester.testWriteStudentsToXMLFile(students);
		students = daoTester.testLoadStudentsFromXMLFile(); //students需要创建吗???
		
		daoTester.dropTable();
		daoTester.createTable();	
		daoTester.testBactchAddRecords(students);		
		daoTester.testInsertRecord();
		daoTester.testUpdateRecord();
		daoTester.testDeleteRecord();	
		daoTester.closeConnection();
	}
	
}

第七部分:数据库配置文件 config.xml

<config>
    <jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
    <dbURL>jdbc:mysql://localhost:3306/JavaCourse</dbURL> 
    <dbUser>root</dbUser>
    <dbPassword>manager</dbPassword>
</config>

console输出结果

=============初始化Excel文件=====================
  学号         姓名       性别           电话                    电子邮件                 学院                      专业 
--------------------------------------------------------------- 
20160056      李四        男         13800001111         910000000@qq.com         信息科学与工程学院         电气工程与自动化
20170001      张三        男         13800000000         900000000@qq.com         信息科学与工程学院         自动化
20180035      王五        女         13800002222         920000000@qq.com         信息科学与工程学院         自动化
20193622      约翰        男         13800001234         920001234@qq.com         信息科学与工程学院         测控
=============加载Excel文件得到学生信息=====================
  学号         姓名       性别           电话                    电子邮件                 学院                      专业 
--------------------------------------------------------------- 
20160056      李四        男         13800001111         910000000@qq.com         信息科学与工程学院         电气工程与自动化
20170001      张三        男         13800000000         900000000@qq.com         信息科学与工程学院         自动化
20180035      王五        女         13800002222         920000000@qq.com         信息科学与工程学院         自动化
20193622      约翰        男         13800001234         920001234@qq.com         信息科学与工程学院         测控
=============把学生信息写入XML文件=====================
  学号         姓名       性别           电话                    电子邮件                 学院                      专业 
--------------------------------------------------------------- 
20160056      李四        男         13800001111         910000000@qq.com         信息科学与工程学院         电气工程与自动化
20170001      张三        男         13800000000         900000000@qq.com         信息科学与工程学院         自动化
20180035      王五        女         13800002222         920000000@qq.com         信息科学与工程学院         自动化
20193622      约翰        男         13800001234         920001234@qq.com         信息科学与工程学院         测控
=============加载XML文件得到学生信息=====================
--------------------------------------------------------------- 
20160056      李四        男         13800001111         910000000@qq.com         信息科学与工程学院         电气工程与自动化
20170001      张三        男         13800000000         900000000@qq.com         信息科学与工程学院         自动化
20180035      王五        女         13800002222         920000000@qq.com         信息科学与工程学院         自动化
20193622      约翰        男         13800001234         920001234@qq.com         信息科学与工程学院         测控
drop table student
Created table student

=============向数据库中批量增加如下学生信息=====================
  学号         姓名       性别           电话                    电子邮件                 学院                      专业 
--------------------------------------------------------------- 
20160056      李四        男         13800001111         910000000@qq.com         信息科学与工程学院         电气工程与自动化
20170001      张三        男         13800000000         900000000@qq.com         信息科学与工程学院         自动化
20180035      王五        女         13800002222         920000000@qq.com         信息科学与工程学院         自动化
20193622      约翰        男         13800001234         920001234@qq.com         信息科学与工程学院         测控

==========增加李六后的学生信息==========
  学号         姓名       性别           电话                    电子邮件                 学院                      专业 
--------------------------------------------------------------- 
20160056      李四        男         13800001111         910000000@qq.com         信息科学与工程学院         电气工程与自动化
20170001      张三        男         13800000000         900000000@qq.com         信息科学与工程学院         自动化
20170012      李六        男         13800003333         930000000@qq.com         信息科学与工程学院         自动化
20180035      王五        女         13800002222         920000000@qq.com         信息科学与工程学院         自动化
20193622      约翰        男         13800001234         920001234@qq.com         信息科学与工程学院         测控

==========更新李六后的学生信息==========
  学号         姓名       性别           电话                    电子邮件                 学院                      专业 
--------------------------------------------------------------- 
20160056      李四        男         13800001111         910000000@qq.com         信息科学与工程学院         电气工程与自动化
20170001      张三        男         13800000000         900000000@qq.com         信息科学与工程学院         自动化
20170012      李六        男         13800003333         930000000@qq.com         信息科学与工程学院         自动化
20180035      王五        女         13800002222         920000000@qq.com         信息科学与工程学院         自动化
20193622      约翰        男         13800001234         920001234@qq.com         信息科学与工程学院         测控

==========删除王五后的学生信息==========
  学号         姓名       性别           电话                    电子邮件                 学院                      专业 
--------------------------------------------------------------- 
20160056      李四        男         13800001111         910000000@qq.com         信息科学与工程学院         电气工程与自动化
20170001      张三        男         13800000000         900000000@qq.com         信息科学与工程学院         自动化
20170012      李六        男         13800003333         930000000@qq.com         信息科学与工程学院         自动化
20193622      约翰        男         13800001234         920001234@qq.com         信息科学与工程学院         测控

此外,还会输出相应的studentList.xml、studentList.xls、以及数据库中的student表单!

你可能感兴趣的:(JAVA,java,数据库,mysql,xml,excel)