适用于MySQL的Java JDBC数据源部署 [英] Java JDBC DataSource Deployment for MySQL

查看:457
本文介绍了适用于MySQL的Java JDBC数据源部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何使用JDBC,以及在JDBC上的Java教程中(

I'm trying to learn how to use JDBC and in the Java Tutorial on JDBC (https://docs.oracle.com/javase/tutorial/jdbc/basics/sqldatasources.html), it says the system admin needs to create a BasicDataSource Object, set its properties, and Registering the DataSource object with a naming service using the following code:

com.dbaccess.BasicDataSource ds = new com.dbaccess.BasicDataSource();
ds.setServerName("grinder");
ds.setDatabaseName("CUSTOMER_ACCOUNTS");
ds.setDescription("Customer accounts database for billing");

Context ctx = new InitialContext();
ctx.bind("jdbc/billingDB", ds);

,然后开发人员可以使用部署的DataSource Object获取连接.

and then the developer can use the deployed DataSource Object to get a Connection.

当前,我正在使用MySQL,并下载了Connector/J,但对于如何部署数据源感到困惑.据我了解,BasicDataSource部署在服务器上.由于我将MySQL服务器设置为在本地计算机上,这是否意味着我需要创建另一个Java程序来创建此DataSource,以便我的用户程序可以连接到它,还是意味着我需要配置MySQL服务器?

Currently, I'm using MySQL and downloaded the Connector/J but I'm confused as to how to deploy the the DataSource. From my understanding, the BasicDataSource is deployed on the server. Since I set up my MySQL server to be on my local machine, does that mean I need to create another Java Program that creates this DataSource so my user program can connect to it or does it mean I need to configure my MySQL server?

推荐答案

上面的评论应该足以满足您自己的目标. 但是,如果您住院的话,可以使用以下代码片段来简化操作:

Comments above should be enough to reach your goal by yourself. But, if you are inpatient here is a code snippet to make it easier:

public static void main(String[] args) {

    //pz is the name of database, user is user name, password is password for the user
    try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/pz?" +
            "user=root&password=root")) {
        // Do something with connection, e.g:
        System.out.println("conn: " + connection.getCatalog());


    } catch (SQLException e) {
        System.err.println("Error: " + e.getMessage());
        e.printStackTrace();
    }
}

我使用的MySql J连接器是 mysql-connector-java-8.0.9-rc-bin.jar (您将收到有关SSL的警告,如下所示:

MySql J connector I used is mysql-connector-java-8.0.9-rc-bin.jar (And you'll get the warning about SSL like this:

2018年7月20日星期五20:16:10警告:不建议在没有服务器身份验证的情况下建立SSL连接.根据MySQL 5.5.45 +,5.6.26 +和5.7.6+的要求,如果未设置显式选项,则默认情况下必须建立SSL连接.为了与不使用SSL的现有应用程序兼容,将verifyServerCertificate属性设置为'false'.您需要通过设置useSSL = false显式禁用SSL,或者设置useSSL = true并为服务器证书验证提供信任库.

这篇关于适用于MySQL的Java JDBC数据源部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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