简体   繁体   English

我正在尝试使用Java将MySql数据导出到文件中,但是我无法获取表头

[英]I am trying to export MySql data to a file using Java, but I am not able to get the table headers

I am able to export mysql to csv file but i am not getting table headers, I want headers to be displayed when my file gets saved to my system. 我能够将mysql导出到csv文件,但是我没有获取表头,我希望将文件保存到系统时显示头。

This is the java code which I am using: 这是我正在使用的Java代码:

import java.io.*;
import java.sql.*;

public class ExportData {

    public static void main(String args[]) {
        String Driver;
        Statement stmt;
        Driver = "com.mysql.jdbc.Driver";
        Connection con = null;

        try {
            Class.forName(Driver);
            con = DriverManager.getConnection(
                  "jdbc:mysql://localhost/emp", "root", "");
            if (!con.isClosed()) {
                System.out.println("Successfully connected to MySQL DataBase \n");
                stmt = con.createStatement();
                String filename = "C:/2.txt";
                String tablename = "employees";
                String sql;
                stmt.executeUpdate("SELECT * INTO OUTFILE \""
                        + filename + "\" FROM " + tablename);
            }
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
            }
        }
    }
}

您需要使用ResultSetMetaData

Here's an example that was attached as a comment to the relevant MySQL manual page: 这是作为相关MySQL手册页的注释附带的示例:

select 'idPago','fecha','lead','idAlumno','idTipoPago','idGpo',
    'idTaller','idDocente','pagoImporte','NoFactura','facturaImporte',
    'mes','formaPago','observaciones' union all
(select id_control_pagos, fecha, lead, id_alumno, id_concepto_pago, id_Gpo,id_Taller,
    id_docente, Pagoimporte, NoFactura, FacturaImporte, Mensualidad_No, FormaPago,
    Observaciones from control_pagos
    into outfile 'c:\\data.csv'
    FIELDS TERMINATED BY ','
    OPTIONALLY ENCLOSED BY '"'
    LINES TERMINATED BY '\n');

In short, you have to add the column labels by hand. 简而言之,您必须手动添加列标签。

您不能将executeUpdate()用于选择查询.....请使用executeQuery()

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

相关问题 我正在尝试在java中使用自定义注释。 但是无法获得注释 - I am trying use custom annotation in java . But not able to get the annotation in 我正在尝试使用Java中的svg libraray画一条线,但无法创建OMSVGPathSegList - I am trying to draw a line using an svg libraray in Java , but I am not able to create OMSVGPathSegList 我无法获取文件路径 - I am not able to get the file path 我无法将所有数据写入文件 - I am not able to write all data to a file 我正在尝试使用JAVA从字符串获取URL - I am trying to get URL from string using JAVA 我正在尝试使用文件中的数据创建数组 - I am trying to create an array using data from a file 我尝试在mysql表中插入数据 - I am try to insert data in mysql table 我正在尝试使用jconsole连接到具有身份验证的JMX,但我无法这样做。 但是我无需身份验证就可以连接它 - I am trying to connect to JMX with authentication using jconsole, but I am not able to do so. But I am able to connect it without authentication 我无法通过ssh连接到mysql,也不知道为什么使用Java - I am not able to connect to mysql via ssh and I don't know why using Java 我是Java的新手,并且尝试连接到derby数据库。 我正在使用netbeans 7.4 - I am a newbie with java and I am trying to connect to a derby database. I am using netbeans 7.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM