Junit怎么测试数据库查询方法

Junit怎么测试数据库查询方法,第1张

@Test

public void jdbcTest() throws ClassNotFoundException, SQLException{

//加载MySql的驱动类

ClassforName("commysqljdbcDriver") ; //环境变量中需要有mysql的驱动包

//简历链接

String url = "jdbc:mysql://localhost:3306/test" ;

String username = "root" ;

String password = "root" ;

Connection con = DriverManagergetConnection(url , username , password ) ;

//创建stat 并执行查询,获得结果集resultSet

Statement stat = concreateStatement();

ResultSet resultSet = statexecuteQuery("select from 你的表 ");

//遍历查询结果集

//1 获得元数据(就是字段名称)

ResultSetMetaData metad = resultSetgetMetaData();

int column = metadgetColumnCount();

Systemoutprintln("查询结果为"+column+"列");

// Systemoutprintln("查询结果为"+metad+"条");

StringBuffer sb = new StringBuffer();

while(resultSetnext()){

for (int i = 0; i < column; i++) {

sbappend("字段:"+metadgetColumnName(i)+",字段值"+resultSetgetString(i)+"\t");

}

sbappend("\n");

}

Systemoutprintln(sbtoString());

}

package SystemDataConnect;

import javasql;

public interface OADateBaseConnect {

public Connection gteCon();

public void Close();

}

package SystemDataConnect;

import javasql;

public class JdbcConnect implements OADateBaseConnect{

private Connection con;

public Connection gteCon(){

try {

ClassforName("sunjdbcodbcJdbcOdbcDriver");

con=DriverManagergetConnection("jdbc:odbc:OA");

} catch (Exception ex) {

Systemoutprintln("ODBC连接数据库失败");

}

return con;

}

public void Close(){

try {

conclose();

} catch (SQLException ex){

Systemoutprintln("ODBC连接数据库后关闭失败");

}

}

}

package SystemDataBaseManipulation;

import javasql;

import SystemDataConnect;

import javautil;

public class OASelect {

public boolean Select(String select)

{

boolean estate=false;

OADateBaseConnect Connect=new JdbcConnect();

Connection Con=ConnectgteCon();

try {

Statement ment = ConcreateStatement();

ResultSet re=mentexecuteQuery(select);

if(renext())

estate=true;

if(re!=null)

reclose();

if(ment!=null)

mentclose();

if(Con!=null)

Conclose();

} catch (SQLException ex){

exprintStackTrace();

Systemoutprintln("OASelect类中的Select方法出现异常");

}

return estate;

}

public String SelectRowList(String select,int row,int list)

{

String value="";

OADateBaseConnect Connect=new JdbcConnect();

Connection Con=ConnectgteCon();

try {

Statement ment = ConcreateStatement();

ResultSet re=mentexecuteQuery(select);

int i=0;

while(renext()){

i++;

if(i==row)

value=regetString(list);

}

if(re!=null)

reclose();

if(ment!=null)

mentclose();

if(Con!=null)

Conclose();

} catch (SQLException ex){

exprintStackTrace();

Systemoutprintln("OASelect类中的SelectRowList方法出现异常");

}

return value;

}

public Vector SelectRow(String select,int row)

{

Vector rows=new Vector();

OADateBaseConnect Connect=new JdbcConnect();

Connection Con=ConnectgteCon();

try {

Statement ment = ConcreateStatement();

ResultSet re=mentexecuteQuery(select);

int j=0;

ResultSetMetaData Meta=regetMetaData();

while(renext()){

j++;

if(j==row){

for (int i = 1; i <= MetagetColumnCount(); i++) {

rowsaddElement(regetString(i));

}

}

}

if(re!=null)

reclose();

if(ment!=null)

mentclose();

if(Con!=null)

Conclose();

} catch (SQLException ex){

exprintStackTrace();

Systemoutprintln("OASelect类中的SelectRow方法出现异常");

}

return rows;

}

public Vector SelectList(String select,int list)

{

Vector lists=new Vector();

OADateBaseConnect Connect=new JdbcConnect();

Connection Con=ConnectgteCon();

try {

Statement ment = ConcreateStatement();

ResultSet re=mentexecuteQuery(select);

while(renext()){

listsaddElement(regetString(list));

}

if(re!=null)

reclose();

if(ment!=null)

mentclose();

if(Con!=null)

Conclose();

} catch (SQLException ex){

exprintStackTrace();

Systemoutprintln("OASelect类中的SelectList方法出现异常");

}

return lists;

}

public Vector SelectAll(String select)

{

Vector lists=new Vector();

OADateBaseConnect Connect=new JdbcConnect();

Connection Con=ConnectgteCon();

try {

Statement ment = ConcreateStatement();

ResultSet re=mentexecuteQuery(select);

ResultSetMetaData Meta=regetMetaData();

while(renext()){

Vector rows=new Vector();

for (int i = 1; i <=MetagetColumnCount() ; i++) {

rowsaddElement(regetString(i));

}

listsaddElement(rows);

}

if(re!=null)

reclose();

if(ment!=null)

mentclose();

if(Con!=null)

Conclose();

} catch (SQLException ex){

exprintStackTrace();

Systemoutprintln("OASelect类中的SelectAll方法出现异常"+select);

}

return lists;

}

}

