利用Maven写学生信息管理系统

本文介绍如何利用Maven来开发一个学生信息管理系统。详细讲述了Main代码、PrepertiesTool配置以及DBLink数据库连接的实现,同时也涉及了IRowMapper接口在数据映射中的应用。
摘要由CSDN通过智能技术生成

Main代码:

package com.zzu.client;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import com.zzu.tool.db.DBLink;
import com.zzu.tool.db.IRowMapper;

public class Main {

	public static void main(String[] args) {
		System.out.println("*********************************");
		System.out.println("*\t\t\t\t*");
		System.out.println("*\t欢迎使用学生信息管理系统\t*");
		System.out.println("*\t\t\t\t*");
		System.out.println("*********************************");
		while (true) {
			menu();
		}
	}

	static void menu() {
		System.out.println("1、添加学生信息");
		System.out.println("2、删除学生信息");// 学号
		System.out.println("3、修改学生信息");// 地址传递
		System.out.println("4、查询学生信息");// name
		System.out.println("请输入操作,以Enter键结束:");
		Scanner scanner = new Scanner(System.in);
		int option = scanner.nextInt();
		DBLink db = new DBLink();
		switch (option) {
		case 1:{
			System.out.println("请输入学号");
			String id =scanner.next();
			String sql = "select id from user_info1 where id = ?";
			if(db.exist(sql, id)) {
				System.out.println("该学号已存在,操作中止!");
				return;
			}
			System.out.println("请输入姓名");
			String name = scanner.next();
			System.out.println("请输入电话号码");
			String mobile = scanner.next();
			System.out.println("请输入家庭住址");
			String address = scanner.next();
			sql = "insert into user_info1 (id,name,mobile,address) value (?,?,?,?)";
			if(db.update(sql, id,name,mobile,address)) {
				System.out.println("添加学生信息成功");
				return;
			}
			break;
		}
		case 2:{
			System.out.println("请输入学号");
			String id =scanner.next();
			String sql = "select id from user_info1 where id = ?";
			if(!db.exist(sql, id)) {
				System.out.println("该学号不存在,操作中止");
				return;
			}
			sql = "delete from user_info1 where id = ?";
			if(db.update(sql, id)) {
				System.out.println("删除成功");
				return;
			}
			System.out.println("删除失败");
			break;
		}
		case 3:{
			System.out.println("请输入学号");
			String id =scanner.next();
			String sql = "select id from user_info1 where id = ?";
			if(!db.exist(sql, id)) {
				System.out.println("该学号不存在,操作中止");
				return;
			}
			System.out.println("请输入性名");
			String name = scanner.next();
			System.out.println("请输入电话号码");
			String mobile = scanner.next();
			System.out.println("请输入家庭住址");
			String address = scanner.next();
			sql = "update user_info1 set name = ?,mobile = ?,address = ? where id = ?";
			if(db.update(sql, name,mobile,address,id)) {
				System.out.println("修改成功");
				return;
			}
		}
			break;
		case 4:{
			System.out.println("请输入学号");
			String id =scanner.next();
			String sql = "select id,name,mobile,address from user_info1 where id = ?";
			/*class RowMapper implements IRowMapper{

				public void rowMapper(ResultSet rs) {
					try {
						if(rs.next()) {
							String id = rs.getString("id");
							String name = rs.getString("name");
							String mobile = rs.getString("mobile");
							String address = rs.getString("address");
							System.out.println("id:"+id+","+"name:" +name+","+"mobile" +mobile+","+"address"+address+".");
						}
						System.out.println("没有找到信息");
					} catch (SQLException e) {
						
						e.printStackTrace();
					}					
				}
				
				
			};*/
			/*
			IRowMapper rowMapper = (rs)->{
				try {
					if(rs.next()) {
						String name = rs.getString("name");
						String mobile = rs.getString("mobile");
						String address = rs.getString("address");
						System.out.println("id:"+id+","+"name:" +name+","+"mobile" +mobile+","+"address"+address+".");
					}
					System.out.println("没有找到信息");
				} catch (SQLException e) {
					
					e.printStackTrace(); 
				}					
			};
			db.select(sql, rowMapper, id);*/
			
			db.select(sql,(rs)->{//逐步简化
				try {
					if(rs.next()) {
						String name = rs.getString("name");
						String mobile = rs.getString("mobile");
						String address = rs.getString("address");
						System.out.println("id:"+id+","+"name:" +name+","+"mobile" +mobile+","+"address"+address+".");
					}
					System.out.println("没有找到信息");
				} catch (SQLException e) {
					
					e.printStackTrace();
				}					
			}  , id);
			break;
		}
		default:
			System.out.println("I'm Sorry,there is not the " + option + " option,please try again.");
		}
	}
}

PrepertiesTool 代码:

