首页 > 编程语言 >MariaDB_About the MariaDB Java Client

MariaDB_About the MariaDB Java Client

时间:2023-04-14 11:33:09浏览次数:62  
标签:About mariadb Java driver connection client MySQL MariaDB


via: https://mariadb.com/kb/en/about-the-mariadb-java-client/

 



Introduction

The MariaDB Client Library for Java Applications is a Type 4 JDBC driver. It was developed specifically as a lightweight JDBC connector for use with MySQL and MariaDB database servers. It's originally based on the Drizzle JDBC code, and with a lot of additions and bug fixes.

Obtaining the driver

The driver (jar and source code) can be downloaded from https://downloads.mariadb.org/client-java/

Installing the driver

Installation is as simple as placing the .jar file in your classpath.

Requirements

  • Java 6
  • A MariaDB or MySQL Server
  • maven (only if you want build from source)

Source code

The source code is available on Launchpad: https://launchpad.net/mariadb-java-client. Development version can be obtained using



bzr branch lp:mariadb-java-client



License

GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

Building and testing the driver

The section deals with building the connector from source and testing it. If you have downloaded a ready built connector, in a jar file, then this section may be skipped.

MariaDB Client Library for Java Applications uses maven for build. You first need to ensure you have both java and maven installed on your server before you can build the driver.

To run the unit test, you'll need a MariaDB or MySQL server running on localhost (on default TCP port 3306) and a database called 'test', and user 'root' with empty password



$ bzr branch lp:mariadb-java-client # Or, unpack the source distribution tarball $ cd mariadb-java-client # For the unit test run, start local mysqld mysqld, # ensure that user root with empty password can login $ mvn package # If you want to build without running unit tests, use # mvn -Dmaven.test.skip=true package



After that , you should have JDBC jar mariadb-java-client-x.y.z.jar in the 'target' subdirectory

Installing the driver

Installation of the client library is very simple, the jar file should be saved in an appropriate place for your application and the classpath of your application altered to include the MariaDB Client Library for Java Applications rather than your current connector.

Using the driver

The following subsections show the formatting of JDBC connection strings for MariaDB, MySQL database servers. Additionally, sample code is provided that demonstrates how to connect to one of these servers and create a table.

Driver Manager

Applications designed to use the driver manager to locate the entry point need no further configuration, the MariaDB Client Library for Java Applications will automatically be loaded and used in the way any previous MySQL driver would have been.

Driver Class

Please note that the driver class provided by the MariaDB Client Library for Java Applications is notcom.mysql.jdbc.Driver but org.mariadb.jdbc.Driver!

Connection strings

Format of the JDBC connection string is



jdbc:mysql://<host>:<port>/<database>?<key1>=<value1>&<key2>=<value2>...



Altenatively



jdbc:mariadb://<host>:<port>/<database>?<key1>=<value1>&<key2>=<value2>...



can also be used.

what't more,for cluster,it should be like this, jdbc:mariadb://<host1>:<port1>,<host2>:<port2>/<database>?<key1>=<value1>&<key2>=<value2>...

Optional URL parameters

General remark: Unknown options accepted and are silently ignored.

Following options are currently supported.



key description supported since version

user

Database user name

1.0.0

password

Password of database user

1.0.0

fastConnect

If set, skips check for sql_mode, assumes NO_BACKSLASH_ESCAPES is *not* set

1.0.0

useFractionalSeconds

Correctly handle subsecond precision in timestamps (feature available with MariaDB 5.3 and later).May confuse 3rd party components (Hibernated)

1.0.0

allowMultiQueries

Allows multiple statements in single executeQuery

1.0.0

dumpQueriesOnException

If set to 'true', exception thrown during query execution contain query string

1.1.0

useCompression

allow compression in MySQL Protocol

1.0.0

useSSL

Force SSL on connection

1.1.0

trustServerCertificate

When using SSL, do not check server's certificate

1.1.1

serverSslCert

Server's certificatem in DER form, or server's CA certificate. Can be used in one of 3 forms, sslServerCert=/path/to/cert.pem (full path to certificate), sslServerCert=classpath:relative/cert.pem (relative to current classpath), or as verbatim DER-encoded certificate string "------BEGING CERTIFICATE-----"

1.1.3

socketFactory

to use custom socket factory, set it to full name of the class that implements javax.net.SocketFactory

1.0.0

tcpNoDelay

Sets corresponding option on the connection socket

1.0.0

tcpKeepAlive

Sets corresponding option on the connection socket

1.0.0

tcpAbortiveClose

Sets corresponding option on the connection socket

1.1.1

tcpRcvBuf

set buffer size for TCP buffer (SO_RCVBUF)

1.0.0

tcpSndBuf

set buffer size for TCP buffer (SO_SNDBUF)

1.0.0

pipe

On Windows, specify named pipe name to connect to mysqld.exe

1.1.3

tinyInt1isBit

Datatype mapping flag, handle MySQL Tiny as BIT(boolean)

1.0.0

yearIsDateType

Year is date type, rather than numerical

1.0.0

sessionVariables

<var>=<value> pairs separated by comma, mysql session variables, set upon establishing successfull connection

1.1.0

localSocket

Allows to connect to database via Unix domain socket, if server allows it. The value is the path of Unix domain socket, i.e "socket" database parameter

1.1.4

sharedMemory

Allowed to connect database via shared memory, if server allows it. The value is base name of the shared memory

1.1.4



JDBC API Implementation Notes

Streaming result sets

By default, Statement.executeQuery() will read full result set from server before returning. With large result sets, this will require large amounts of memory. Better behavior in this case would be reading row-by-row, with ResultSet.next(), so called "streaming" feature. It is activated using Statement.setFetchSize(Integer.MIN_VALUE)

Prepared statements

The driver only uses text protocol to communicate with the database. Prepared statements (parameter substitution) is handled by the driver, on the client side.

CallableStatement

Callable statement implementation won't need to access stored procedure metadata ( mysql.proc) table, if both of following is true

  • CallableStatement.getMetadata() is not used
  • Parameters are accessed by index, not by name

When possible, following 2 rules above provides both better speed and eliminates concerns about SELECT privileges on mysql.proc table

Optional JDBC classes

Following optional interfaces are implemented by the org.mariadb.jdbc.MySQLDataSource class : javax.sql.DataSource, javax.sql.ConnectionPoolDataSource, javax.sql.XADataSource

Usage examples

The following code provides a basic example of how to connect to a MariaDB or MySQL server and create a table.

Creating a table on a MariaDB or MySQL Server



Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password"); Statement stmt = connection.createStatement(); stmt.executeUpdate("CREATE TABLE a (id int not null primary key, value varchar(20))"); stmt.close(); connection.close();



 

标签:About,mariadb,Java,driver,connection,client,MySQL,MariaDB
From: https://blog.51cto.com/u_16070335/6189774

相关文章

  • Java中File类中常用的一些方法
    File.delete()删除文件或文件夹目录。File.createNewFile()创建一个新的空文件。File.mkdir()创建一个新的空文件夹。File.list()获取指定目录下的文件和文件夹名称。File.listFiles()获取指定目录下的文件和文件夹对象。File.exists......
  • 半小时实现Java网络爬虫框架
    最近在做一个搜索相关的项目,需要爬取网络上的一些链接存储到索引库中,虽然有很多开源的强大的爬虫框架,但本着学习的态度,自己写了一个简单的网络爬虫,以便了解其中的原理。今天,就为小伙伴们分享下这个简单的爬虫程序!!首先介绍每个类的功能:DownloadPage.java的功能是下载此超链接的页......
  • Java_判断操作系统类型
    Java判断操作系统类型(适用于各种操作系统)/***平台*@authorisea533*/publicenumEPlatform{Any("any"),Linux("Linux"),Mac_OS("MacOS"),Mac_OS_X("MacOSX"),Windows("Windows"),OS2("OS......
  • Java基础---数据类型
    数据类型Java的两大数据类型:内置数据类型、引用数据类型内置数据类型Java语言提供了八种基本类型。六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型。byte、short、int、long、float、double、char、boolean基本类型范围byte:(8位)-128~127short:(26......
  • JavaScript 之 confirm,alert,prompt
    //confirmfunctiondisp_confirm(){varr=confirm("Pressabutton!")if(r==true){alert("YoupressedOK!")}else{alert("YoupressedCancel!")}}//alert<script>window.alert("确......
  • Java_打包
     发布打war包dos命令:jarcvfname.war. war包是带jsp页面,jar包不带jsp页面的. 在JDK的bin目录下提供了打包程序jar.exe。如果要展开helloapp.war文件,命令为:jarxvfhelloapp.war  假定有一个Web应用:C:\myHomemyHome/WEB-INF/……myHome/files/……myHome/image/……myHome/......
  • java.text.ParseException: Unparseable date: "11/10/10" at java.text.DateFormat.
    使用DateFormat的使用,然后自己试了下,结果出来个错误:java.text.ParseException:Unparseabledate:"11/10/10" atjava.text.DateFormat.parse(DateFormat.java:337)下面是Date的输出比较:Datedate=newDate();System.out.println(date.toString());//WedNov1010:15:05C......
  • JavaScript 之 JSON [1]介绍、语法、值
    JavaScript之JSON[1]介绍、语法、值1、简介JSON指的是JavaScript对象表示法(JavaScriptObjectNotation)JSON是轻量级的文本数据交换格式JSON使用Javascript语法来描述数据对象,但JSON仍独立于语言和平台。JSON解析器和JSON库支持许多不同的编程语言。目前非常......
  • 聊聊Java中的mmap
    mmap是什么当我们读取或修改大文件时,传统的文件I/O操作可能会变得很慢,这时候mmap就可以派上用场了。mmap(Memory-mappedfiles)是一种在内存中创建映射文件的机制,它可以使我们像访问内存一样访问文件,从而避免频繁的文件I/O操作。使用mmap的方式是在内存中创建一个虚拟地址,然后将文......
  • 使用java.util.zip对生成的字节数组输出文件流 进行打包压缩(单个、批量),并返回压缩包
    废话不多说直接上代码 packagegov.test.util;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.util.List;importjava.util.Map;importorg.apache.tools.zip.ZipEntry;importorg.apache.tools.zip.Zip......