是否可以连接 mysql 和 sql server 来获取其中存在的表?
用户不应该指定类型是ms sql还是my sql? 我想在netbeans 中做吗? 如果没有的话有什么办法吗?
谢谢
The user should not specify the type whether it is ms sql or my sql?
I want to do it in netbeans?
If not is there any way?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑
不知何故,我认为您想知道如何开发可以处理多个数据库的应用程序。所以这是一个可以帮助你的模式。但要实际查询数据库并从中获取所需的所有数据,您将需要使用相应的 API。 (例如,用于 Java 应用程序的 JDBC)。读到这个答案的最后,我刚刚添加了一个如何将 JDBC 与实际的 Sql Server 数据库一起使用的示例。
(第一部分:如何管理持久性层)
你的程序应该有一个多层架构。这些层之一应该是将从数据库查询/检索数据的持久层。从现在开始,我只讨论这个持久层。
实现此目的的一种方法是使用 Java 接口。
然后,为每种数据库类型都有一个该接口的实现类。因此,您将拥有:
然后您可以有一个管理器来决定您要使用哪个数据库:
因此,在您的上层中,您将能够使用合适的数据库系统为持久层创建一个管理器:
现在您的管理器的属性
dataService
将是Mysql
类的实例,您将能够使用该类的操作。这只是每次向系统添加新数据库时需要更改的一行代码(+ 实现接口并使用数据库特定驱动程序的类)。仅供参考,这是一个桥设计模式!
(第二部分:使用 JDBC)
例如,这里是 Sql Server 数据库的实现类实际上的样子:
在
connection()
方法中,您可以看到我引用了 Sql Server 驱动程序:com.microsoft.sqlserver.jdbc.SQLServerDrive
因此,对于每个数据库,您都需要下载一个特定的驱动程序 (.jar) 并在实现类中指定。
最后,这是插入的样子:
希望这个解释能帮助您构建您想要的内容。您可能需要谷歌搜索所有这些关键字(例如 JDBC、数据库层,稍后您可以进一步使用 JPA,这是一个对象关系映射器。无论如何,关键字要在 Google 上检查!)。例如这里有一些文档来帮助您将 JDBC 与mysql。
EDIT
I somehow, thought that you wanted to know how to develop an application that can handle multiple databases. So this is a pattern that can help you with that. But to actually query your database and get all the data you want from it, you will need to use the according APIs. (JDBC for your Java application for instance). Read until the end of this answer, I just added an example of how to use JDBC with an actual Sql Server database.
(First part: how to manage your persistence layer)
You should have a multi-layer architecture for your program. One of these layers should be the persistence layer that is going to query/retrieve data from your databases. From now on, I am only going to discuss this persistence layer.
One way to do this is to use Java Interfaces.
And then, have one implementation class of this interface for each database type. So you will have:
And then you could have a manager that will decide which one of the databases you want to use:
So in your upper layers you will be able to create a manager for your persistence layer with the suitable database system:
Now the attribut
dataService
from your manager will be an instance of theMysql
class, and you will be able to use the operations of this class. That's just one line of code that you need to change every time you add a new database to your system (+ the class that implements the interface and that uses the specific drivers of your database).FYI, that's a brige design-pattern!
(Second part: working with JDBC)
For instance here is what the implement class for a Sql Server database can actually look like:
In the
connection()
method, you can see that I referenced the Sql Server driver:com.microsoft.sqlserver.jdbc.SQLServerDrive
So for each database, you will have a specific driver to download (.jar) and to specify in your implementation class.
Finally, here is what an insert looks like:
Hope this explanation will help you building what you want. You may need to google all those keywords (like JDBC, database layer, and you can go further with JPA later, wich is an Object-Relational-Mapper. Anyway, Keywords to check on Google!). For instance here is some documentation to help you use JDBC with mysql.