1. Javaweb 프로젝트의 요구 사항에 따라 점차적으로 기관을 설립하고 건설합니다. 특정 클래스 패키지에는 다음이 포함됩니다. 모델, DB, DAO, 테스트;
특정 아키텍처에 대한 자세한 내용은 아래 그림을 참조하십시오.
2. 프로젝트 구조를 기반으로 새 데이터베이스 테스트 및 데이터베이스 테이블 T_USERINFO를 작성하고 해당 테스트 데이터를 추가합니다. (여기에는 데이터베이스의 녹색 버전을 사용하고 있습니다. 특정 다운로드 주소 : http://pan.baidu.com/s/1mg88yac)
특정 데이터베이스 설정 작업에 대한 자세한 내용은 아래 그림을 참조하십시오.
3. 패키지에 다양한 유형의 코드를 작성하면 특정 참조 코드는 다음과 같습니다.
userinfo.java
/** * filename : userInfo.java * @Description : TODO 캡슐화 된 객체 정보 * 저작권 : 사람 * 회사 사람 * @Author : Gaoxing * @version v1.0 * 생성물 : 2014-5-25 2:26:41 PM * * 수정 기록 : * 날짜 저자 버전 배제 * --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- is modified: <Description of the modification reason> */package com.org.user.model; /** * @className : userInfo * @Description : TODO는 객체의 정보를 캡슐화합니다 * @Auxing * @Date : 2014-5-25 2:26:41 PM */public userInfo {private int userId; 개인 문자열 사용자 이름; 개인 문자열 비밀번호; / ** * @title : userInfo * @description : todo (이 메소드의 기능 설명) * @param : @param userId * @param : @param username * @param * @throws */ public userInfo (int userId, String username, string password) {super (); this.userid = userId; this.username = username; this.password = 비밀번호; } /** * @title : userInfo * @description : todo parameterless construction method * @param : * @throws * /public userInfo () {super ();} public int getUserId () {return userId; } public void setUserId (int userId) {this.userid = userId; } public String getUserName () {return username; } public void setusername (String username) {this.username = username; } public String getPassword () {return password; } public void setpassword (문자열 비밀번호) {this.password = password; }}userinfodbmanger.java
/** * filename : userinfodbmanger.java * @description : 데이터베이스에 연결하기위한 todo 작업 * 저작권 : 사람 * 회사 사람 * @author : gaoxing * @version v1.0 * 생성 된 : 2014-5-25 2:47:38 pm * * Modification History : * 날짜 저자 배열 * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ & What is modified: <Description of the modification 이유> */패키지 com.org.user.db; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; com.mysql.jdbc.connection; import com.mysql.jdbc.preparedstatement; /** * @className : userInfodbmanger * @description : 데이터베이스에 연결하려는 TODO의 작업 * @Author : gaoxing * @date : 2014-5-25 2:47:38 pm */public class userinfodbmanger {private static connection conn = null; 개인 준비 상태 ps = null; 개인 결과 세트 RS = NULL; public static connection getConn () {String url = "jdbc : mysql : // localhost : 3306/test"; try {class.forname ( "com.mysql.jdbc.driver"); try {conn = (Connection) DriverManager.getConnection (url, "root", "mysql"); } catch (sqlexception e) {system.out.println (e.getMessage ()); }} catch (classNotFoundException e) {System.out.println (e.getMessage ()); } return conn; } public void close () {try {rs.close (); ps.close (); conn.close (); } catch (sqlexception e) {e.getMessage (); }}}userinfodao.java
/** * filename : userinfodao.java * @description : 데이터베이스 연결을 통해 운영을위한 객체 정보를 처리합니다. * Copyright : Person * Company Person * @Auxing * @version v1.0 * 생성 된 : 2014-5-25 2:36:09 PM * * 수정 기록 : * 날짜 저자 버전 불쾌감 * -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Why & What is modified: <Description of the modification 이유> */패키지 com.org.user.dao; import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; import java.util.list; import com.mysql.jdbc.connection; import com.mysql.jdbc.preparedstatement; import.db.userinfbmanger; com.org.user.model.userinfo; /** * @className : userInfodao * @description : todo는 데이터베이스 연결 * @author : gaoxing * @date : 2014-5-25 2:36:09 pm */public class userinfodao {connection conn = null; 준비된 상태 ps = null; resultSet rs = null; 공개 목록 <userInfo> find () {list <UserInfo> list = new ArrayList <UserInfo> (); 문자열 sql = "t_userinfo에서 선택 *"; conn = userinfodbmanger.getConn (); try {ps = (proadstatement) conn.preparestatement (SQL); rs = ps.ExecuteQuery (); while (rs.next ()) {userInfo ui = new userInfo (); ui.setuserid (rs.getint (1)); ui.setusername (rs.getstring (2)); ui.setpassword (rs.getstring (3)); list.add (ui); }} catch (sqlexception e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } 반환 목록; }}userInfotest.java
/** * filename : userInfotest.java * @Description : DAO 패키지를 테스트하는 TODO 메소드 * 저작권 : 사람 * 회사 사람 * @Author : gaoxing * @version v1.0 * 생성 : 2014-5-25 5:43:03 PM * * 수정 기록 : * 날짜 버전 설명 * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- What is modified: <Description of the modification reason> */package com.org.user.test; static org.junit.assert를 가져옵니다.*; Java.util.list 가져 오기; import org.junit.fterclass; import org.junit.beforeclass; import org.junit.test; import com.org.user.dao.userinfodao; import com.org.user.model.userinfo; /** * @className : userInfotest * @Description : DAO 패키지 테스트를위한 TODO 메소드 * @Author : Gaoxing * @Date : 2014-5-25 5:43:03 PM * /Public Class userInfotest { /** * @Title : SetUpbeForeClass * @Description : TO의 기능 설명) * @param : @param : @param : @param : @Param : java.lang.exception * @return : void * @throws */ @beforeclass public static void setupbeforeclass ()는 예외를 {}/ ** * @title : earcowdownafterclass * @description : to (이 방법의 함수 설명) * @param : @throws java.lang.exception * @voidehtors * @Thorthors * @Thorthors * @Thorthors * @Thorthors * @Thorthors * @Thorthors * @Thorce * : public static void teadownafterclass ()는 {@link com.org.user.dao.userinfodao#find ()}에 대한 예외 {} /*** 테스트 메소드를 던집니다. */ @test public void testfind () {userInfodao udao = new userInfodao (); 목록 <UserInfo> list = udao.find (); for (int i = 0; i <list.size (); i ++) {userInfo ui = list.get (i); System.out.println ( "이름 :"+ui.getusername ()+"암호 :"+ui.getPassword ()); }}}4. 클래스 컨텐츠를 작성한 후, 프로젝트 구성 프로세스 중에 보조 주니트 테스트 패키지 junit.jar를 추가해야하며 데이터베이스 연결 MySQ-Connector-Java-5.1.7-bin.jar sqljdbc.jar를 가져 와서 데이터베이스에 연결할 수 있도록해야합니다.
5. 모든 작업이 완료된 후에는 서비스를 시작하여 결과를 볼 수 있습니다. Junit 테스트 벤치 및 콘솔에 다음 결과가 나타나면 프로젝트가 성공적으로 실행되었음을 의미합니다.
위의 것은 MySQL 데이터베이스의 단위 테스트에 쿼리 데이터를 연결하기 위해 편집기가 제공하는 예제 코드의 전체 내용입니다. 모두가 wulin.com을 더 지원하기를 바랍니다 ~