0

我的机器上有一个数据库(northwind),我必须用 java 编写代码,以便从存储在数据库中的表(客户)中提取数据。

如果这仅特定于客户表,那么我会这样做,但我想让我的代码通用,以便我也可以通过简单地在字符串变量中给出表的名称来从其他表中提取数据。

请看一下我的代码。

主班

package main;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import model.TableModel;
import service.DBConnection;
import service.WriteExcel;

public class Main {

public static void main(String[] args) throws SQLException, ClassNotFoundException {

    double start = System.nanoTime();
    String tableName = "Customers";
    Class<?> c = Class.forName(tableName);
    Connection conn = new DBConnection().getConnection();
    System.out.println("Connection Established");

    QueryRunner run = new QueryRunner();
    ResultSetHandler<List<TableModel>> resultHandler = new BeanListHandler<TableModel>(c.getClass())
    List<TableModel> data = run.query(conn, "SELECT * FROM `" + tableName + "`;",
            resultHandler);

    WriteExcel we = new WriteExcel(tableName+"_sheet", new File(tableName+".xlsx"));
    we.writeMultipleRows(data);
    we.writeWorkbookToFile();
    System.out.println("File Written Succesfully");
    conn.close();

    System.out.println("Time Taken: " + (System.nanoTime()-start)/1000000+" ms");

}

}

在上面的代码中,在第 27 行,如果语句将如下所示

ResultSetHandler<List<TableModel>> resultHandler = new BeanListHandler<TableModel>(Customers.class);

这运行得很好,正如我所说,我希望这个语句独立于表名,使我的代码更通用。

表模型

package model;

import java.util.List;

public interface TableModel {

public List<String> getObjectAsList();

}

顾客

package model;

import java.util.ArrayList;
import java.util.List;

public class Customers implements TableModel {

private String customerId;
private String companyName;
private String contactName;
private String contactTitle;
private String address;
private String city;
private String region;
private String postalCode;
private String country;
private String phone;
private String fax;


public String getCustomerId() {
    return customerId;
}

public void setCustomerId(String customerId) {
    this.customerId = customerId;
}

public String getCompanyName() {
    return companyName;
}

public void setCompanyName(String companyName) {
    this.companyName = companyName;
}

public String getContactName() {
    return contactName;
}

public void setContactName(String contactName) {
    this.contactName = contactName;
}

public String getContactTitle() {
    return contactTitle;
}

public void setContactTitle(String contactTitle) {
    this.contactTitle = contactTitle;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getRegion() {
    return region;
}

public void setRegion(String region) {
    this.region = region;
}

public String getPostalCode() {
    return postalCode;
}

public void setPostalCode(String postalCode) {
    this.postalCode = postalCode;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getFax() {
    return fax;
}

public void setFax(String fax) {
    this.fax = fax;
}

public List<String> getObjectAsList(){
    List<String> fields = new ArrayList<>();
    fields.add(customerId);
    fields.add(companyName);
    fields.add(contactName);
    fields.add(contactTitle);
    fields.add(address);
    fields.add(city);
    fields.add(region);
    fields.add(postalCode);
    fields.add(country);
    fields.add(phone);
    fields.add(fax);
    return fields;
}

@Override
public String toString() {
    return "{ CustomerID = "+getCustomerId()+","
            + " CompanyName = "+getCompanyName()+","
            + " ContactName = "+getContactName()+","
            + " ContactTitle = "+getContactTitle()+","
            + " Address = "+getAddress()+","
            + " City = "+getCity()+","
            + " Region = "+getRegion()+","
            + " PostalCode = "+getPostalCode()+","
            + " Country = "+getCountry()+","
            + " Phone = "+getPhone()+","
            + " Fax = "+getFax()+"}";
}

}

我使用 DbUtils 库来提取数据库。欢迎任何有关增强我的代码的进一步建议。

4

1 回答 1

2

如果我正确理解您的问题,您可以尝试以下方法。

要查询表,您可以使用run.query(SQL, ResultHandler).

    ResultSetHandler<List<Map<String, Object>>> resultHandler = genericResultHandler();

    List<Map<String, Object>> result = null;

    // Execute the SQL statement and return the results in a List of
    // T objects generated by the BeanListHandler.
    try
    {
        result = run.query(sqlQuery, resultHandler, varargs);
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }

    result.stream().forEach(System.out::println);

这里有趣的部分是私有方法genericResultHandler。出于演示目的,我使用 aHashMap来存储值和相应的 cloumn 名称。

private ResultSetHandler<List<Map<String, Object>>> genericResultHandler()
{
    return new ResultSetHandler<List<Map<String, Object>>>()
    {
        @Override
        public List<Map<String, Object>> handle(java.sql.ResultSet rs) throws SQLException
        {
            List<Map<String, Object>> result = new ArrayList<>();

            // Query all rows of the table.
            while (rs.next())
            {
                // Get metadata of the table.
                java.sql.ResultSetMetaData meta = rs.getMetaData();
                int cols = meta.getColumnCount();
                Map<String, Object> data = new HashMap<>();

                // For each column store column name and value of the cell into the hashmap.
                for (int i = 1; i < cols; i++)
                {
                    String colName = meta.getColumnName(i);
                    Object value = rs.getObject(colName);
                    data.put(colName, value);
                }

                // Add the row to the result list. 
                result.add(data);
            }

            return result;
        }
    };
}

之后我使用了一些进口产品:

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

输出将是这样的(对于我的测试表):

{month=JANUARY, temperature=1.6, globalradiation=0.0, monthid=1}
{month=FEBRUARY, temperature=-0.9, globalradiation=0.0, monthid=2}
{month=MARCH, temperature=0.9, globalradiation=0.0, monthid=3}
{month=APRIL, temperature=7.2, globalradiation=0.0, monthid=4}
{month=MAY, temperature=14.1, globalradiation=0.0, monthid=5}
于 2017-11-27T11:43:22.803 回答