简体   繁体   English

使用Excel工作表作为Java数据库

[英]Using excel sheet as a database with java

is there a possible way to compare an input from user in java 有没有一种方法可以比较Java中用户的输入

with several attribute value in excel sheet? 在Excel工作表中有几个属性值?

I am trying to read 6 input from the user (Symptoms) and compare it with 我正在尝试从用户(症状)中读取6个输入并将其与

each attribute values in my disease symptoms excel sheet. 我的疾病症状excel表中的每个属性值。

disease symptoms is something like this: 疾病症状是这样的:

------------------------------------------------------------------------
disease_id | disease name | symptoms_1     | symptoms_2    | symptoms_3 |..

    1      |  flu         |  fever         |  dry cough    | headache  |
    2      |  diarrhea    |abdominal cramps| abdominal pain| fever     |

------------------------------------------------------------------------

first, the application will ask: 首先,应用程序将询问:

do you experience any of these symptoms, then it shows all symptoms_1 您是否遇到这些症状,然后显示所有症状_1

values in a drop-down list for user to select from it. 下拉列表中的值供用户选择。 then the it asks for the next symptoms same way. 然后它以相同的方式询问下一个症状。

ex: if I select fever from the first drop-down list(all values of symptoms_1), in java I want to increase disease 1 and disease 2 by 20% and 例如:如果我从第一个下拉列表中选择发烧(“症状_1”的所有值),则在Java中我希望将疾病1和疾病2增加20%,

then when I select dry cough in the second drop-down list(all values of symptoms_2), so disease_1 will be 40%. 然后当我在第二个下拉列表中选择干咳(症状_2的所有值)时,疾病_1将为40%。

My question is this way is possible in java using excel as a database? 我的问题是这种方式可以在Java中使用Excel作为数据库吗?

if not please give an idea to solve this problem. 如果不是,请提出一个解决此问题的想法。

Thanks. 谢谢。

It's possible. 这是可能的。 Take the Excel file as the database, sheets as tables and columns in sheets as columns for your tables. 将Excel文件作为数据库,将工作表作为表,将工作表中的列作为表的列。

1 - You need to add the JDBC ODBC driver to your project. 1-您需要将JDBC ODBC驱动程序添加到您的项目中。

2 - The code below may help you get the source of your drop-down list (user choices). 2-以下代码可以帮助您获取下拉列表的来源(用户选择)。 It assumes that you have an Excel File with a sheet1 being your Symptoms table. 假定您有一个Excel文件,其中sheet1是您的症状表。 sheet1 has columns symptoms_1, symptoms_2, ... sheet1的栏中包含症状_1,症状_2,...

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {

    public static Connection getConnection() throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String url = "jdbc:odbc:excelDB";
    String username = "yourName";
    String password = "yourPass";
    Class.forName(driver);
    return DriverManager.getConnection(url, username, password);
}

public static void main(String args[]) throws Exception {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    conn = getConnection();
    stmt = conn.createStatement();
    String excelQuery = "select symptoms_1 from [Sheet1$]";
    rs = stmt.executeQuery(excelQuery);

    while (rs.next()) {
      //Fill the data for your drop-down list
    }

    rs.close();
    stmt.close();
    conn.close();
  }
}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM