zoukankan      html  css  js  c++  java
  • 把sql输出成。sql文件

    作者原创,转载注明出处:

    代码:

    package importfile;
    
    
    import java.io.*;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    
    import java.util.ArrayList;
    
    import java.util.List;
    
    
    
    public class test {
    	public static void main(String[] args) throws Exception {  
    	
    		SqlExport exprot = new SqlExport();
    		if(exprot.isExist())
    		{
    			System.out.println("脚本文件创建成功");
    		}else
    		{
    			System.out.println("脚本文件创建失败");
    			return;
    		}
    		List<String> lists = new ArrayList<String>();
    		lists.add("sql语句");
    		exprot.export(lists);
    		
    //		String url="jdbc:oracle:thin:@localhost:1522:orcl11g";  
    //		String user="username";  
    //		String password="password"; 
    //		Class.forName("oracle.jdbc.driver.OracleDriver"); 
    //		Connection conn = null;  
    //		conn = DriverManager.getConnection(url, user, password); 
    //    
    //  
    //		PreparedStatement pst = null;  
    //		List<String> lists = new ArrayList<String>();
    //		try {
    //    	
    //			pst = conn.prepareStatement("select * from test");  
    //			ResultSetMetaData rsd = pst.executeQuery().getMetaData();  
    //			for(int i = 0; i < rsd.getColumnCount(); i++) {  
    //				System.out.print("java类型:"+rsd.getColumnClassName(i + 1));  
    //				System.out.print("  数据库类型:"+rsd.getColumnTypeName(i + 1)); 
    //				lists.add(rsd.getColumnName(i + 1));
    //				System.out.print("  字段名称:"+rsd.getColumnName(i + 1));  
    //				System.out.print("  字段长度:"+rsd.getColumnDisplaySize(i + 1));  
    //				System.out.println();  
    //			} 
    //			for(int i = 0;i<lists.size();i++)
    //			{
    //				System.out.println(lists.get(i));
    //			}
    //      
    //      
    //			} catch(SQLException e) {  
    //				throw new RuntimeException(e);  
    //			} finally {  
    //				try {  
    //					pst.close();  
    //					pst = null;  
    //					exprot.close(exprot.getOut());
    //					} catch(SQLException e) {  
    //						throw new RuntimeException(e);  
    //					}  
    //			}  
    //  
    //		}  
    	}
    }
    class SqlExport
    {
    	public void export (List<String> list) throws IOException  
    	{
    		    
    		try {
    			for(int i =0;i<list.size();i++)
    			{
    				out.write(list.get(i)+"
    ");
    			}
    			out.write("
    ");
    			out.flush();
    		    } catch (IOException e) {
    				e.printStackTrace();
    			}
    	}
    	//判断文件是否存在
    	public  boolean isExist()
    	{
    		
    	    File file=new File(path);    
    		file.delete();
    		try {
    			out = new FileWriter(path,true);
    			return true;
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			return false;
    		}
    			   
         }
    		
    	public void close(FileWriter out) throws IOException
    	{
    		out.close();
    	}
    		
    	public FileWriter getOut() 
    	{
    		return out;
    	}
    		
    	private List<String> lists;
    	private String path = ".//sql脚本.sql";
    	private FileWriter out = null;
    }
    

      注: 其中注释部分是数据连接语句,输出到脚本的sql语句可以在注释部分去组装,

  • 相关阅读:
    Mysql初探:内存数据刷盘机制
    数据管理流程,基础入门简介
    数据分析:复杂业务场景下,量化评估流程
    架构设计:服务自动化部署和管理流程
    Hadoop框架:MapReduce基本原理和入门案例
    架构设计:微服务模式下,实现灰度发布模式
    架构设计:分布式结构下,服务部署发布
    编码风格:Mvc模式下SSM环境,代码分层管理
    开发工具:Mybatis.Plus.插件三种方式的逆向工程
    Hadoop框架:HDFS高可用环境配置
  • 原文地址:https://www.cnblogs.com/qinshou/p/6122775.html
Copyright © 2011-2022 走看看