目录
一、底层代码
1.DBHelper
代码
#mysql driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&useSSL=false user=root pwd=123456
方法
package com.heminjie.util; import java.io.InputStream; 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 java.util.Properties; /** * 提供了一组获得或关闭数据库对象的方法 * */ public class DBHelper { private static String driver; private static String url; private static String user; private static String password; static {// 静态块执行一次,加载 驱动一次 try { InputStream is = DBHelper.class .getResourceAsStream("config.properties"); Properties properties = new Properties(); properties.load(is); driver = properties.getProperty("driver"); url = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("pwd"); Class.forName(driver); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * 获得数据连接对象 * * @return */ public static Connection getConnection() { try { Connection conn = DriverManager.getConnection(url, user, password); return conn; } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void close(ResultSet rs) { if (null != rs) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } } public static void close(Statement stmt) { if (null != stmt) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } } public static void close(Connection conn) { if (null != conn) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } } public static void close(Connection conn, Statement stmt, ResultSet rs) { close(rs); close(stmt); close(conn); } public static void close(Connection conn, Statement stmt, ResultSet rs,PreparedStatement ps) { close(rs); close(stmt); close(conn); close(ps); } public static boolean isOracle() { return "oracle.jdbc.driver.OracleDriver".equals(driver); } public static boolean isSQLServer() { return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver); } public static boolean isMysql() { return "com.mysql.jdbc.Driver".equals(driver); } public static void main(String[] args) { Connection conn = DBHelper.getConnection(); DBHelper.close(conn); System.out.println("isOracle:" + isOracle()); System.out.println("isSQLServer:" + isSQLServer()); System.out.println("isMysql:" + isMysql()); System.out.println("数据库连接(关闭)成功"); } }
2.实体类
班级类
package com.heminjie.entity; /** * 班级类 * @author Administrator * */ public class Class { private int cid; private String cname; public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Class() { // TODO Auto-generated constructor stub } public Class(int cid, String cname) { this.cid = cid; this.cname = cname; } @Override public String toString() { return "Class [cid=" + cid + ", cname=" + cname + "]"; } }
爱好类
package com.heminjie.entity; /** * 爱好类 * @author Administrator * */ public class Hobby { private int hid; private String hname; public int getHid() { return hid; } public void setHid(int hid) { this.hid = hid; } public String getHname() { return hname; } public void setHname(String hname) { this.hname = hname; } public Hobby() { // TODO Auto-generated constructor stub } public Hobby(int hid, String hname) { this.hid = hid; this.hname = hname; } @Override public String toString() { return "Hobby [hid=" + hid + ", hname=" + hname + "]"; } }
学生类
package com.heminjie.entity; import java.io.Serializable; import java.util.List; /** * 学生类 * @author Administrator * */ public class Student implements Serializable{ private static final long serialVersionUID = 1L; private int sid; private String sname; private Class cl; private Theaher th; private List<Hobby> hy; // private String hys; // public String getHys() { // return hys; // } // public void setHys(String hys) { // this.hys = hys; // } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Class getCl() { return cl; } public void setCl(Class cl) { this.cl = cl; } public Theaher getTh() { return th; } public void setTh(Theaher th) { this.th = th; } public List<Hobby> getHy() { return hy; } public void setHy(List<Hobby> hy) { this.hy = hy; } public Student() { // TODO Auto-generated constructor stub } public Student(int sid, String sname, Class cl, Theaher th, List<Hobby> hy) { this.sid = sid; this.sname = sname; this.cl = cl; this.th = th; this.hy = hy; } public Student(String sname, Class cl, Theaher th, List<Hobby> hy) { this.sname = sname; this.cl = cl; this.th = th; this.hy = hy; } // public Student(String sname, Class cl, Theaher th, List<Hobby> hy,String hys) { // this.sname = sname; // this.cl = cl; // this.th = th; // this.hy = hy; // this.hys = hys; // } @Override public String toString() { return "Student [sid=" + sid + ", sname=" + sname + ", cl=" + cl + ", th=" + th + ", hy=" + hy + "]"; } }
教员类
package com.heminjie.entity; /** * 教员类 * @author Administrator * */ public class Theaher { private int tid; private String tname; private Class cid; public int getTid() { return tid; } public void setTid(int tid) { this.tid = tid; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public Class getCid() { return cid; } public void setCid(Class cid) { this.cid = cid; } public Theaher() { // TODO Auto-generated constructor stub } public Theaher(int tid, String tname, Class cid) { this.tid = tid; this.tname = tname; this.cid = cid; } @Override public String toString() { return "Theaher [tid=" + tid + ", tname=" + tname + ", cid=" + cid + "]"; } }
3.数据库访问层 dao
班级类(接口)
package com.heminjie.dao; import java.util.List; import com.heminjie.entity.Class; public interface IClDao { /** * 爱好集合 * @return */ public List<Class> getAll(); /** * 查询单个 * @param cid 班级编号 * @return 返回对象 */ public Class getdg(int cid); }
学生类(接口)
package com.heminjie.dao; import java.util.List; import com.heminjie.entity.Class; import com.heminjie.entity.Hobby; import com.heminjie.entity.Student; import com.heminjie.entity.Theaher; public interface IStuDao { /** * 学生集合 * @return */ public List<Student> getAll(String cid,String tid,String hid,int pageIndex,int pageSize); /** * 查询单个学生 * @param sid 学生编号 * @return 返回单个学生 */ public Student getdg(int sid); /** * 增加 * @param stu * @return */ public int addStu(String sname, Class cl, Theaher th, List<Hobby> hy); /** * 删除 * @param sid 删除id * @return 返回行数 */ public int delStu(int sid); /** * 修改 * @param sid 学生id * @param sname 学生姓名 * @param cl 班级 * @param th 教员 * @param hy 爱好 * @return 返回执行行数 */ public int updStu(int sid,String sname, Class cl, Theaher th, List<Hobby> hy); /** * 总数 * @return */ public int count(String cid,String tid,String hid); }
爱好类(接口)
package com.heminjie.dao; import java.util.List; import com.heminjie.entity.Hobby; public interface IHyDao { /** * 爱好集合 * @return */ public List<Hobby> getAll(); /** * 爱好查询单个 * @param hid 爱好编号 * @return 返回爱好对象 */ public Hobby getdg(int hid); }
教员类(接口)
package com.heminjie.dao; import java.util.List; import com.heminjie.entity.Theaher; public interface IThDao { /** * 教员集合 * @return */ public List<Theaher> getAll(); /** * 查看单个 * @param tid * @return */ public Theaher getdg(int tid); }
班级类(方法)
package com.heminjie.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.heminjie.entity.Class; import com.heminjie.entity.Hobby; import com.heminjie.util.DBHelper; public class ClDao implements IClDao{ //扩大作用域 Connection conn = null; Statement stmt = null; ResultSet rs = null; @Override public List<Class> getAll() { List<Class> ls = new ArrayList<Class>(); try { //连接数据库 conn = DBHelper.getConnection(); //定义sql语句 String sql = "select * from h_bj"; //执行sql语句 stmt = conn.prepareStatement(sql); //获得结果集 rs = stmt.executeQuery(sql); //遍历结果集 while(rs.next()) { //实例化 Class cl = new Class(rs.getInt(1),rs.getString(2)); //加到集合中 ls.add(cl); } } catch (Exception e) { e.printStackTrace(); } finally { DBHelper.close(conn, stmt, rs); } return ls; } @Override public Class getdg(int cid) { Class cl = new Class(); try { //连接数据库 conn = DBHelper.getConnection(); //定义sql语句 String sql = "select * from h_bj where cid="+cid+""; //执行sql语句 stmt = conn.prepareStatement(sql); //获得结果集 rs = stmt.executeQuery(sql); //遍历结果集 while(rs.next()) { //实例化 cl = new Class(rs.getInt(1),rs.getString(2)); } } catch (Exception e) { e.printStackTrace(); } finally { DBHelper.close(conn, stmt, rs); } return cl; } public static void main(String[] args) { System.out.println(new ClDao().getdg(1)); } }
学生类(方法)
package com.heminjie.dao; import java.sql.Connection; import java.