![](/img/trans.png)
[英]I am trying use custom annotation in java . But not able to get the annotation in
[英]I am trying to export MySql data to a file using Java, but I am not able to get the table headers
我能够将mysql导出到csv文件,但是我没有获取表头,我希望将文件保存到系统时显示头。
这是我正在使用的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
这是作为相关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');
简而言之,您必须手动添加列标签。
您不能将executeUpdate()
用于选择查询.....请使用executeQuery()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.