Java implementation Connection MySQL database unit Test query data instance code _java

Source: Internet
Author: User
Tags getmessage

1, in accordance with the requirements of the Javaweb project gradually set up an organization, the specific types of packages are: model, DB, DAO, test;

Detailed structure of the following figure:

2, according to the structure of the project to create a new database test and database table T_userinfo and add corresponding test data; (Here I use the green version of the database, the specific download address: http://pan.baidu.com/s/1mg88YAc)

Details of the establishment of the database operation detailed in the following figure:

3, write the package of various types of code, the specific reference code as follows:

Userinfo.java

/** * FileName:UserInfo.java * @Description: Todo encapsulates the information of the object * Copyright:personage * Company personage * @author: gaoxing * @version V1.0 * createdate:2014-5-25 afternoon 2:26:41 * Modification History: * Date Author Versi On Discription *-----------------------------------------------------------------------------------* 2014-5-25 G
 
X 1.0 1.0 * Why & What is modified: < Modify cause description > */package Com.org.user.model;  /** * @ClassName: UserInfo * @Description: Todo Encapsulated object information * @author: gaoxing * @date: 2014-5-25 pm 2:26:41 * Public
    Class UserInfo {private int userid;
    Private String username;
    
private String password; 
   /** * @Title: UserInfo * @Description: TODO (Describe the effect of this method) * @param: @param userid * @param: @param username * @param: @param password * @throws/public UserInfo (int userid, string Username, string password) {su
    Per ();
    This.userid = userid;
 This.username = Username;   This.password = password;
/** * @Title: UserInfo * @Description: Todo parameterless construction method * @param: * @throws/Public UserInfo () {super ();
  public int GetUserID () {return userid;
  The 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 Connect to database operations * Copyright:personage * Company personage * @author: Gaoxing * @version V1.0 * createdate:2014-5-25 afternoon 2:47:38 * Modification History: * Date Autho R Version discription *-----------------------------------------------------------------------------------* 2014
 
-5-25 GX 1.0 1.0 * Why & What is modified: < Modify cause 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 afternoon 2:47:38 * * Pub
  Lic class Userinfodbmanger {private static Connection conn = 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 ());
    The 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: TODO handles object information through a database connection * Copyright:personage * Company Personag     E * @author: gaoxing * @version V1.0 * createdate:2014-5-25 afternoon 2:36:09 * Modification History: * Date  Author Version discription *-----------------------------------------------------------------------------------*
 
2014-5-25 GX 1.0 1.0 * Why & What is modified: < Modify cause 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 Process object information through a database connection * @author: gaoxing * @date: 2014-5-25 2:36
  : */public class Userinfodao {Connection conn=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);
    The catch (SQLException e) {//TODO auto-generated catch block E.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 afternoon 5:43:03 * Modification History: * Date Author Version discription *-----------------------------------------------------------------------------------* 2014-5-
 
GX 1.0 1.0 * Why & What is modified: < Modify cause 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 5:43:03/  public class Userinfotest {/** * @Title: Setupbeforeclass * @Description: TODO (Describe the effect of this method) * @param: @throws Java.lang.Exception * @return: void * @throws/@BEforeclass public static void Setupbeforeclass () throws Exception {}/** * @Title: Teardownafterclass * @De
  Scription:todo (describes the effect of this method) * @param: @throws java.lang.Exception * @return: void * @throws/@AfterClass public static void Teardownafterclass () throws Exception {}/** * Test method for {@link Com.org.user.dao.User
   Infodao#find ()}.
     * * @Test public 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 process 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. After all the work is done, you can start the service to run the results of the review, if the following results on the JUnit test bench and console indicate that the project is running successfully.

The above is a small series for everyone to bring the Java Implementation Connection MySQL database Unit test Query Data sample code all content, I hope that we support cloud Habitat Community ~

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.