您的位置:

包含vertx连接mysql数据库的词条

本文目录一览:

通过代理在java连接mysql问题,怎么解决

首先,准备开发工具套件,我们并不会引入过多工具包,仅仅需要: Java8 Vert.x 3 如果你是用Maven做为项目管理工具,请将Vert.x 3引入: io.vertx vertx-core3.3.2 代码实现: package com.maxleap.mysqlproxy; import io.vertx.core.AbstractVe...

如何用java 5分钟实现一个最简单的mysql代理服务器

public class MysqlProxyServer {

private static final Logger logger = LoggerFactory.getLogger(MysqlProxyServer.class);

public static void main(String[] args) {

Vertx.vertx().deployVerticle(new MysqlProxyServerVerticle());

}

public static class MysqlProxyServerVerticle extends AbstractVerticle {

private final int port = 3306;

private final String mysqlHost = "10.10.0.6";

@Override

public void start() throws Exception {

NetServer netServer = vertx.createNetServer();//创建代理服务器

NetClient netClient = vertx.createNetClient();//创建连接mysql客户端

netServer.connectHandler(socket - netClient.connect(port, mysqlHost, result - {

//响应来自客户端的连接请求,成功之后,在建立一个与目标mysql服务器的连接

if (result.succeeded()) {

//与目标mysql服务器成功连接连接之后,创造一个MysqlProxyConnection对象,并执行代理方法

new MysqlProxyConnection(socket, result.result()).proxy();

} else {

logger.error(result.cause().getMessage(), result.cause());

socket.close();

}

})).listen(port, listenResult - {//代理服务器的监听端口

if (listenResult.succeeded()) {

//成功启动代理服务器

logger.info("Mysql proxy server start up.");

} else {

//启动代理服务器失败

logger.error("Mysql proxy exit. because: " + listenResult.cause().getMessage(), listenResult.cause());

System.exit(1);

}

});

}

}

public static class MysqlProxyConnection {

private final NetSocket clientSocket;

private final NetSocket serverSocket;

public MysqlProxyConnection(NetSocket clientSocket, NetSocket serverSocket) {

this.clientSocket = clientSocket;

this.serverSocket = serverSocket;

}

private void proxy() {

//当代理与mysql服务器连接关闭时,关闭client与代理的连接

serverSocket.closeHandler(v - clientSocket.close());

//反之亦然

clientSocket.closeHandler(v - serverSocket.close());

//不管那端的连接出现异常时,关闭两端的连接

serverSocket.exceptionHandler(e - {

logger.error(e.getMessage(), e);

close();

});

clientSocket.exceptionHandler(e - {

logger.error(e.getMessage(), e);

close();

});

//当收到来自客户端的数据包时,转发给mysql目标服务器

clientSocket.handler(buffer - serverSocket.write(buffer));

//当收到来自mysql目标服务器的数据包时,转发给客户端

serverSocket.handler(buffer - clientSocket.write(buffer));

}

private void close() {

clientSocket.close();

serverSocket.close();

}

}

}

测试一下

try {

Class.forName(name);//指定连接类型

Connection conn = DriverManager.getConnection(url, user, password);//url为代理服务器的地址

PreparedStatement pst = conn.prepareStatement("select * from test;");//准备执行语句

ResultSet resultSet = pst.executeQuery();

while (resultSet.next()) {

System.out.println(resultSet.getLong(1) + ": " + resultSet.getString(2));

}

} catch (Exception e) {

e.printStackTrace();

}

vertx怎么上传图片到MySQL

两种办法:1、以char或varchar类型保存图片的上传路径;2、以blob格式把图片以二进制保存在数据库里。建议用第一种至于用不用PDO,全凭喜好,以MySQL完全可以办到。

如何用Java快速实现一个最简单的MySQL代理呢?

首先,准备开发工具套件,我们并不会引入过多工具包,仅仅需要:

Java8

Vert.x 3

如果你是用Maven做为项目管理工具,请将Vert.x 3引入:

dependency

groupIdio.vertx/groupId

artifactIdvertx-core/artifactIdversion3.3.2/version

/dependency

代码实现:

package com.maxleap.mysqlproxy;

import io.vertx.core.AbstractVerticle;

import io.vertx.core.Vertx;

import io.vertx.core.logging.Logger;

import io.vertx.core.logging.LoggerFactory;

import io.vertx.core.net.NetClient;

import io.vertx.core.net.NetServer;

import io.vertx.core.net.NetSocket;

/**

* @author sneaky

* @since 1.0.0

*/

public class MysqlProxyServer {

private static final Logger logger = LoggerFactory.getLogger(MysqlProxyServer.class);

public static void main(String[] args) {

Vertx.vertx().deployVerticle(new MysqlProxyServerVerticle());

}

public static class MysqlProxyServerVerticle extends AbstractVerticle {

private final int port = 3306;

private final String mysqlHost = "10.10.0.6";

@Override

public void start() throws Exception {

NetServer netServer = vertx.createNetServer();//创建代理服务器

NetClient netClient = vertx.createNetClient();//创建连接mysql客户端

netServer.connectHandler(socket - netClient.connect(port, mysqlHost, result - {

//响应来自客户端的连接请求,成功之后,在建立一个与目标mysql服务器的连接

if (result.succeeded()) {

//与目标mysql服务器成功连接连接之后,创造一个MysqlProxyConnection对象,并执行代理方法

new MysqlProxyConnection(socket, result.result()).proxy();

} else {

logger.error(result.cause().getMessage(), result.cause());

socket.close();

}

})).listen(port, listenResult - {//代理服务器的监听端口

if (listenResult.succeeded()) {

//成功启动代理服务器

logger.info("Mysql proxy server start up.");

} else {

//启动代理服务器失败

logger.error("Mysql proxy exit. because: " + listenResult.cause().getMessage(), listenResult.cause());

System.exit(1);

}

});

}

}

public static class MysqlProxyConnection {

private final NetSocket clientSocket;

private final NetSocket serverSocket;

public MysqlProxyConnection(NetSocket clientSocket, NetSocket serverSocket) {

this.clientSocket = clientSocket;

this.serverSocket = serverSocket;

}

private void proxy() {

//当代理与mysql服务器连接关闭时,关闭client与代理的连接

serverSocket.closeHandler(v - clientSocket.close());

//反之亦然

clientSocket.closeHandler(v - serverSocket.close());

//不管那端的连接出现异常时,关闭两端的连接

serverSocket.exceptionHandler(e - {

logger.error(e.getMessage(), e);

close();

});

clientSocket.exceptionHandler(e - {

logger.error(e.getMessage(), e);

close();

});

//当收到来自客户端的数据包时,转发给mysql目标服务器

clientSocket.handler(buffer - serverSocket.write(buffer));

//当收到来自mysql目标服务器的数据包时,转发给客户端

serverSocket.handler(buffer - clientSocket.write(buffer));

}

private void close() {

clientSocket.close();

serverSocket.close();

}

}

}

测试一下

try {

Class.forName(name);//指定连接类型

Connection conn = DriverManager.getConnection(url, user, password);//url为代理服务器的地址

PreparedStatement pst = conn.prepareStatement("select * from test;");//准备执行语句

ResultSet resultSet = pst.executeQuery();

while (resultSet.next()) {

System.out.println(resultSet.getLong(1) + ": " + resultSet.getString(2));

}

} catch (Exception e) {

e.printStackTrace();

}

vertx为什么不火

国内没有普及。Vertx发布于2011年,除了多语言支持,也已经支持了Postgres、MySQL、MongoDB、Redis等常用组件,只在华为的开源框架servicecomb上有用到过,基本上只能看官网和英文网站才有比较深入的介绍,国内还没有普及,要将vertx用到生产,必定绕不开它的集群部署模式,而它依赖的组件(如hazelcast)的中文介绍更是少得可怜。