我是Java新手,在从数据库访问数据时遇到困难。我正在使用SQL服务器,WebDriver和Java。我创建了一个类来连接数据库并将数据返回给调用者。我能够在二维数组ex- Object [] []数据集中从数据库中检索数据。
调用者类将获得类似于 - Object [] []数据集的数据。有时我的数据值为0.00(这是运费)。
我在调用者程序中检查此值时遇到困难,因为我想检查运费是否为0。
我尝试了以下方式,但我不确定这是否是正确的方法。有人可以告诉我正确的方法吗?
private DecimalFormat freight= new DecimalFormat("#.##");
dbObj = new DBOperations();
dbObj.establishConnection("DSN_dbtest");
dbObj.executeQuery(queryFS);
resultSet= dbObj.getDataResult();
freightCharge = resultSet[0][3].toString();
System.out.println("Freight charge=" + freightCharge); =>Displays .0000
if( freight.format(Double.parseDouble(freightCharge.toString())).equalsIgnoreCase("0"))
System.out.println("Freight charge is =" +freight.format(Double.parseDouble(freightCharge.toString())) );
类 - DBOperations -
package lampsplus.utilities;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.Types;
public class DBOperations {
private String connectionString=null, formattedString;
private Connection conn;
private Statement stmt;
private ResultSet resSet;
private ResultSetMetaData rsMD;
private Object[][] dataSet;
int rows,cols;
/**
* This method returns a result set retrieved from the database.
*
* @return Object - two dimensional array of fetched data.
*/
public Object[][] getDataResult()
{
return dataSet;
}
/**
* Class constructor to initialize variables.
*
*/
public DBOperations()
{
System.out.println("initializing constructor dboperations:");
connectionString="jdbc:odbc:";
this.conn = null;
this.stmt = null;
this.resSet = null;
this.dataSet=null;
}
/**
* This method is used to create a database connection.
*
* @param string - dsn name to connect the db
* @return void
* @throws Exception - An unexpected exception
*/
public void establishConnection(String dsnName) throws Exception
{
System.out.println("Establishing connection:");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connectionString= connectionString+dsnName;
System.out.println("connection string :" + connectionString);
conn = DriverManager.getConnection(connectionString);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
}
/**
* This method is used to execute a database query.
*
* @param string - Query to execute
* @throws Exception - An unexpected exception
*/
public void executeQuery(String query) throws Exception
{
resSet = stmt.executeQuery(query);
rsMD= resSet.getMetaData();
rows=cols=0;
while(resSet.next())
rows++;
cols=rsMD.getColumnCount();
dataSet= new Object[rows][cols];
rows=0;
resSet.beforeFirst();
System.out.println("Data type ====" + resSet.getClass().getName());
while(resSet.next())
{
for(cols=0;cols<rsMD.getColumnCount(); cols++)
{
if (rsMD.getColumnType(cols+1) == Types.NVARCHAR )
dataSet[rows][cols]= resSet.getString(cols+1).toString();
else
dataSet[rows][cols] =(Object)resSet.getString(cols+1);
System.out.print(" " + dataSet[rows][cols]);
}
rows++;
}
}
/**
* This method is used to close a database connection.
*
* @return void
* @throws Exception - An unexpected exception
*/
public void closeConnection() throws Exception
{
System.out.println("closing connection:");
connectionString=null;
resSet.close();
stmt.close();
conn.close();
}
}