|
|
| java |
Expert:hendra
this is a source code you give to me, this data "Rajesh Kumar" get in "String Text" right. I want data like "Rajesh Kumar" get from database not one record but many record and field. How to read the data from database record and go to file txt.Thank's
import java.io.*;
public class WriteTextFileExample{ public static void main(String[] args)throws IOException{ Writer output = null; String text = "Rajesh Kumar";===> i want this get from database record File file = new File("write.txt"); output = new BufferedWriter(new FileWriter(file)); output.write(text); output.close(); System.out.println("Your file has been written"); } }
|
| Answers |
Hi friend,
import java.sql.*; import java.io.*;
Code to help in solving the problem :
public class FileWrite{ public static void main(String[] args) { System.out.println("MySQL Connect Example."); Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "jdbctutorial"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "root"; Statement stmt=null; Writer output = null; String text = ""; try { File file = new File("write.txt"); output = new BufferedWriter(new FileWriter(file)); Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url+dbName,userName,password); stmt = conn.createStatement(); String query = "select * from tablename"; ResultSet rs = stmt.executeQuery(query); while(rs.next()) { text = rs.getString(1); output.write(text); output.close(); } System.out.println("Your file has been written"); System.out.println("Connected to the database"); conn.close(); System.out.println("Disconnected from database"); } catch (Exception e) { e.printStackTrace(); } } }
For more information on Java IO visit to :
http://www.roseindia.net/java/example/java/io/
Thanks
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|