1. 程式人生 > >適合中職學生學生管理系統的研發(java+jdbc+mysql)(四)

適合中職學生學生管理系統的研發(java+jdbc+mysql)(四)

這一長比較重要設計的到資料庫,我們上三的時候說到介面新增按鈕之後進入介面新增的操作,我這裡對JDBC進行了封裝,當然連線JDBC有很多的技術,我們這裡採用的properties方式,這種方式方便開發者管理,並且對初學者來說容易操作.

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/soft1701
user=root
pwd=root

封裝的BaseDao程式碼如下,裡面封裝了開啟資料,以及增刪改查的方法,方便學生操作


public class BaseDao {
	private Connection con;
	private PreparedStatement pstm;
	private ResultSet rst;
	
	private static String driver;
	private static String url;
	private static String user;
	private static String pwd;
	
	static{
		Properties prop = new Properties();
		try {
			// 載入屬性檔案
			prop.load(BaseDao.class.getClassLoader().getResourceAsStream("dao/dbinfo.properties"));
			// 讀取屬性檔案中的屬性
			driver = prop.getProperty("driver");
			url = prop.getProperty("url");
			user = prop.getProperty("user");
			pwd = prop.getProperty("pwd");
			// 載入驅動
			Class.forName(driver);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	// 獲取連線
	public Connection getConnection(){
		try {
			con = DriverManager.getConnection(url, user, pwd);
			return con;
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public  List<Student> QueryAllStudents() {
		Statement stt = null;
		ResultSet rs = null;
		List<Student> students = new ArrayList<Student>();
		try {
			stt = (Statement) con.createStatement();
			rs=stt.executeQuery("select * from students");
			while(rs.next()){
				int id = rs.getInt("_id");
				String name = rs.getString("name");
				String sex = rs.getString("sex");
				int age = rs.getInt("age");
				String phone = rs.getString("phone");
				String address = rs.getString("address");
				Student student = new Student(name,sex,age,phone,address);
				students.add(student);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("查詢失敗");
		}finally{
			try {
				stt.close();
				rs.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return students;
	}

	/**
	 * 新增資料
	 *
	 * */
	public  boolean AddStudentInfor( Student student) {
		boolean result;
		String mysql = "insert into students(name,sex,age,phone,address) values(?,?,?,?,?)";
		PreparedStatement ptt = null;
		try {
			ptt = (PreparedStatement) con.prepareStatement(mysql);
			ptt.setString(1, student.getName());
			ptt.setString(2, student.getSex());
			ptt.setInt(3, student.getAge());
			ptt.setString(4, student.getPhone());
			ptt.setString(5, student.getAddress());
			ptt.execute();
			result = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = false;
		}finally {
			try {
				ptt.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}



	public  boolean DeleteStudentInfor( int _id) {
		boolean result;
		String sql="delete from students where _id=?";
		PreparedStatement pst = null;
		try {
			pst = (PreparedStatement) con.prepareStatement(sql);
			pst.setInt(1, _id);
			pst.execute();
			result = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = false;
		} finally {
			try {
				pst.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
	public static boolean UpdateStudentInfor(Connection connection, Student student) {
		boolean result;
		String sql="update students set name=?,sex=?,age=?,phone=?,address=? where _id = ?";
		PreparedStatement pst = null;
		try {
			pst=(PreparedStatement) connection.prepareStatement(sql);
			pst.setString(1, student.getName());
			pst.setString(2, student.getSex());
			pst.setInt(3, student.getAge());
			pst.setString(4, student.getPhone());
			pst.setString(5, student.getAddress());
			pst.setInt(6, student.getId());
			pst.execute();
			result = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = false;
		} finally {
			try {
				pst.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}

	public void closeAll(){
		if(rst!=null){
			try {
				rst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}