JDBCExample.java 1.9 KB
package com.xxxcom.xxxproduct;
 

import com.mongodb.MongoCommandException;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.asyncsql.AsyncSQLClient;
import io.vertx.ext.asyncsql.PostgreSQLClient;
import io.vertx.ext.jdbc.JDBCClient;
import io.vertx.ext.sql.SQLConnection;

public class JDBCExample extends AbstractVerticle {
 
	 
	public JDBCExample(){
		vertx=Vertx.vertx();
		
	}
	 
	  @Override
	  public void start() throws Exception {

	  final AsyncSQLClient client = PostgreSQLClient.createShared(vertx, new JsonObject()
	        .put("url", "jdbc:postgresql://120.76.158.62:5432/iot")
	        .put("driver_class", "org.postgresql.Driver")
	        .put("max_pool_size",10)
	        .put("username", "postgres")
	        .put("password", "Nmamtf_com"));
	 
	    client.getConnection(conn -> {
	      if (conn.failed()) {
	        System.err.println(conn.cause().getMessage());
	        return;
	      }

	      final SQLConnection connection = conn.result();
	      connection.execute("create table test(id int primary key, name varchar(255))", res -> {
	        if (res.failed()) {
	          throw new RuntimeException(res.cause());
	        }
	        // insert some test data
	        connection.execute("insert into test values(1, 'Hello')", insert -> {
	          // query some data
	          connection.query("select * from test", rs -> {
	            for (JsonArray line : rs.result().getResults()) {
	              System.out.println(line.encode());
	            }

	            // and close the connection
	            connection.close(done -> {
	              if (done.failed()) {
	                throw new RuntimeException(done.cause());
	              }
	            });
	          });
	        });
	      });
	    });
	  }
	  
	  
	 public static void main(String[] args) {
		  
	 }
}