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
Win a copy of
Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework
this week in the
Java in General
forum!
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
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
JDBC and Relational Databases
export query from table column to text file
aypa genga
Greenhorn
Posts: 5
posted 15 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: 80083
412
posted 15 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 15 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: 80083
412
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
avani vakharwala welcome to JavaRanch
And thank you.
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
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...