java实现连接mysql数据库单元测试查询数据的实例代码
程序员文章站
2024-03-12 21:19:44
1、按照javaweb项目的要求逐步建立搭建起机构,具体的类包有:model 、db、dao、test;
具体的架构详见下图:
2、根据搭建的项目架构新建数据库...
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测试台和控制台出现如下结果就表示项目运行成功了。
以上就是小编为大家带来的java实现连接mysql数据库单元测试查询数据的实例代码全部内容了,希望大家多多支持~