`
asjava
  • 浏览: 137928 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

JDBC 连接 MariaDB 步骤原理及依赖包

 
阅读更多

获取driver(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.

把.jar文件复制到程序的classpath下

如果是在Eclipse中做工程,则参照<使用Eclipse时导入第三方jar包之常用方法>
http://jingyan.baidu.com/article/6079ad0e7e4de128fe86db40.html

Requirements

  • Java 7 (until April 2015) or 8
  • com.sun.JNA is used by some library functions and a jar is available at

 https://github.com/twall/jna

  • only needed when connecting to the server with unix sockets or windows shared memory

 

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

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 not com.mysql.jdbc.Driver butorg.mariadb.jdbc.Driver!

注意MariaDB 提供的驱动类已经不再是com.mysql.jdbc.Driver 而是org.mariadb.jdbc.Driver了

 

Connection strings

Format of the JDBC connection string is

JDBC连接字符串:

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

Altenatively

也可以用:

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

can also be used.

 

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 withMariaDB 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 are true

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

When possible, following the two rules above provides both better speed and eliminates concerns about SELECT privileges on the 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();

 

关于mariadb对SLF4J的依赖

经测mariadb-java-client-1.2.2.jar需要SLF4J的支持. 如果没有导入SLF4J 的jar包, 连接数据库时会报如下错误:

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
 at org.mariadb.jdbc.Driver.<clinit>

Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory

 

导入SLF4J核心包(slf4j-api-1.7.12.jar)后, 程序可以正常运行, 但仍会报一个警告:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

导入slf4j-jdk14-1.7.12.jar包后修复.

 

附SLF4J下载地址:http://www.slf4j.org/download.html

分享到:
评论

相关推荐

    mariadb jdbc(连接列子有jar包)

    MariaDB JDBC连接示例及驱动使用详解 在Java应用程序中,与数据库进行交互是常见的需求。MariaDB,作为MySQL的一个分支,提供了强大的功能和良好的性能。为了在Java中连接到MariaDB数据库,我们需要使用JDBC(Java ...

    MariaDB + gelara集群部署(可离线),附带安装包及依赖包

    1. **JDBC连接**:使用Java进行数据库连接,需要引入JDBC驱动,如MariaDB的Java Connector/J。通过`Class.forName()`加载驱动,`DriverManager.getConnection()`建立连接。 2. **连接池**:为了提高性能和资源利用...

    mariadb-10.6.3安装包,包含linux,windows版本.zip

    MariaDB是MySQL的一个分支,由MySQL的原始开发者创建,旨在保持开源数据库的纯粹性并提供更先进的特性。这个压缩包“mariadb-10.6.3安装包,包含linux,windows版本.zip”包含了针对两种主要操作系统——Linux和...

    mariadb-1.5.x jar包

    使用`mariadb-java-client-1.5.x.jar`,你需要在你的Java项目中将其添加为依赖,这可以通过以下步骤完成: 1. **添加依赖**:如果你使用的是Maven,可以在`pom.xml`文件中添加以下依赖: ```xml &lt;groupId&gt;org....

    阿里云 mariadb安装配置

    - 对于使用Java程序连接MariaDB的场景,需要将原先使用的MySQL JDBC驱动替换为MariaDB JDBC驱动。示例中给出的是如何在`pom.xml`文件中进行依赖替换,将其替换为`org.mariadb.jdbc`的`mariadb-java-client`,版本号...

    mariadb-java-client-1.2.3

    "mariadb-java-client-1.2.3.jar" 是MariaDB Java客户端的JAR文件,它包含了驱动程序和所有必要的类,使得Java应用能够通过JDBC API连接到MariaDB服务器。这个版本号1.2.3表明这是该客户端的一个特定发行版,可能...

    Java连接各个版本Oracle/Mysql/DB2…数据库代码

    此外,现代的开发环境中,我们通常会使用依赖管理工具,如Maven或Gradle,将JDBC驱动作为项目依赖,避免手动管理jar包。在Maven的pom.xml或Gradle的build.gradle文件中添加对应的依赖即可。 总的来说,Java通过JDBC...

    mysql JDBC驱动

    MySQL JDBC驱动,全称为Java Database Connectivity (JDBC) 驱动,是Java...在Java项目中,正确配置和使用MySQL JDBC驱动是连接MySQL数据库的基础,了解其工作原理和使用方法对于提升开发效率和保证数据安全至关重要。

    CentOS7下部署tomcat8.0.33+mariadb5.5+jython2.7环境测试.doc

    以上步骤详细介绍了在CentOS7环境下部署一个包含Tomcat、MariaDB和Jython的服务器环境,这对于开发和运行Java Web应用或者基于Jython的脚本是非常必要的。注意,整个过程中需要确保所有依赖项都已正确安装和配置,以...

    hibernate 环境搭建

    5. **JDBC驱动**: 需要添加MariaDB的JDBC驱动依赖,例如对于Maven: ```xml &lt;groupId&gt;org.mariadb.jdbc &lt;artifactId&gt;mariadb-java-client &lt;version&gt;2.7.2 ``` Gradle的话: ```groovy implementation '...

    虚拟机单节点dolphinscheduler调度安装部署

    根据提供的标题、描述、标签及部分内容,我们可以详细探讨如何在虚拟机单节点上进行DolphinScheduler调度系统的安装与部署。此过程不仅涉及基础环境的准备,还包含了多个依赖组件的安装配置,如JDK、MySQL、...

    Confluence安装部署文档V20190726.docx

    执行以下命令以卸载所有MariaDB相关的包: ```bash rpm -qa | grep mariadb rpm -e &lt;package-name&gt; --nodeps ``` 注意,如果卸载时遇到依赖问题,可以使用`--nodeps`强制卸载。然后从源码包或RPM安装MySQL 5.6.34...

    Hadoop集群MYSQL的安装指南.docx

    - 修改Hive的配置文件`hive-site.xml`,指定`javax.jdo.option.ConnectionURL`为MySQL的连接字符串,例如:`jdbc:mysql://localhost/metastore?createDatabaseIfNotExist=true` - 设置用户名和密码:`javax.jdo....

    Spring Boot 关系型数据库多数据源

    1. **添加依赖**:确保项目中已经包含Spring Boot的Web和Data JPA依赖,以及针对MySQL的JDBC驱动。如果使用Mybatis,还需要添加Mybatis和Spring Boot的Mybatis starter。 2. **配置文件**:在`application.yml`或`...

    mondrian和saiku的数据库建模的指导文档,里面有具体步骤。

    - **其他依赖**:确保JDBC驱动位于drivers目录。 ##### 1.6 Schema准备 - **Schema文件**:从[此链接](http://trac.spatialytics.com/geomondrian/browser/trunk/demo/FoodMart.xml)获取Sample Schema文件。 ####...

    spring-boot-mybatis-plus数据层框架

    1. **导入依赖**:在pom.xml文件中添加MyBatis-Plus的依赖: ```xml &lt;groupId&gt;com.baomidou&lt;/groupId&gt; &lt;artifactId&gt;mybatis-plus-boot-starter &lt;version&gt;3.3.2 ``` 2. **配置数据源**:在`application....

    Linux相关软件的部署

    4. **修改数据库配置信息**:编辑`ROOT/WEB-INF/classes/systemconfig/jdbc.properties`文件,设置正确的数据库连接信息。 5. **启动Tomcat**:执行命令`#/usr/local/weifang/apache-tomcat-8.0.44/bin/startup.sh`...

    手把手教你在腾讯云上搭建hive3.1.2的方法

    在腾讯云上搭建Hive 3.1.2的步骤是一项关键的任务,尤其对于需要处理大规模数据的企业或个人开发者来说。Hive是一个基于Hadoop的数据仓库工具,它允许使用SQL语句来查询和管理分布式存储的数据。以下是详细的搭建...

Global site tag (gtag.js) - Google Analytics