1, in accordance with the requirements of the Javaweb project gradually set up the organization, the specific class package has: Model, DB, DAO, test; Detailed architecture:
2, according to build the project structure new database test and database table T_userinfo and add the corresponding test data; (Here I am using the green version of the database, specific: HTTP://PAN.BAIDU.COM/S/1MG88YAC) to establish the specific database operations see:
3, write the various class code in the package, the specific reference code is as follows:
UserInfo.ja VA
/** * FileName:UserInfo.java * @Description: Todo encapsulates the object information * Copyright:personage * Company personage * @author: gaoxing * @version V1.0 * createdate:2014-5-25 pm 2:26:41 * * Modification History: * Date Author Version discription *-----------------------------------------------------------------------------------* 2014-5-25 GX 1.0 1.0 * Why & What is modified: < Modify reason description > */package com.org.user.model ;/** * @ClassName: UserInfo * @Description: Todo Package object information * @author: gaoxing * @date: 2014-5-25 pm 2:26:41 */publ IC class UserInfo {private int userid; Private String username; private String password; /** * @Title: UserInfo * @Description: TODO (Describe the function of this method) * @param: @param userid * @param: @param username * @param: @param password * @throws */public UserInfo (int userid, string Username, string password) {super (); This.userid = Userid;this.username = Username;this.password= password;} /** * @Title: UserInfo * @Description: Todo non-parametric 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 (String password) {this.password = password;}}
UserInfoDBM Anger.java
/** * FileName:UserInfoDBManger.java * @Description: TODO Connection Database Operation * Copyright:personage * Company personage * @author: Gaoxing * @version V1.0 * createdate:2014-5-25 pm 2:47:38 * * Modification History: * Date Author Version discription *--------------------------------------------------------------------------- --------* 2014-5-25 GX 1.0 1.0 * Why & What is modified: < Modify reason description > */package com.org. User.db;import Java.sql.drivermanager;import Java.sql.resultset;import Java.sql.sqlexception;import Com.mysql.jdbc.connection;import com.mysql.jdbc.preparedstatement;/** * @ClassName: Userinfodbmanger * @Description: TODO Connect database operation * @author: gaoxing * @date: 2014-5-25 pm 2:47:38 */public class Userinfodbmanger {private static Connection C Onn = null;private PreparedStatement PS = null;private ResultSet 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 ();p s.close (); Conn.close ();} catch (SQLException e) {e.getmessage ();}}}
UserInfoDao . Java
/** * FileName:UserInfoDao.java * @Description: TODO handles manipulating object information through a connection to a database * Copyright:personage * Company personage * @author: Gaoxing * @version V1.0 * createdate:2014-5-25 pm 2:36:09 * * Modification History: * Date Author Version discription *---------------------------------------------------------------------- -------------* 2014-5-25 GX 1.0 1.0 * Why & What is modified: < Modify reason description > */package 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 Com.org.user.db.userinfodbmanger;import com.org.user.model.userinfo;/** * @ClassName: Userinfodao * @Description: TODO handles manipulating object information through a database connection * @author: gaoxing * @date: 2014-5-25 pm 2:36:09 */public class Userinfodao {Connection con N=null; PreparedStatement Ps=null; ResultSet Rs=null;public List<userinfO> find () {list<userinfo> list=new arraylist<userinfo> (); String sql= "SELECT * from T_userinfo"; Conn=userinfodbmanger.getconn (); try {ps= (PreparedStatement) 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 auto-generated catch Blocke.printstacktrace (); }return list;}}
UserInfoTes T.java
/** * FileName:UserInfoTest.java * @Description: Todo Test DAO Package method * Copyright:personage * Company Personage * @a uthor:gaoxing * @version V1.0 * createdate:2014-5-25 pm 5:43:03 * * Modification History: * Date Author Version discription *------------------------------------------------------------------------------- ----* 2014-5-25 GX 1.0 1.0 * Why & What is modified: < Modify reason description > */package com.org.user . test;import Static Org.junit.assert.*;import Java.util.list;import Org.junit.afterclass;import Org.junit.beforeclass;import Org.junit.test;import Com.org.user.dao.userinfodao;import com.org.user.model.userinfo;/** * @ClassName: Userinfotest * @Description: TODO Test DAO Package method * @author: gaoxing * @date: 2014-5-25 pm 5:43:03 */public class Userinfotest {/** * @Title: Setupbeforeclass * @Description: TODO (Describe the function of this method) * @param: @throws java.lang.Exception * @return: void * @throws */@BeforeClasspubLic static void Setupbeforeclass () throws Exception {}/** * @Title: Teardownafterclass * @Description: TODO (describes the function of this method) * @ PARAM: @throws java.lang.Exception * @return: void * @throws */@AfterClasspublic static void Teardownafterclass () th Rows Exception {}/** * Test method for {@link com.org.user.dao.userinfodao#find ()}. */@Testpublic void Testfind () {Userinfodao udao=new Userinfodao (); List<userinfo> List=udao.find (); for (int i = 0; i < list.size (); i++) {UserInfo ui=list.get (i); System.out.println ("Name:" +ui.getusername () + "Password:" +ui.getpassword ());}}}
4, after writing a good class content, in the construction project to join the Auxiliary JUnit test package Junit.jar, but also to import the database connection Mysq-connector-java-5.1.7-bin.jar Sqljdbc.jar, So that you can connect to the database;
5, all the work done, you can start the service to run the view results, if the following results in the JUnit test Bench and console show that the project is running successfully.
Java implementation Connection MySQL database unit test query data item sharing