package cn.jbit.epetShop.dao; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; import cn.jbit.epetShop.dao.BaseDao; public class BaseDao { public static String DRIVER; public static String URL ; public static String DBNAME; public static String DBPASS; Connection conn = null; static{ init(); } public static voidthe init () { the Properties the params = new new the Properties (); String the configFile = " the database.properties " ; // profile path // load configuration file input stream into the InputStream IS = BaseDao. class .getClassLoader () the getResourceAsStream (the configFile). ; the try { // read from the input stream property list 7 in the params .load ( IS ); } the catch (IOException E) { e.printStackTrace (); } //Get the specified value corresponding to the DRIVER = the params .getProperty ( " Driver " ); the URL = the params .getProperty ( " URL " ); DBNAME = the params .getProperty ( " User " ); DBPASS in = the params .getProperty ( " password " ) ; } // database connection public connection getConn () throws a ClassNotFoundException, SQLException { connection Conn = null; try { Class.forName(DRIVER); conn = DriverManager.getConnection(URL, DBNAME, DBPASS); } catch (SQLException e) { e.printStackTrace(); } return conn; } public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public int executeSQL(String preparedSql, Object[] param) { Connection conn = null ; PreparedStatement pstmt = null ; int NUM = 0 ; / * process SQL, execute the SQL * / the try { Conn = getConn (); // get the database connection pstmt = conn.prepareStatement (preparedSql); // get PreparedStatement object IF (param ! = null ) { for ( int I = 0 ; I <param.length; I ++ ) { pstmt.setObject (I + . 1 , param [I]); // precompiled sql setting parameters } } // System.out.println (preparedSql); NUM = pstmt.executeUpdate (); // execute SQL statements } the catch (ClassNotFoundException E) { e.printStackTrace (); // process a ClassNotFoundException } the catch (SQLException E) { e.printStackTrace (); // process exception SQLException } the finally { the this .closeAll (Conn, pstmt, null ); } return NUM; }
1 driver=com.mysql.jdbc.Driver 2 url=jdbc:mysql://localhost:3306/petShop 3 user=root 4 password=
com.myschool.entity Package; Import the java.io.Serializable; Import java.util.Date; public class Student the implements the Serializable { Private static Final Long serialVersionUID = 6439763802252472361L ; // define entity attributes Private int studentNo; Private String longinPwd; Private String studentName; Private int Sex; Private Grade Grade; // the year as a property of the object Private String Phone; Private String address; Private Date bornDate; private String email; private String identityCard; //封装 public int getStudentNo() { return studentNo; } public void setStudentNo(int studentNo) { this.studentNo = studentNo; } public String getLonginPwd() { return longinPwd; } public void setLonginPwd(String longinPwd) { this.longinPwd = longinPwd; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public grade getGrade() { return grade; } public void setGrade(grade grades) { this.grade = grades; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Date getBornDate() { return bornDate; } public void setBornDate(Date bornDate) { this.bornDate = bornDate; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getIdentityCard() { return identityCard; } public void setIdentityCard(String identityCard) { this.identityCard = identityCard; } //有参构造 public Student(int studentNo, String longinPwd, String studentName, int sex, grade gradeID, String phone, String address, Date bornDate, String email, String identityCard) { this.studentNo = studentNo; this.longinPwd = longinPwd; this.studentName = studentName; this.sex = sex; this.gradeID = gradeID; this.phone = phone; this.address = address; this.bornDate = bornDate; this.email = email; this.identityCard = identityCard; } //Constructor with no arguments public Student () {
package com.myschool.entity; public class grade { private int gradeid; private String gradeName; public int getGradeid() { return gradeid; } public void setGradeid(int gradeid) { this.gradeid = gradeid; } public String getGradeName() { return gradeName; } public void setGradeName(String gradeName) { this.gradeName = gradeName; } public grade(int gradeid, String gradeName) { super(); this.gradeid = gradeid; this.gradeName = gradeName; } public grade() { super(); } }
package com.myschool.dao; import java.util.List; import com.myschool.entity.Student; public interface IStudentDao { /* * 查询所有学生记录,年级名称 */ public List<Student> Search() throws Exception; } package com.mychool.dao.impl; import java.sql.ResultSet; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import com.myschool.dao.IStudentDao; import com.myschool.dao.baseDao; import com.myschool.entity.Student; import com.myschool.entity.grade; public classThe extends BaseDao the implements IStudentDao {IStudentDaoImpl @Override public List <Student> Search () throws Exception { // create a collection of generic objects student List <Student> = stus new new the ArrayList <Student> (); String SQL = " the Select studentName, from GradeName Student, Grade WHERE Grade.gradeid = Student.gradeid " ; // definition of the object receiving basedao resultSet check out the data query method the resultSet = rSet the executeQuery (SQL); IF (! rSet = null ) { the while (rSet.next () ) { Student STU = new newStudent (); Grade Grade = new new Grade (); // create an object grades; grade.setGradeName (rSet.getString ( " gradeName " )); // to assign grades Object stu.setStudentName (rSet.getString ( " studentName " ) ); // give students an assignment objects stu.setGradeID (Grade); stus.add (STU); // add students to object to the object in the collection } } // recycling release resources closeResouse (); return stus; } }
1.service接口: package com.myschool.service; import java.util.List; import com.myschool.entity.Student; public interface IService { public List<Student> Search() throws Exception; } 2.service接口实现类: package com.mychool.service.Impl; import java.util.List; import com.mychool.dao.impl.IStudentDaoImpl; import com.myschool.dao.IStudentDao; import com.myschool.entity.Student; import com.myschool.service.IService; public class IServiceImpl implements IService{ IStudentDao isd=new IStudentDaoImpl(); @Override public List<Student> Search() throws Exception { return isd.Search(); } }