JDBC源码分析之使用自定义的驱动包链接MySQL(非官方编译的JDBC)

21 篇文章 0 订阅
11 篇文章 0 订阅

自己想要学习JDBC,首先能够编译一个JAR包,如果能直接用那就非常的不错。
但是,如果遇到了错误,需要进行深度学习。

错误如下:

Exception in thread “main” java.sql.SQLException: No suitable driver found for jdbc:mysql://192.168.61.165:15307/kundb1?continueBatchOnError=false&useServerPrepStmts=true&alwaysSendSetIsolation=false&useLocalSessionState=true&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC&useCursorFetch=true
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at com.sql.Test.test(Test.java:25)
at com.sql.Test.main(Test.java:12)

这时候,可以先用官方的驱动,验证一下自己的测试代码。如果没问题,那就是自己编译的jar包问题。

之前,用Eclipse自己玩了一下,通过工具导出,发现不对,这里重新记录下。

首先,在此页面下载所有需要的安装包。

https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-installing-source.html

相关的安装包,存放到指定目录,例如:
/home/swu/code/jdbc/libJar
在这里插入图片描述

然后,本地安装ant编译器

sudo apt install ant

接下来,从github下载源码:
https://github.com/mysql/mysql-connector-j

下载的目录位置:
/home/swu/code/jdbc/mysql-connector-j

在此目录下,创建一份新的文件, 命名为:build.properties
内容加入:

$ cat build.properties
com.mysql.cj.build.jdk=/opt/jdk8
com.mysql.cj.extra.libs=/home/swu/code/jdbc/libJar

其中:jdk安装在**/opt/jdk8**;
相关的依赖包,存放在**/home/swu/code/jdbc/libJar**

最后,在/home/swu/code/jdbc/mysql-connector-j执行命令:

$ ant dist
Buildfile: /home/swu/code/jdbc/mysql-connector-j/build.xml

-extra-libs-check:

-jdk-check:

-compiler-check:

clean:

-load-info-properties:

-init-copy-common:
    [mkdir] Created dir: /home/swu/code/jdbc/mysql-connector-j/build
     [copy] Copying 551 files to /home/swu/code/jdbc/mysql-connector-j/build/mysql-connector-java-8.0.26-SNAPSHOT

-init-filter-license:

-init-no-crypto:

-init-license-headers:

-init-copy:

-init-notices-commercial:

-init-notices-gpl:
      [get] Getting: file:./LICENSE
      [get] To: /home/swu/code/jdbc/mysql-connector-j/build/mysql-connector-java-8.0.26-SNAPSHOT/LICENSE
      [get] .
     [copy] Copying 1 file to /home/swu/code/jdbc/mysql-connector-j/build/mysql-connector-java-8.0.26-SNAPSHOT

-init-info-files:
     [echo] ## INFO_BIN ##
     [echo] build-date: 2021-08-18 14:14:43 +0800
     [echo] os-info: Linux amd64 5.11.0-25-generic
     [echo] compiler: javac 1.8.0_201
     [echo] build-tool: Apache Ant(TM) version 1.10.7 compiled on October 24 2019
     [echo] ## INFO_SRC ##
     [echo] version: 8.0.26-SNAPSHOT
     [echo] branch: release/8.0
     [echo] date: 2021-08-17 20:08:23 +0800
     [echo] commit: ce250d1cce15a71c867c98e152df5892a14fac31
     [echo] short: ce250d1c

init:

-clean-output:

compile-driver:
     [echo] Compiling MySQL Connector/J JDBC implementation with '/opt/jdk8' to 'build/mysql-connector-java-8.0.26-SNAPSHOT'
    [javac] Compiling 529 source files to /home/swu/code/jdbc/mysql-connector-j/build/mysql-connector-java-8.0.26-SNAPSHOT
    [javac] Creating empty /home/swu/code/jdbc/mysql-connector-j/build/mysql-connector-java-8.0.26-SNAPSHOT/com/mysql/cj/xdevapi/package-info.class
    [javac] Creating empty /home/swu/code/jdbc/mysql-connector-j/build/mysql-connector-java-8.0.26-SNAPSHOT/com/mysql/cj/x/protobuf/package-info.class
     [java] Applying CommonChecks.
     [java] Applying TranslateExceptions.
     [java] Applying AddMethods.

-compile-integration-c3p0:
     [echo] Compiling MySQL Connector/J-c3p0 integration with '/opt/jdk8' to 'build/mysql-connector-java-8.0.26-SNAPSHOT'
    [javac] Compiling 1 source file to /home/swu/code/jdbc/mysql-connector-j/build/mysql-connector-java-8.0.26-SNAPSHOT

compile-integration:

compile:

dist:
    [mkdir] Created dir: /home/swu/code/jdbc/mysql-connector-j/build/mysql-connector-java-8.0.26-SNAPSHOT/META-INF/services
     [copy] Copying 4 files to /home/swu/code/jdbc/mysql-connector-j/build/mysql-connector-java-8.0.26-SNAPSHOT/META-INF
      [jar] Building jar: /home/swu/code/jdbc/mysql-connector-j/build/mysql-connector-java-8.0.26-SNAPSHOT/mysql-connector-java-8.0.26-SNAPSHOT.jar

BUILD SUCCESSFUL
Total time: 34 seconds

生成jar包存放在当前目录的build子目录,可以直接使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值