Table of Contents
1 开发环境
2 JDBC驱动的选择
2.1 Oracle9i
2.2 SqlServer2000
3 总结
Home Database Mysql Tutorial Linux下通过JDBC连接Oracle,SqlServer和PostgreSQL

Linux下通过JDBC连接Oracle,SqlServer和PostgreSQL

Jun 07, 2016 pm 03:18 PM
jdbc linux oracle connect pass

今天正好需要统计三个网站栏目信息更新情况,而这三个网站的后台采用了不同的数据库管理系统。初步想法是通过建立一个小的Tomcat webapp,进而通过JDBC访问这三个后台数据库,并根据返回的数据生成报表。 1 开发环境 数据库管理系统:一个是SqlServer 2000,

今天正好需要统计三个网站栏目信息更新情况,而这三个网站的后台采用了不同的数据库管理系统。初步想法是通过建立一个小的Tomcat webapp,进而通过JDBC访问这三个后台数据库,并根据返回的数据生成报表。

1 开发环境

  • 数据库管理系统:一个是SqlServer 2000,另一个是Oracle 9i,再一个是PostgreSQL9.1
  • Tomcat执行平台:CentOSx64 + JDK7.0x64 (全64位环境)

2 JDBC驱动的选择

2.1 Oracle9i

Oracle官方提供了ojdbc6.jar这个type 4 JDBC驱动,仅此一个文件就能完成对JDBC的支持,将其拷贝到WEB-INF/lib下。关键代码如下:


         Class.forName("oracle.jdbc.driver.OracleDriver");
         DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
         String dbUrl = "jdbc:oracle:thin:@IP地址:1521:实例名";


oracle对于兼容性做的非常好,这个版本的JDBC驱动对oracle9i依然支持良好。


2.2 SqlServer2000

微软官方也提供了SqlServer的type 4 JDBC驱动,具体说来有两个文件:sqljdbc4.jar, sqljdbc.jar,其中带4的是最新的版本可以运行于JDK7.0下,而不带4的版本不能运行于

JDK7下,问题是sqljdbc4.jar只支持SqlServer 2005以上的版本,不支持SqlServer 2000。也就是说要想通过微软提供的JDBC驱动操作SqlServer 2000,你的JDK版本一定要比JDK7低才行,可是我们的开发环境已经确定了是JDK7。

有此想到了两个解决思路:


一是使用JDBC-ODBC桥接,unixODBC是Linux平台上的ODBC实现,进而还需要安装SqlServer 2000的ODBC驱动,www.unixODBC.org给出了两个链接,一个不是免费开源的;另一个是基于freetds的ODBC驱动,不知为什么www.freetds.org网站打不开了。我以前用过freetds的命令行连接SqlServer还是不错的。ODBC的配置非常复杂,尤其是在Linux 环境下,特别不友好。懒惰的心理下,放弃了这种思路。


二是既然freetds可以操作SqlServer,为什么不越过ODBC,让JDBC直接基于tds呢,很幸运在网上找到了一个好的解决方案:jTDS,详见http://jtds.sourceforge.net。这个项目提供了基于TDS协议的JDBC实现,是一个纯的type 4 JDBC驱动,通过一个jtds-1.3.0.jar即可满足通过JDBC访问SqlServer2000的需要。而且可以运行于JDK7.0下,正好满足我的要求。


最终采取了第二种思路,关键代码如下:


              Class.forName("net.sourceforge.jtds.jdbc.Driver");
              DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());
              String dbUrl = "jdbc:jtds:sqlserver://IP地址:1433/数据库名";


2.3 PostgreSQL9.1

官网jdbc.potgresql.org提供postgresql-9.2-1000.jdbc4.jar,这也是一个type 4 JDBC驱动,下载即可使用。关键代码如下:

           Class.forName("org.postgresql.Driver");
           dbUrl = "jdbc:postgresql://IP地址:5432/数据库名";

这个驱动加载的时候自动注册,所以不需要再单独进行registerDriver了。

3 总结

通过这个小小的项目,总结一下目前常用的数据库的JDBC驱动选择:

(1)SQL Server 对于2005及以上版本,建议使用微软官方的type 4JDBC驱动;对于2000版本,如果JDK环境较老,可以使用微软官方版本,如果JDK7.0则使用jTDS这个type 4JDBC驱动;

(2)Oracle,对于所有版本推荐使用官方的thin驱动,也就是type4的JDBC驱动;

(3)PostgreSQL, MySql,官方提供了type 4的JDBC驱动,使用即可。

网络是个宝藏,但是要找到你真正需要的东西,就必须有完整的知识体系结构,必须有清晰的分析思路。


如果需要在.net框架下访问Sqlserver和Oracle,可以参考我的另一篇博客:http://blog.csdn.net/smstong/article/details/5874451


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

See all articles