Code tagged with database

backup and restore your database with rake

namespace :db do
  namespace :backup do
    
    def interesting_tables
      ActiveRecord::Base.connection.tables.sort.reject! do |tbl|
        ['schema_info', 'sessions', 'public_exceptions'].include?(tbl)
      end
    end
  
    desc "Dump entire db."
    task :write => :environment do 

      dir = RAILS_ROOT + '/db/backup'
      FileUtils.mkdir_p(dir)
      FileUtils.chdir(dir)
    
      interesting_tables.each do |tbl|

        klass = tbl.classify.constantize
        puts "Writing #{tbl}..."
        File.open("#{tbl}.yml", 'w+') { |f| YAML.dump klass.find(:all).collect(&:attributes), f }      
      end
    
    end
  
    task :read => [:environment, 'db:schema:load'] do 

      dir = RAILS_ROOT + '/db/backup'
      FileUtils.mkdir_p(dir)
      FileUtils.chdir(dir)
    
      interesting_tables.each do |tbl|

        klass = tbl.classify.constantize
        ActiveRecord::Base.transaction do 
        
          puts "Loading #{tbl}..."
          YAML.load_file("#{tbl}.yml").each do |fixture|
            ActiveRecord::Base.connection.execute "INSERT INTO #{tbl} (#{fixture.keys.join(",")}) VALUES (#{fixture.values.collect { |value| ActiveRecord::Base.connection.quote(value) }.join(",")})", 'Fixture Insert'
          end        
        end
      end
    
    end
  
  end
end
Language Ruby / Tagged with rails, activerecord, database, rake

Test JDBC drivers

Posted by Chad Humphries over 2 years ago
A simple program to test the various JDBC drivers.

import java.sql.*;

public class TestJdbc {
	static String sql = "select * from test_table where column1 = ?"; 

	static String userId = "userid";
	static String password = "password";
	static String dbServer = "dbserver";
	static String dbName = "database";
	
	public static void main(String[] args) throws Exception {
		testMSDriver();
	}

	private static void testMSDriver() throws Exception {
		Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
		Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://"+ dbServer +";SendStringParametersAsUnicode=false;dataSourceName=SQL2000JDBC;databaseName="+ dbName +";serverName="+ dbServer +";selectMethod=cursor", userId, password);

		long start = System.currentTimeMillis();
		PreparedStatement stmt = conn.prepareStatement(sql);
		stmt.setString(1, "string1");
		ResultSet rs = stmt.executeQuery();

		while (rs.next()) {
	 		// System.out.println("Result: " + rs.getString(1));
		}
		conn.close();
		System.out.println("Elapsed time for MS driver: " + 
						(System.currentTimeMillis() - start));
	}
}
Language Java / Tagged with jdbc, database