使用SqlConnection的JOOQ DSL

时间:2016-12-10 18:44:19

标签: jooq vert.x

我正在使用AsyncSQLClient来获取异步。在vertx中连接到我的数据库。现在我正在努力如何使用JOOQs DSL。我正在尝试以下方法:

client.getConnection(res -> {
    if (res.succeeded()) {
          SQLConnection connection = res.result();

           DSL dsl = DSL.using(connection, SQLDialect.POSTGRES_9_4);

           connection.close();
           client.close();
    } else {
    }
});

这不起作用,因为使用需要Connection而不是SQLConnection。有没有办法在JOOQ中使用SQLConnection?有没有其他方法可以为JOOQ创建异步连接?

3 个答案:

答案 0 :(得分:1)

不,你不能将JOOQ与Vert.x AsyncSQL客户端一起使用。

但Vert.x社区中有人创建了jOOQ CodeGenerator to create vertxified DAOs and POJOs

答案 1 :(得分:1)

您可以为vert.x SQLConnection类型实现(和开源!)“代理”。例如,如果要运行以下vert.x方法:

interface SQLConnection {
    SQLConnection queryWithParams(
        String sql, 
        JsonArray params, 
        Handler<AsyncResult<ResultSet>> resultHandler
    );
}

您的代理会公开这样的方法:

class jOOQSQLConnection {
    final SQLConnection delegate;
    <R extends Record> jOOQSQLConnection query(
        ResultQuery<R> sql, 
        Handler<AsyncResult<Result<R>>> resultHandler
    ) {
        wrapInjOOQSQLConnection(

            // Extract the SQL string from the jOOQ query
            delegate.query(sql.getSQL()),

            // Extract the bind variables from the jOOQ query and wrap them in the 
            // JsonArray type, as requested by vert.x 
            wrapjOOQParamsInJsonArray(sql.getBindValues()),

            // This is a handler that receives a vert.x ResultSet and wraps / transforms
            // it into a jOOQ result (which contains the <R> type for type safety)
            r -> resultHandler.handle(wrapInjOOQResult(r.result()))
        );
    }
}

上述内容可能无法开箱即用,但它可以让您了解如何包装。

答案 2 :(得分:0)

using (SqlConnection con = connection.getconnection()){

                    SqlCommand cmd = new SqlCommand("empreg", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@empid", txtempid.Text);
                    cmd.Parameters.AddWithValue("@empname", txtempname.Text);
                    cmd.Parameters.AddWithValue("@salary", txtsalary.Text);
                    cmd.Parameters.AddWithValue("@tell", txttell.Text);
                    cmd.Parameters.AddWithValue("@address", txtaddress.Text);
                    cmd.Parameters.AddWithValue("@blog", txtblog.Text);
                    cmd.Parameters.AddWithValue("@gender", cmbgender.Text);
                    cmd.Parameters.AddWithValue("@hiredate", dtpdate.Value);
                    cmd.Parameters.AddWithValue("@op", op);
                    int i = cmd.ExecuteNonQuery();

                    //If i > 0 AND op = "insert"
                    if (i > 0 && op == "insert"){

                        MessageBox.Show("1 row is  saved sucessfuly ");
                        submode.readdgv("emp", DGV2);
                        submode.autoid(txtempid, "emp");

                        txtempname.Clear();
                        txtsalary.Clear();
                        txttell.Clear();
                        txtaddress.Clear();
                        txtblog.Clear();
                        cmbgender.SelectedIndex = -1;
                        dtpdate.Value = DateTime.Now;
                        txtempname.Focus();

                    }

                    else if (i >= 0 && op == "update"){

                        MessageBox.Show("1 row is updated sucessfuly ");
                        submode.readdgv("emp", DGV2);
                        submode.autoid(txtempid, "emp");

                    }

                    else if (i >= 0 && op == "delete"){

                        MessageBox.Show("1 row is deleted sucessfuly");
                        submode.readdgv("emp", DGV2);
                        submode.autoid(txtempid, "emp");

                    }
                    else{

                        MessageBox.Show("process is failed");
                        submode.readdgv("emp", DGV2);
                        submode.autoid(txtempid, "emp");

                    }

                }