java实现连接mysql数据库单元测试查询数据项目分享

1、按照javaweb项目的要求逐步建立搭建起机构,具体的类包有:model 、db、dao、test;具体的架构详见下图:

技术分享

 

2、根据搭建的项目架构新建数据库test和数据库表t_userinfo并且添加对应的测试数据; (这里我使用的是绿色版的数据库,具体的下载地址:http://pan.baidu.com/s/1mg88YAc) 具体的建立数据库操作详见下图:

技术分享技术分享

3、编写包中的各种类代码,具体参考代码如下:

UserInfo.java

/**
 * FileName:     UserInfo.java
 * @Description: TODO封装对象的信息
 * Copyright: personage  
 * Company  personage    
 * @author:    gaoxing
 * @version    V1.0 
 * Createdate:         2014-5-25 下午2:26:41
 *
 * Modification  History:
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2014-5-25       GX          1.0             1.0
 * Why & What is modified: <修改原因描述>
 */
package com.org.user.model;

/**
 * @ClassName:     UserInfo
 * @Description:TODO封装对象的信息
 * @author:   gaoxing
 * @date:        2014-5-25 下午2:26:41
 */
public class UserInfo {
	 private  int userid;
	   private String username;
	   private String password;
	   
/**
	 * @Title:        UserInfo
	 * @Description:    TODO(描述这个方法的作用)
	 * @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无参的构造方法
 * @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 连接数据库的操作
 * Copyright: personage  
 * Company  personage    
 * @author:    gaoxing
 * @version    V1.0 
 * Createdate:         2014-5-25 下午2:47:38
 *
 * Modification  History:
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2014-5-25       GX          1.0             1.0
 * Why & What is modified: <修改原因描述>
 */
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连接数据库的操作
 * @author: gaoxing
 * @date: 2014-5-25 下午2:47:38
 */
public 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());
			}
		} 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 处理通过数据库的连接进行操作对象信息
 * Copyright: personage  
 * Company  personage    
 * @author:    gaoxing
 * @version    V1.0 
 * Createdate:         2014-5-25 下午2:36:09
 *
 * Modification  History:
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2014-5-25       GX          1.0             1.0
 * Why & What is modified: <修改原因描述>
 */
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处理通过数据库的连接进行操作对象信息
 * @author:   gaoxing
 * @date:        2014-5-25 下午2:36:09
 */
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);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
	  	}
		return list;
	}
}

UserInfoTest.java

/**
 * FileName:     UserInfoTest.java
 * @Description: TODO测试dao包的方法
 * Copyright: personage  
 * Company  personage    
 * @author:    gaoxing
 * @version    V1.0 
 * Createdate:         2014-5-25 下午5:43:03
 *
 * Modification  History:
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2014-5-25       GX          1.0             1.0
 * Why & What is modified: <修改原因描述>
 */
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 测试dao包的方法
 * @author:   gaoxing
 * @date:        2014-5-25 下午5:43:03
 */
public class UserInfoTest {

	/**
	 * @Title: setUpBeforeClass
	 * @Description: TODO(描述这个方法的作用)
	 * @param: @throws java.lang.Exception   
	 * @return: void   
	 * @throws
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	/**
	 * @Title: tearDownAfterClass
	 * @Description: TODO(描述这个方法的作用)
	 * @param: @throws java.lang.Exception   
	 * @return: void   
	 * @throws
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	/**
	 * Test method for {@link com.org.user.dao.UserInfoDao#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("名称:  "+ui.getUsername()+"密码:  "+ui.getPassword());

		}
	}

}

4、在编写好类内容之后,在搭建项目过程中要加入辅助的JUnit的测试包junit.jar,同时也要导入数据库的连接mysq-connector-java-5.1.7-bin.jar  sqljdbc.jar,这样才可以连接到数据库;

5、所有的工作做好之后,就可以启动服务进行运行了查看结果了,如果在JUnit测试台和控制台出现如下结果就表示项目运行成功了。

技术分享

技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。