今天正好需要统计三个网站栏目信息更新情况,而这三个网站的后台采用了不同的数据库管理系统。初步想法是通过建立一个小的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

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

Use the EXPLAIN command to analyze the execution plan of MySQL queries. 1. The EXPLAIN command displays the execution plan of the query to help find performance bottlenecks. 2. The execution plan includes fields such as id, select_type, table, type, possible_keys, key, key_len, ref, rows and Extra. 3. According to the execution plan, you can optimize queries by adding indexes, avoiding full table scans, optimizing JOIN operations, and using overlay indexes.

Subqueries can improve the efficiency of MySQL query. 1) Subquery simplifies complex query logic, such as filtering data and calculating aggregated values. 2) MySQL optimizer may convert subqueries to JOIN operations to improve performance. 3) Using EXISTS instead of IN can avoid multiple rows returning errors. 4) Optimization strategies include avoiding related subqueries, using EXISTS, index optimization, and avoiding subquery nesting.

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

Renaming a database in MySQL requires indirect methods. The steps are as follows: 1. Create a new database; 2. Use mysqldump to export the old database; 3. Import the data into the new database; 4. Delete the old database.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
