简体   繁体   English

远程MySQL DB和Eclipse

[英]Remote MySQL DB and eclipse

I am currently trying to implement a simple servlet that has to communicate with our database. 我目前正在尝试实现一个必须与数据库通信的简单servlet。

I have no real prior experience with databases, so I was wondering how I should I go about this? 我以前没有真正的数据库经验,所以我想知道我应该怎么做? I have downloaded the mysql-connector-java-5.1.40 from dev.mysql. 我已经从dev.mysql下载了mysql-connector-java-5.1.40。

Going over some of the directions on the web for setting up the connection, it seems to only be for local mysql, but what of remote? 查看网络上用于建立连接的一些说明,它似乎仅适用于本地mysql,但是远程使用什么呢? The remote's user and pass is demo/demo; 远程用户和通行证是演示/演示; of course I would also need to log into the the remote server with my credentials. 当然,我还需要使用我的凭据登录到远程服务器。 How do I go about connecting to this remote db? 如何连接到该远程数据库?

Edit: So I believe I successfully connected to the DB, at least I can see it in my eclipse under data sources and the tables are present (company and stock_prices), however my eclipse still says I have an unsuitable driver even though I do have one associated with it. 编辑:所以我相信我已经成功连接到数据库,至少我可以在Eclipse中的数据源下看到它,并且存在表(company和stock_prices),但是我的Eclipse仍然说我有不合适的驱动程序,即使我确实有与之相关的一种。

The proper way of consuming a database resources in a web container (or in an application server) is through the javax.sql.DataSource abstraction. 消耗Web容器(或应用程序服务器)中的数据库资源的正确方法是通过javax.sql.DataSource抽象。 So you should configure a data source in your container. 因此,您应该在容器中配置数据源。 For tomcat it's as simple as creating a file named context.xml in your war's META-INF folder with the following content (replace address and credentials with your own): 对于tomcat,就像在您的战争的META-INF文件夹中创建一个名为context.xml的文件一样简单,其中包含以下内容(用您自己的地址和凭据替换):

<Context>
    <Resource name="jdbc/[YourDatabaseName]"
        auth="Container"
        type="javax.sql.DataSource"
        username="[DatabaseUsername]"
        password="[DatabasePassword]"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql:/[yourserver]:3306/[your-db]"
        maxActive="20"
        maxIdle="20"/>
</Context>

Then when you want to perform a DB operation: 然后,当您要执行数据库操作时:

  • you either look up the data source: 您可以查找数据源:

    DataSource ds =(DataSource) new InitialContext().lookup("java:comp/env/jdbc[YourDatabaseName]"); DataSource ds =(DataSource)new InitialContext()。lookup(“ java:comp / env / jdbc [YourDatabaseName]”);

  • or simply use dependency injection for managed components like servlets: 或仅对servlet之类的托管组件使用依赖项注入:

    @Resource(name="jdbc/YourDataSource") Datasource ds;

The you just get a connection from the datasource in order to execute statements to the database. 您只需从数据源获得连接即可执行到数据库的语句。

The DB driver can be put in one of two places: DB驱动程序可以放在两个位置之一:

  • the war's lib folder 战争的lib文件夹
  • tomcat's lib folder Tomcat的lib文件夹

It's recommended to put it in tomcat's lib, because drivers are singletons and if you have several apps with different versions of the driver in the same container bad things will happen. 建议将其放在tomcat的lib中,因为驱动程序是单例,并且如果在同一容器中有多个具有不同版本驱动程序的应用程序,则可能会发生不良情况。

How do I go about connecting to this remote db? 如何连接到该远程数据库?

Connecting to a remote DB is the same as connecting to alocal DB. 连接到远程数据库与连接到本地数据库相同。 Just pass the correct DB address in the connection string. 只需在连接字符串中传递正确的数据库地址即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM