GroovySql 查询数据库示例(groovy 2.2)

本文介绍了GroovySQL 2.2版本下如何查询数据库,包括建立连接、执行SQL语句以及防止SQL注入的方法。通过示例展示了使用GroovySQL进行数据库操作的简单性和便利性。
摘要由CSDN通过智能技术生成

版本:groovy 2.2

API:JDBC/GroovySQL


package DbUtil_Shield
import groovy.sql.Sql
import java.sql.Connection;

import net.sourceforge.jtds.jdbc.Driver
/**
 *
 * @author Administrator
 */
class DefaultGroovySqlDbUtil {
      
    def url="localhost";
        def user="sa";
        def password="sa";
        def port=8087;
        def dbName="shiled";
        
    Sql  getGroovySql(){
        
            def sql=Sql.newInstance("jdbc:jtds:sqlserver://localhost:4512/dbxxx"sa","sa","net.sourceforge.jtds.jdbc.Driver");
           
        
        def sql_id_list="select id  from v_products where ctid=65535 and is_auditing=1 and trueeship='0' and subsite_auditing='1'"
        
   
            sql.eachRow(sql_id_list){
                row  ->
                println "${ row.id}"
            }
            
        
    }
    
    static void main(argvs){
        
        
        def dbutil=new DefaultGroovySqlDbUtil();
        
        println dbutil.getUser();
        
        dbutil.getGroovySql();
        
        
        
    }
}







官方文档:

groovy.sql
Class Sql

    java.lang.Object
        groovy.sql.Sql

    Direct Known Subclasses:
        DataSet


    public class Sql
    extends Object

    A facade over Java's normal JDBC APIs providing greatly simplified resource management and result set handling. Under the covers the facade hides away details associated with getting connections, constructing and configuring statements, interacting with the connection, closing resources and logging errors. Spe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值