JDBC简化连接

时间:2008-11-24 07:23:05

标签: java jdbc

是否有任何库可以简化使用JDBC连接服务器的任务?可以采用mysql://username:password@host/db之类字符串的内容,类似于herePHP MDB2所做的内容。

我对Spring之类的ORM或复杂库不感兴趣,因为我甚至没有使用Java语言(但仍然在JVM上)这样做。

1 个答案:

答案 0 :(得分:0)

以下是使用Spring的JdbcTemplate的示例。

def create
    build_resource(sign_up_params)

    resource[:provider] = "<default provider>"
    if !User.find_by_email(resource.email) then
        resource.save
        yield resource if block_given?
        if resource.persisted?
            if resource.active_for_authentication?
                set_flash_message! :notice, :signed_up
                sign_up(resource_name, resource)
                respond_with resource, location: after_sign_up_path_for(resource)
            else
                set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
                expire_data_after_sign_in!
                respond_with resource, location: after_inactive_sign_up_path_for(resource)
            end
        else
            set_flash_message! :error, :"Something went wrong"
            clean_up_passwords resource
            set_minimum_password_length
            respond_with resource
        end
    else
        user = User.find_by_email(resource.email)
        if user.identities.count > 0 and user.provider != "<default provider>" then
        @identity = Identity.new
        @identity.provider = "made up provider"
        @identity.save
        user.password = resource.password
        if resource.country != nil and resource.country != "" then
             user.country = resource.country
        end
        if resource.title != nil and resource.title != "" then
            if resource.title != nil and resource.title != "" then
                user.title = resource.title
            end
            if resource.first_name != nil and resource.first_name != "" then
                user.first_name = resource.first_name
            end
            if resource.last_name != nil and resource.last_name != "" then
                user.last_name = resource.last_name
            end
            if resource.phone_number != nil and resource.phone_number != "" then
                user.phone_number = resource.phone_number
            end
            if resource.country_code != nil and resource.country_code != "" then
                user.country_code = resource.country_code
            end
            user.provider = "<default provider>"

            user.save
            yield user if block_given?
            if user.persisted?
                if resource.active_for_authentication?
                    set_flash_message! :notice, :signed_up
                    respond_with resource, location: after_sign_up_path_for(resource)
                else
                    set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
                    expire_data_after_sign_in!
                    respond_with resource, location: after_inactive_sign_up_path_for(user)
                end
            else
            set_flash_message! :error, :"Something went wrong"
            clean_up_passwords resource
            set_minimum_password_length
            respond_with resource
        end
    else
        set_flash_message! :error, "This Account already exists"
        redirect_to new_user_session_path
    end
end