【问题标题】:How i can use cloud mysql database with java?我如何在 java 中使用云 mysql 数据库?
【发布时间】:2013-06-08 08:26:11
【问题描述】:

我有一个内置 Java/Swing mysql 的软件。我可以使用任何云服务通过 JDBC 存储和检索数据吗?或者我还能用什么?

【问题讨论】:

    标签: java mysql database jakarta-ee crm


    【解决方案1】:

    是的,有几个 SQL-in-the-cloud 选项,包括 Amazon RDSGoogle Cloud SQL 两者都提供 MySQL。设置完成后,您可以像使用任何其他 MySQL 数据库一样使用 JDBC 连接到它们。

    【讨论】:

    • 简单的 JDBC 可以吗?或者需要更多的编码而且我的软件没有使用任何网络组件。抱歉,我对云数据库没有一点经验。
    • 简单的 JDBC 就可以了。无论您选择哪个提供商,他们的在线管理工具和文档都将帮助您确定所需的连接参数(包括身份验证)。可能最好从遵循教程开始。
    【解决方案2】:

    检查以下代码以获取参考。

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ page import="java.util.List" %>
    <%@ page import="java.sql.*" %>
    <%@ page import="com.google.appengine.api.utils.SystemProperty" %>
    
    <html>
      <body>
    
    <%
    String url = null;
    if (SystemProperty.environment.value() ==
        SystemProperty.Environment.Value.Production) {
      // Load the class that provides the new "jdbc:google:mysql://" prefix.
      Class.forName("com.mysql.jdbc.GoogleDriver"); 
      url = "jdbc:google:mysql://your-project-id:your-instance-name/guestbook?user=root";
    } else {
      // Local MySQL instance to use during development.
      Class.forName("com.mysql.jdbc.Driver");
      url = "jdbc:mysql://127.0.0.1:3306/guestbook?user=root";
    }
    
    Connection conn = DriverManager.getConnection(url);
    ResultSet rs = conn.createStatement().executeQuery(
        "SELECT guestName, content, entryID FROM entries");
    %>
    
    <table style="border: 1px solid black">
    <tbody>
    <tr>
    <th width="35%" style="background-color: #CCFFCC; margin: 5px">Name</th>
    <th style="background-color: #CCFFCC; margin: 5px">Message</th>
    <th style="background-color: #CCFFCC; margin: 5px">ID</th>
    </tr>
    
    <%
    while (rs.next()) {
        String guestName = rs.getString("guestName");
        String content = rs.getString("content");
        int id = rs.getInt("entryID");
     %>
    <tr>
    <td><%= guestName %></td>
    <td><%= content %></td>
    <td><%= id %></td>
    </tr>
    <%
    }
    conn.close();
    %>
    
    </tbody>
    </table>
    
    
      </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-28
      • 2016-08-21
      • 2013-06-08
      • 1970-01-01
      • 2017-06-20
      • 2016-08-12
      • 2018-04-03
      • 2013-11-28
      相关资源
      最近更新 更多