package com.zzu.tool;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesTool {

	private static Properties properties = new Properties();
	
	static {
		InputStream inputStream = PropertiesTool.class.getClassLoader().getResourceAsStream("db.properties");//将db.properties变为javaIO流对象
		try {
			properties.load(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String getValue(String key) {
		return properties.getProperty(key);
	}
}

DBLink代码:

package com.zzu.tool.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.zzu.tool.PropertiesTool;



/**
 * 数据库管理工具类
 *
 * @author GaoHuanjie
 */
public class DBLink {
	
	private Logger logger = Logger.getLogger(DBLink.class);
	
	/**
	 * 获取数据库连接
	 *
	 * @author GaoHuanjie
	 */
	private Connection getConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");//加载驱动
			String userName = PropertiesTool.getValue("db.username");
			String password = PropertiesTool.getValue("db.password");
			String url = PropertiesTool.getValue("db.url");
			return DriverManager.getConnection(url, userName, password);//获取连接
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	/**
	 * 判断SQL语句是否能查出数据
	 *
	 * @author GaoHuanjie
	 */
	public boolean exist(String sql) {
		Connection connection = null;
		Statement statement =null;
		ResultSet resultSet=null;
		try {
			connection = getConnection();//获取连接
			statement = connection.createStatement();
			resultSet= statement.executeQuery(sql);//执行sql,将查询的数据存到ResultSet类型的变量中
			return resultSet.next();
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {
			close(resultSet,statement,connection);
		}
		return false;
	}
	
	/**
	 * 判断SQL语句是否能查出数据
	 *
	 * @author GaoHuanjie
	 */
	public boolean exist(String sql, Object ...params) {
		Connection connection = null;
		PreparedStatement preparedStatement =null;
		ResultSet resultSet=null;
		try {
			connection = getConnection();//获取连接
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				preparedStatement.setObject(i+1, params[i]);
			}
			resultSet= preparedStatement.executeQuery();//执行sql,将查询的数据存到ResultSet类型的变量中
			return resultSet.next();
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {
			close(resultSet,preparedStatement,connection);
		}
		return false;
	}
	
	/**
	 * 查询数据
	 *
	 * @author GaoHuanjie
	 */
	public void select(String sql,IRowMapper rowMapper) {//接口无法创建对象,所以rowMapper参数一定指向IRowMapper接口实现类对象
		Connection connection = null;
		Statement statement =null;
		ResultSet resultSet=null;
		try {
			connection = getConnection();//获取连接
			statement = connection.createStatement();
			resultSet= statement.executeQuery(sql);//执行sql,将查询的数据存到ResultSet类型的变量中
			rowMapper.rowMapper(resultSet);//因为rowMapper参数指向IRowMapper接口实现类对象,所以此处将调用接口实现类中所实现的rowMapper方法  多态
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {//释放资源
			close(resultSet,statement,connection);
		}
	}
	
	/**
	 * 查询数据
	 *
	 * @author GaoHuanjie
	 */
	public void select(String sql,IRowMapper rowMapper,Object ...params) {
		Connection connection = null;
		PreparedStatement preparedStatement =null;
		ResultSet resultSet=null;
		try {
			connection = getConnection();//获取连接
			preparedStatement= connection.prepareStatement(sql);//含有?号占位符的sql
			for (int i = 0; i < params.length; i++) {
				preparedStatement.setObject(i+1, params[i]);//为?号赋值
			}
			resultSet= preparedStatement.executeQuery();//执行sql
			rowMapper.rowMapper(resultSet);//因为rowMapper参数指向IRowMapper接口实现类对象,所以此处将调用接口实现类中所实现的rowMapper方法  多态
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {//释放资源
			close(resultSet,preparedStatement,connection);
		}
	}

	/**
	 * 修改(insert、update和delete)数据
	 *
	 * @author GaoHuanjie
	 */
	public boolean update(String sql) {
		Connection connection = null;
		Statement statement = null;
		try {
			connection = getConnection();//获取数据库连接对象,一个对象表示一次数据库连接
			statement = connection.createStatement();//获取Statement对象
			int result = statement.executeUpdate(sql);//执行sql语句,返回受影响的行数,仅限于数据insert、update和delete
			return result>0;//处理结果
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {//即便有异常也会执行代码
			close(statement,connection);
		}
		return false;
	}
	
	/**
	 * 修改(insert、update和delete)数据
	 *
	 * @author GaoHuanjie
	 */
	public boolean update(String sql,Object ...params) {
		Connection connection= null;
		PreparedStatement preparedStatement= null;
		try {
			connection= getConnection();
			preparedStatement= connection.prepareStatement(sql);//含有?号占位符的sql
			for (int i = 0; i < params.length; i++) {
				preparedStatement.setObject(i+1, params[i]);//为?号赋值
			}
			return preparedStatement.executeUpdate()>0;
		} catch (Exception e) {
			logger.debug(e.getMessage(), e);
		}finally {
			close(preparedStatement,connection);
		}
		return false;
	}
	
	/**
	 * 释放资源
	 *
	 * @author GaoHuanjie
	 */
	private void close(Statement statement,Connection connection) {
		try {
			if(statement!=null) {//有可能由于异常导致statement没有赋值,比如url出错
				statement.close();
			}
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		try {
			if(connection!=null) {
				connection.close();
			}
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
	}
	
	/**
	 * 释放资源
	 *
	 * @author GaoHuanjie
	 */
	private void close(ResultSet resultSet,Statement statement,Connection connection) {//重载
		try {
			if(resultSet!=null) {
				resultSet.close();
			}
		} catch (SQLException e) {
			logger.debug(e.getMessage(), e);
		}
		close(statement,connection);
	}
}

IRowMapper :

package com.zzu.tool.db;
import java.sql.ResultSet;

public interface IRowMapper {

	void rowMapper(ResultSet rs);
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值