是否可以连接 mysql 和 sql server 来获取其中存在的表?

发布于 2024-10-18 07:47:27 字数 74 浏览 2 评论 0原文

用户不应该指定类型是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皇甫轩 2024-10-25 07:47:27

编辑

不知何故,我认为您想知道如何开发可以处理多个数据库的应用程序。所以这是一个可以帮助你的模式。但要实际查询数据库并从中获取所需的所有数据,您将需要使用相应的 API。 (例如,用于 Java 应用程序的 JDBC)。读到这个答案的最后,我刚刚添加了一个如何将 JDBC 与实际的 Sql Server 数据库一起使用的示例。

(第一部分:如何管理持久性层)

你的程序应该有一个多层架构。这些层之一应该是将从数据库查询/检索数据的持久层。从现在开始,我只讨论这个持久层。

实现此目的的一种方法是使用 Java 接口。

public interface DataService
{
   connect();
   close();
   ArrayList<IUser> retrieveUserDatas(..);
   // other expected operations
   .... 
}

然后,为每种数据库类型都有一个该接口的实现类。因此,您将拥有:

class SqlServer implements DataService
{
   // Implementation of your operations using Sql Server drivers
}

class Mysql implements DataService
{ 
   // Implementation of your operations using Mysql drivers
}

然后您可以有一个管理器来决定您要使用哪个数据库:

class Manager
{
   DataService dataService;
   public Manager(string databaseType)
   {
       if(databaseType.equals("sqlserver"))
       {
          dataService = new SqlServer();
       }

       else if(databaseType.equals("mysql"))
       {
          dataService = new Mysql();
       }
   }
}

因此,在您的上层中,您将能够使用合适的数据库系统为持久层创建一个管理器:

Manager manager = new Manager("mysql"); // I want to manage my database layer with mysql

现在您的管理器的属性 dataService 将是 Mysql 类的实例,您将能够使用该类的操作。这只是每次向系统添加新数据库时需要更改的一行代码(+ 实现接口并使用数据库特定驱动程序的类)。

仅供参考,这是一个桥设计模式

(第二部分:使用 JDBC)

例如,这里是 Sql Server 数据库的实现类实际上的样子:

public class SqlServerAccess implements IDataServerAccess {

    static final String url= "jdbc:sqlserver://localhost:1433;databaseName="NAME OF YOUR DATABASE";integratedSecurity=true;";
    Connection connector;


    public void connection() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            connector = DriverManager.getConnection(url, "root", "");
        } catch (Exception ex) {
            Logger.getLogger(SqlServerAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    @Override
    public void close() {
        try {
            if(connector != null){
                connector.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(SqlServerAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

connection() 方法中,您可以看到我引用了 Sql Server 驱动程序:com.microsoft.sqlserver.jdbc.SQLServerDrive

因此,对于每个数据库,您都需要下载一个特定的驱动程序 (.jar) 并在实现类中指定。

最后,这是插入的样子:

public void insertData(Data data) {
        try {
            Statement st = dataServerAccess.connect().createStatement();
            st.execute("INSERT INTO DATA(bla, bla, bla, bla, created_at)"
                                + "VALUES('" + data.bla()
                                + "', '" + ..
                                + "', '" + ..
                                + "', '" + ..
                                + "','"  + getTodayTimestamp() +"')");
        } catch (SQLException ex) {

        } finally {
            dataServerAccess.close();
        }
    }

希望这个解释能帮助您构建您想要的内容。您可能需要谷歌搜索所有这些关键字(例如 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.

public interface DataService
{
   connect();
   close();
   ArrayList<IUser> retrieveUserDatas(..);
   // other expected operations
   .... 
}

And then, have one implementation class of this interface for each database type. So you will have:

class SqlServer implements DataService
{
   // Implementation of your operations using Sql Server drivers
}

class Mysql implements DataService
{ 
   // Implementation of your operations using Mysql drivers
}

And then you could have a manager that will decide which one of the databases you want to use:

class Manager
{
   DataService dataService;
   public Manager(string databaseType)
   {
       if(databaseType.equals("sqlserver"))
       {
          dataService = new SqlServer();
       }

       else if(databaseType.equals("mysql"))
       {
          dataService = new Mysql();
       }
   }
}

So in your upper layers you will be able to create a manager for your persistence layer with the suitable database system:

Manager manager = new Manager("mysql"); // I want to manage my database layer with mysql

Now the attribut dataService from your manager will be an instance of the Mysql 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:

public class SqlServerAccess implements IDataServerAccess {

    static final String url= "jdbc:sqlserver://localhost:1433;databaseName="NAME OF YOUR DATABASE";integratedSecurity=true;";
    Connection connector;


    public void connection() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            connector = DriverManager.getConnection(url, "root", "");
        } catch (Exception ex) {
            Logger.getLogger(SqlServerAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    @Override
    public void close() {
        try {
            if(connector != null){
                connector.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(SqlServerAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

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:

public void insertData(Data data) {
        try {
            Statement st = dataServerAccess.connect().createStatement();
            st.execute("INSERT INTO DATA(bla, bla, bla, bla, created_at)"
                                + "VALUES('" + data.bla()
                                + "', '" + ..
                                + "', '" + ..
                                + "', '" + ..
                                + "','"  + getTodayTimestamp() +"')");
        } catch (SQLException ex) {

        } finally {
            dataServerAccess.close();
        }
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文