package SystemDataBaseManipulation;

import javasql;

import SystemDataConnect;

public class OAInsert {

public boolean Insert(String insert){

boolean estate=false;

OADateBaseConnect Connect=new JdbcConnect();

Connection Con=ConnectgteCon();

try {

Statement ment = ConcreateStatement();

mentexecuteUpdate(insert);

estate=true;

if(ment!=null)

mentclose();

if(Con!=null)

Conclose();

} catch (SQLException ex){

exprintStackTrace();

Systemoutprintln("OAInsert类中的Insert方法出现异常");

}

return estate;

}

}

package SystemDataBaseManipulation;

import SystemDataConnect;

import javasql;

public class OAUpdate{

public boolean Update(String update)

{

boolean estate=false;

OADateBaseConnect Connect=new JdbcConnect();

Connection Con=ConnectgteCon();

try {

Statement ment = ConcreateStatement();

mentexecuteUpdate(update);

estate=true;

if(ment!=null)

mentclose();

if(Con!=null)

Conclose();

} catch (SQLException ex){

exprintStackTrace();

Systemoutprintln("OAUpdate类中的Update方法出现异常"+update);

}

return estate;

}

}

package SystemDataBaseManipulation;

import javasql;

import SystemDataConnect;

public class OADelete {

public boolean Delete(String delete)

{

boolean estate = false;

OADateBaseConnect Connect = new JdbcConnect();

Connection Con = ConnectgteCon();

try {

Statement ment = ConcreateStatement();

mentexecute(delete);

estate = true;

if(ment!=null)

mentclose();

if(Con!=null)

Conclose();

} catch (SQLException ex) {

exprintStackTrace();

Systemoutprintln("OADelete类中的Delete方法出现异常");

}

return estate;

}

}

方法一:

在java语句里得。

ResultSetMetaData

rsMeta

=

rsgetMetaData();

for(int

i=1;

i<=rsgetMetaData()getColumnCount();

i++)

{

outprintln(rsMetagetColumnName(i));

}

rsclose();

方法二:

用sql语句得。

SELECT

SysObjectsName,

SysColumnsName,

SysTypesName,

SysColumnsLength,

SysPropertiesValue

FROM

SysObjects,

SysTypes,

SysColumns

LEFT

JOIN

SysProperties

ON

(SyscolumnsId

=

SyspropertiesId

AND

SyscolumnsColid

=

SyspropertiesSmallid)

WHERE

(SysobjectsXtype

='u'

OR

SysobjectsXtype

='v')

AND

SysobjectsId

=

SyscolumnsId

AND

SysTypesXType

=

SyscolumnsXType

AND

SysTypesName

<>

'sysname'

AND

SysobjectsName

=

'你要的表名'

你在查询分析器里面看看是怎么样一个结果。

您好,

ResultSet得相关参数代码片断,下面的代码对类型判断和转换还不全面,如果还有其他字段请自行添加

Java代码

try {

ResultSetMetaData meta = rsgetMetaData();

int columnCount = metagetColumnCount();

strutName = new String[columnCount];

strutType = new byte[columnCount];

rslast();

int itemCount = rsgetRow();

rsfirst();

data = new Object[itemCount][columnCount];

for (int i = 1; i <= columnCount; i++){

int type = (byte) metagetColumnType(i);

//2是BigDecimal

if(type == 2){

strutType[i-1] = DBFFieldFIELD_TYPE_N;

}

//12是String

else if(type == 12){

strutType[i-1] = DBFFieldFIELD_TYPE_C;

}

//91是javautilDate

else if(type == 91){

strutType[i-1] = DBFFieldFIELD_TYPE_D;

}

strutName[i-1] = metagetColumnName(i);

}

for (int i = 0; rsnext(); i++){

for (int j = 0; j < columnCount; j++){

Object da = rsgetObject(j+1);

if(da instanceof BigDecimal){

Double d = DoubleparseDouble(datoString());

data[i][j] = d;

}else{

data[i][j] = da;

}

}

}

}catch (Exception e){

eprintStackTrace();

}finally{

rs = null;

}

上面采用的是传统方法进行导出dbf文件,如果大数据量导出时,就要先将文件生成到服务器端,然后从response里拿输入流,将刚生成的文件写到客户端,也就是多一个文件流写入的过程。

以上就是关于Junit怎么测试数据库查询方法全部的内容,包括:Junit怎么测试数据库查询方法、java连接数据库时出现的问题.、怎样用JAVA语句获得SQL表的列标题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/10157096.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存