exporting data into sql Script



import java.sql.*;
import java.io.*;
public class JavaDemo125{
    public static void main(String ar[])throws Exception{
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test","system","123456");
        Statement st=con.createStatement();
        String qr="select *from users";
        ResultSet rs=st.executeQuery(qr);
        ResultSetMetaData rsmd=rs.getMetaData();
        int cols=rsmd.getColumnCount();
        String str="";
        while(rs.next()){
            String q="insert into users values(";
            for(int i=1;i<=cols;i++){
                String s1=rs.getString(i);
                q=q+"'"+s1+"',";
            }            
            String q2=q+")";
            //int i=q2.lastIndexOf(",");
            q2=q2.replace(",)",");");
            str=str+q2+"\n";
        }
        System.out.println(str);
        FileWriter fw=new FileWriter("data.sql");
        fw.write(str);
        fw.close();
        con.close();
    }
}



No comments:

Popular Posts