MY SQL & FILE

/*

        Author:ZT
	Date : Sep 07,2017
	Purpose: Read from MYSQL & Write TO Text File


*/



package a.b.c;


import javax.swing.*;
import java.sql.*;
import java.io.*;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;


public class sqlandfile {


	static Connection con=null;
	static Statement  stmt=null;
	static ResultSet  rs=null;
	static PrintWriter out=null;

	//
	static String jdbc_Driver="com.mysql.jdbc.Driver";
	static String db_url="jdbc:mysql://localhost/db1";
	// static String db_url="jdbc:mysql://localhost:3306";
	static String usr="User ID";
	static String pass="PassWord";


	
	public static void main(String args[]){

		String welcome="Welcome to SQL & File ";

		System.out.println(welcome);
	try{


		// Open Text  File

		File f1=new File("samsam.txt");
		FileOutputStream fos=new FileOutputStream(f1);
		out=new PrintWriter(fos);


		//  Open Data Base  Connection 

		Class.forName(jdbc_Driver);
		con=DriverManager.getConnection(db_url,usr,pass);
		//stmt=con.createStatement();
		//stmt.executeUpdate("drop table db1.xx");
		//stmt.executeUpdate("create table db1.xx1(stno int)");
		stmt=con.createStatement();
		rs=stmt.executeQuery("select * from student");
		
		out.println(welcome);

		while(rs.next()){

			//String id=rs.getInt("stid");
			String id=" ";
			String stfname=rs.getString("stfname");
			String stlname=rs.getString("stlname");

			out.println(id + "   "+stfname+"   "+stlname);

		} // end of while statement
		//
		
		
	}catch(Exception e1){

		JOptionPane.showMessageDialog(null,e1.getMessage());


	}

		try {   
			Process p = Runtime.getRuntime().exec("notepad samsam.txt"); 
		}catch(Exception e2){

			JOptionPane.showMessageDialog(null,e2.getMessage());

		} 

	finally{



		try{
			if(stmt!=null){
				con.close();
			}

			if(out!=null){
				out.close();
			}

			rs.close();

		}catch(Exception ee1){

		JOptionPane.showMessageDialog(null,ee1.getMessage());


		}

	} // finally  



	} // end of main() method





} // end of class sqlandfile