-1

我正在制作一个程序,我将从文本文件中读取数据并将其存储在 mysql 中的表中。在我的文件中,数据如图所示:在此处输入图像描述

程序要做的是,首先用户给出目录,程序在其中搜索文本文件,然后创建一个包含两个字段(ID、NAME)的表,然后在其中插入值。所有文件都具有相同的结构。ID 在第三行,名字在第五行。

谁能帮我查询在表中插入这些值?我的代码中的“错误”在哪里?我的程序的代码如下:

    public static void main(String args[]) throws Exception {

        Class.forName("com.mysql.jdbc.Driver");
        Connection con = (Connection) DriverManager.getConnection(
                "jdbc:mysql://localhost:3808/mydb", "root", "root");

        String dirpath = "";
        Scanner scanner1 = new Scanner(System.in);
        while (true) {
            System.out.println("Please give the directory:");
            dirpath = scanner1.nextLine();
            File fl = new File(dirpath);
            if (fl.canRead())

                break;
            System.out.println("Error:Directory does not exists");
        }

        try {
            String files;
            File folder = new File(dirpath);
            File[] listOfFiles = folder.listFiles();

            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                    files = listOfFiles[i].getName();
                    if (files.endsWith(".txt") || files.endsWith(".TXT")) {
                        List<File> txtFiles = new ArrayList<File>();
                        txtFiles.add(listOfFiles[i]);
                        String[] parts = files.split("\\.");
                        String tablename = parts[0];
                        DatabaseMetaData dm = (DatabaseMetaData) con
                                .getMetaData();
                        ResultSet rs = dm
                                .getTables(null, null, tablename, null);

                        if (!rs.next()) {
                            System.out.println("The table '" + tablename
                                    + "' just created. The data are:");
                        } else {
                            System.out.println("The table '" + tablename
                                    + "' already exists.");
                            continue;

                        }

                        for (File txtFile : txtFiles) {
                            List sheetData = new ArrayList();

                            try {
                                FileReader in = new FileReader(txtFile);
                                BufferedReader br = new BufferedReader(in);
                                String line = br.readLine();
                                while (line != null) {
                                    System.out.println(line);
                                    line = br.readLine();

                                }
                                in.close();

                            } catch (Exception e) {
                                System.err.println("Error: " + e.getMessage());
                            }

                            getCreateTable1(con, tablename);
                            {
                                BufferedReader br = new BufferedReader(
                                        new FileReader((txtFile)));

                                String currentLine = br.readLine();
                                Map<Integer, String> Fields = new HashMap<Integer, String>();

                                while (currentLine != null) {
                                    String[] tokens = currentLine.split("\t");
                                    int id = Integer.parseInt(tokens[2]);
                                    String name = tokens[4];
                                    Fields.put(id, name);
                                    currentLine = br.readLine();
                                }

                            }
                            importData(con, txtFile, tablename);
                        }
                    }
                }
            }

        } catch (Exception e) {
            System.out.println();
        }
    }

导入功能的代码是:

    private static String importData(Connection con, File txtFile,
            String tablename) {

        Map<Integer, String> Fields = new HashMap<Integer, String>();
        try {
            Statement stmt = con.createStatement();
            String all = org.apache.commons.lang3.StringUtils.join(Fields, ",");
            String sql = "INSERT INTO " + tablename + " VALUES (" + all + ")";
            stmt.executeUpdate(sql);
            System.out.println("Fill table...");

        } catch (Exception e) {
            System.out.println(((SQLException) e).getSQLState());
            System.out.println(e.getMessage());
            e.printStackTrace();

        }
        return null;
    }
}
4

2 回答 2

1

我相信您正在创建的查询字符串有问题。这些值是整数和字符串,因此请确保您的字符串值用单引号括起来。尝试调试代码或打印“sql”变量。更好的是使用 PreparedStatement,它消除了一些这样的问题。

于 2013-06-20T10:57:59.077 回答
0

您正在使用在运行时不接受输入参数的语句,而是使用 PreparedStatement

于 2013-06-20T11:03:40.007 回答