如何在不使用Hibernate的情况下在JDBC中提供数据库独立性。 [英] How To Provide Database Independency in JDBC Without Using Hibernate.?

查看:88
本文介绍了如何在不使用Hibernate的情况下在JDBC中提供数据库独立性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在属性文件的帮助下解决这个问题,但在属性文件中,我们只能处理数据库驱动程序问题。如果我想将MySQL切换到Oracle数据库,我需要更改我的所有查询。问题是如何在JDBC中使查询独立?

I am trying to solve this problem with the help of properties file, but in a Properties file, we can handle only Database Driver problem. If I want to switch my MySQL to Oracle database I need to change my all query. The problem is how to make query independent in JDBC?

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class DBIndependencyExample {
    public static void main(String[] args) {
        try {
            Properties pros = new Properties();
            InputStream fis = new FileInputStream(
                    "D:\\Programs\\Eclipse\\DBIndependecyByPropertiesFile\\src\\connectdb.properties");
            pros.load(fis);
            String Drivername = pros.getProperty("k1");
            //System.out.println(Drivername);
            String url = pros.getProperty("k2");
            String un = pros.getProperty("k3");
            String pw = pros.getProperty("k4");
            Class.forName(Drivername);
            Connection con = DriverManager.getConnection(url, un, pw);
            System.out.println("Driver Is Loaded With" + Drivername);
            System.out.println("Connection is Opened");
            Statement smt = con.createStatement();
            String sql = pros.getProperty("k5");
            //System.out.println(sql);
            ResultSet rs = smt.executeQuery(sql);
            while (rs.next()) {
                System.out.println("username is:" + rs.getString(1) + " password is:" + rs.getString(2));
            }
            con.close();
            System.out.println("Connection is closed");
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

属性文件:

//Mysql Connectivity 
//Start Properties File Code
k1=com.mysql.jdbc.Driver
k2=jdbc:mysql://localhost:3306/practice
k3=root
k4=root
k5=select * from student

//Oracle  Connectivity

k1=oracle.jdbc.driver.OracleDriver
k2=jdbc:oracle:thin:@localhost:1521/orcla
k3=scott
k4=manish       
k5=select * from dept


推荐答案


如果我想将我的mysql切换到oracle数据库,我需要更改我的
所有查询。

If i want to switch my mysql to oracle database i need to change my all query.

如果您的SQL查询仅依赖于ANSI SQL而从不依赖于专有特定(函数,关键字和......),那么您应该能够在不对查询进行任何更改的情况下从DBMS切换到另一个。

请注意,Hibernate不会将DBMS特性转换为另一个,例如在Oracle编写的DUAL表上转换查询使用MySQL方式。

Hibernate确保您的SQL查询在不创建本机查询时可移植,Hibernate仍然提供这种可能性。

If your SQL queries rely only on the ANSI SQL and never on proprietary specificites (function, keywords, and...) you should be able to switch from an DBMS to another one without any change in the queries.
Note that Hibernate will not translate for a DBMS specifities to another one as for example translate a query on the DUAL table written in Oracle to a MySQL way.
Hibernate ensures that your SQL queries be portable while you don't create native queries, a possibility still provided by Hibernate.

此处是原始的SQL ANSI草案,这里最新版信息技术的下载链接 - 数据库语言 - - SQL - 第1部分:框架(SQL /框架)

Here is the original SQL ANSI draft and here a download link for the last version of Information technology -- Database languages -- SQL -- Part 1: Framework (SQL/Framework)

这篇关于如何在不使用Hibernate的情况下在JDBC中提供数据库独立性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