Java implementation Connection MySQL database unit test query data item sharing

Source: Internet
Author: User
Tags getmessage

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.java

/** * 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;}}

Userinfodbmanger.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;}}

Userinfotest.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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.