Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within JDBC and Relational Databases
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
JDBC and Relational Databases
export query from table column to text file
aypa genga
Greenhorn
Posts: 5
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I want to export a single column of a table to a text file. How can i do it with a sql query.
Please help
Campbell Ritchie
Marshal
Posts: 79151
377
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Use a simple select statement, then write the results to the text file with a FileWriter or Formatter.
avani vakharwala
Greenhorn
Posts: 6
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.sql.*; import java.io.*; public class filejebc { /** * @param args */ public static void main(String[] args) { try { Connection cn=null; Class.forName("org.postgresql.Driver"); String url="jdbc:postgresql:postgres"; String user="postgres" ; String password="Welcome123"; cn=DriverManager.getConnection(url, user, password); Statement st = cn.createStatement(); ResultSet res = st.executeQuery("SELECT * FROM emp"); System.out.println("Employee Name: " ); File f=new File("output.txt"); FileOutputStream fp=new FileOutputStream(f); while (res.next()) { String employeeName = res.getString("empname"); fp.write(employeeName.getBytes()); String newline="\r\n"; fp.write(newline.getBytes()); System.out.println(employeeName); } fp.flush(); fp.close(); cn.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Campbell Ritchie
Marshal
Posts: 79151
377
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
avani vakharwala welcome to JavaRanch
And thank you.
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Loading Data in Table
Looking for a database to Java code generator
java code to export mssql data into csv file
How to preserve new lines when parsing
Can i store bold/italic text modified in JTextpane in Database??
More...