审计记录

src/main/java/com/xxxcom/xxxproduct/JDBCExample.java 1.9 KB
胡敏 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
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) {
		  
	 }
}